F4 Phantom

Do Cross-Domain Google Analytics without the Query String in Your URL

Let’s say you have two completely different primary domains, say, example.com, and purchase.com, and you want to track them as a single site on Google Analytics. For example, if a user goes from one site to the other, you want that visitor to show up on the same Google Analytics report and not count as two visitors, but rather as a single visitor who crossed domains.  How do you do it?

The Problem

The problem is that the cookies (or cookie, if you are using Universal Analytics) used by Google Analytics to track visitors won’t get passed across domain names.  This isn’t Google’s fault; it’s just the way it is for security reasons.

The solution that Google has come up with is to pass the cookie information through the link in the form of a query string variable.  Let’s say you want to have a link on example.com to purchase.com.  You would add a query string to the links to purchase.com so they would look something like this if you are using Universal Analytics:

http://purchase.com/?_ga=1.128050564.988680402.1423013712

This URL would appear in the user’s browser after they reach the site.  Personally, I thought this was REALLY UGLY and unacceptable!  Imagine users sharing this link on Twitter!  The query string is even more convoluted and has more parameters if you use Classic Analytics.

In all of my research online, I was unable to find a method to do cross-domain analytics without an ugly parameter in the URL.  So, I came up with my own trick.

In this article, I’ll briefly explain how to set up cross domain Google Analytics using their latest Universal Tracking code, then I’ll describe by trick to get rid of the ugly query string.

How to Set Up Cross-Domain Universal Analytics

Google has done a pretty good job of documenting how to set up cross-domain analytics, so I won’t repeat the individual steps here.  I used the steps in this article from Google on how to set up cross domain analytics using their latest Universal Analytics.

This code uses JavaScript to modify the links on your page which cross domains and which you want to include in your cross-domain analytics.  The nice thing is that you don’t have to individually modify each link.  Google’s JavaScript code does that for you (Google calls it “decorating” your links… Are they serious? “Uglify” would be a better word.)

However, if your page uses some tricky JavaScript or other non-HTML method to do links, the code might not work and you might have to annotate those links manually.

As I mentioned before, if you use the older Classic Analytics code to do cross-domain analytics, you will get a much uglier URL, and you will have to individually annotate all of your links and forms with an onClick JavaScript statement.  Therefore, I recommend using Universal Analytics.

The one major bump I ran into occurred when I tried to set up the new View and Filters in Google Analytics, as described in the article.  Make sure you have full Edit permissions, not only on the Google Analytics property, but also on the account to do this.  If you own the GA property, you probably already have permissions.  Otherwise, get the owner to give you full edit permissions everywhere.

Another thing you’ll want to do which is not mentioned in the Google article is to exclude self-referrals from your reports.  This article tells you how to exclude self-referrals.

If you are using Google Tag Manager and want to set up cross-domain analytics there, check out this article and excellent video on Cross Domain Tracking in Google Tag Manager.

If you’ve done the steps correctly and you click on a cross-domain link that you want to track, you should see the “decorated” URL in the browser window.

I recommend leaving your site in this state and checking your Google Analytics report in a day or two, just to make sure the data is being gathered properly.  You should be able to log into Google Analytics and see your separate domains tracked as if they were part of the same site.  The Behavior Flow diagrams should show traffic going from one domain to the other.

If you are not seeing separate domains in your reports, the problem could be that you have not set up your filters correctly.  By default, Google Analytics does not show the domain name in its reports – it only shows the part after the slash.  So, if you are going from example.com/buy to purchase.com/cart, then you would see visits from “/buy” to “/cart”.  But, if you are going from example.com to purchase.com, all of the visits will show up as “/”, which is not helpful.  If that happens, go back and set up your filters.

Don’t proceed unless you have cross-domain analytics working.

Removing the Ugly Query String Variable

If your cross-domain analytics are working, then you’re ready to kill the ugly query string.  I use a JavaScript library to read in the site’s URL from the browser, modify it to remove the “_ga” variable, and put it back. 

You could write some simple JavaScript code yourself, but URLs are deceptively complex beasts.  Removing one query string variable while leaving the others intact is not as trivial as it sounds if you account for all types of URLs with other query variables, etc.  So, I went for a third party query string manipulation plugin called URL.js.  Go to that site and click on “Build”, then check the following parameters:

URI.js - Custom Build-cropped

For our purposes, there are many settings that would probably work just fine.  These are just the settings that I used which worked.

Next, click the “Build!” button and copy the code into a file called uri.js and upload it to your website.

On all of the destination pages that you have cross-domain links to, add this code to the bottom of the page before the closing <body> tag:

In the first line, replace the src path to wherever you put your uri.js file.  Also, this code uses jQuery to trigger off of $(window).load.  You might need to do “jQuery(window).load” instead.  If you are not using jQuery, there are alternate methods using plain JavaScript, but I have not tested them.  Please leave a comment if you’ve tried any!

Now, when you visit that page from one of your other domains, the ugly query string should disappear after the page finishes loading!!!

Let me say that this technique is still experimental and not endorsed by Google by any means. Before trying this on a production site, do offline experiments!!

Google’s tracking code is asynchronous, which means that there could be timing issues where this code removes the query string variable before Google is done looking at it.  But, it has worked fine in my experiments.  I’ll post any new findings I have.

If you try this, let me know how it works for you!! – Brian

5 thoughts on “Do Cross-Domain Google Analytics without the Query String in Your URL

  1. Great article, couldn’t find a solution to this elsewhere. I know you didn’t recommend editing the url yourself, but I found using the following code with regex a little easier than including another file, and I think it should be relatively foolproof:

    $(window).load(function() {
    currentURL = window.location.href;
    newURL = currentURL.replace(/&_ga(\=[^&]*)?(?=&|$)|_ga(\=[^&]*)?(&|$)/,””);
    if(newURL.substr(-1) === “?”){newURL = newURL.slice(0, – 1)}
    history.replaceState(“state”, “”, newURL);
    });

    I *think* this will only fail if there’s ‘_ga’ at the end of a different query string or value.

    Thanks

    1. Hi Alfie,
      I am a newbie when it comes to web hosting/programming and i have similar issue where the ugly string variable pops up due to cross level domain tracking. Could you tell me where exactly should i upload the code you posted onto the server and in which format?.

      Shamil

Leave a Comment or Ask a Question