Google Analytics: Automatic tracking across multiple domains

The challenge

One of the challenges in deploying Google Analytics tracking across multiple domains is preserving the user’s session details (including referring site and any campaign tracking you may have setup).

The scenario

A user searches Google for “cheap widgets”. Your site, www.CheapWidgets.com, appears top of the search results, so the user decides to click on the link to your site. While browsing around, they click a link that leads to your insightful widget blog which is located at blog.CheapWidgets.com.

With the default Google Analytics setup, www.CheapWidgets.com will be seen as a referrer to your blog subdomain and a new set of cookies will be generated for that user, losing sight of information such as the referring site (Google) and the keywords that they used to find you (”cheap widgets”).

Ideally, this information would be preserved across all subdomains and any relevant third party domains that you own.

The solution

Google Analytics does provide a solution for this, but it’s a bit cumbersome:

Step 1: Modify your tracking code to include a couple of extra functions (which is simple enough).
Step 2: Manually tag each link to one of your subdomains or third party domains with a JavaScript function (which is annoying).
Step 3: Remember to tag each link that points to any of your domains (which is a burden). This is something that could very easily be overlooked.

The real solution

I worked in the iQ dojo with our analytics sensei Niamh to produce a script that will automate this process. It automatically tags links to a set of domains that you configure inside the script.

The first thing you need to do is update the tracking code across each of your domains and subdomains.

Updated tracking code

<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-xxxxx-x"); // Replace with your own account number
    pageTracker._setDomainName(".yourdomain.com"); // Don't forget to update this
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false);
    pageTracker._trackPageview();
</script>

The value of _setDomainName should be the root of the domain you’re installing the code on. So, the value for both blog.CheapWidgets.com and www.CheapWidgets.com would be

    pageTracker._setDomainName(".CheapWidgets.com");

The next and final step is to download the script, update the domains that you would like to track and include it on your page.

Sample configuration

var domains_to_track =
[
    'blog.yourdomain.com',
    'wiki.yourdomain.com',
    '*.someotherdomain.com'
];

Include the script with the following code (it must appear in the body, just before your tracking code):

<script src="/path/to/js/xdomain.js" type="text/javascript"></script>
<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script> [...]

How do I know if it’s working?

There are a couple of ways to tell if the script is working. Firstly, when you click a link between tracked domains, you should see the details being passed in the query string.

Google Analytics Cookie being passed via URL

You can also view the cookies quite easily in FireFox to make sure that it’s working. In the latest version of FireFox (3.5+), go to Tools → Options → Privacy → Remove indivudual cookies. In versions 3.0 and earlier go to Tools → Options → Privacy → Show Cookies.

You will get a new window with a list of all your cookies. You can type your domain into the bar at the top and it will filter all cookies for that domain. The cookie you are interested in is __utmz. When you click from one of your domains to another, you should see a set of cookies being created for each domain, but the contents of each should be identical.

Cookies window in FireFox

If you have any questions about this script, or wish to report any issues, you can drop me an email at peter.mckenna@iqcontent.com.

Categories Web analytics