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.

9 Comments

  1. This looks promising. I was just looking for a solution like this.

    Quick question? I want to track across different domains (not just subdomains). Are the examples above applicable to that? Anything I need to do differently to set this up? Thanks!

  2. Hi Max,

    Thanks for your comment. Yes, this tracks across multiple domains and not just sub-domains. The setup above works for both sub-domain tracking and cross-domain tracking, you don’t need to change anything. Just make sure to update the script with the domains you want to track and you will be fine.

    If you have any more implementation questions, feel free to mail me directly.

    Peter.

  3. Thanks for this excellent script. I have a quick question. I assume this code and the GA code needs to be at the top of your page before any links is that correct?

  4. Hey Mike,

    Thanks for the comment. Actually this script needs to appear at the end of the page, just before the tag to function correctly.

    If you have any more questions, let me know.

  5. So both the xdomain script and the GA script need to be at the bottom of the page or just the xdomain script?

    Thanks!

  6. Hey Mike, both of them need to be at the bottom of the page.
    Cheers.

  7. Pingback: Coniecto » The iHCI conference 2009

  8. Pingback: Cross Domain Tracking on Multiple Domains using Google Analytics | Leo Fogarty .com

  9. Great post :)
    I have a question also. I have a two domains with different GA accounts and I would like to implement cross domain tracking with two trackers. One, that collects cross domain data and another that collects data like as it doing right now – separately.
    Could you give an example how to do that?