Automatic Cross Domain Tracking Revisited
If you’ve been to one of our Google Analytics trainings, you may have heard me say this: tracking visitors from one domain to another is a huge pain in the neck. Whenever someone goes from domain1.com to domain2.com, you have to take their cookie data and pass it over to the second domain via the URL. If you don’t, the visitor generates a new visit and their referral data gets jacked up.
Normally, this is a tedious, manual procedure. After making a minor change to your Google Analytics Tracking Code, you then have to modify every link on domain1.com that takes a person to domain2.com. If you have a few links, no big deal. But what if you have thousands of them?
We’ve posted before about automating the cross-domain tracking process, but a lot has happened since then. Namely, Google Analytics has a sexy new asynchronous tracking code. So we finally got around to creating a script that automatically links domains using the new code syntax.
Not only that, but this script tracks outbound link clicks and downloads, all automatically. They’ll show up as events.
We’re pretty proud of it, and since it’s so darn useful to us, we figured we’d share it with you. Give it a try and let us know how it works for you!
Step 1: Modify Your Google Analytics Tracking Code
First, you’ll need to make sure that your Google Analytics Tracking Code is set up to allow for cross-domain tracking. The _setDomainName and _setAllowLinker methods are required for this to work:
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-Y']); _gaq.push(['_setDomainName', 'domain1.com']); _gaq.push(['_setAllowLinker', true]);
Make sure that _setDomainName is set to the second-level domain that the tracking code resides on.
Step 2: Download and Modify the Script
Click here to download the script, then modify the domains and file types you’d like to track.
If I wanted to track the domains lunametrics.com and lunametricsstore.com, I’d modify line 5 to read:
var domains=["lunametrics.com", "lunametricsstore.com"];
And if I wanted to automatically track downloads for PowerPoint presentations, I’d add .ppt to the list on line 6:
var fileTypes=[".doc", ".xls", ".exe", ".zip", ".pdf", ".mov", ".mp3", ".ppt"];
Step 3: Upload Script and Include on Pages
Upload the xdomain.js file to your web server and then reference it on all pages of your site. Place the reference below your Google Analytics Tracking Code:
<script src="/path/to/xdomain.js" type="text/javascript"></script>
Because the script relies on jQuery, you will also have to reference the jQuery library at some point before the call to the script. You can download a copy of the jQuery library and host it on your web server, or just reference the one hosted by jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
Step 4: Test It!
To make sure the script is working, click on one of the links that takes a visitor from one domain to the other. Check the URL that displays in your browser location field after the new page loads. You should see a string of information in the query string:
![]()
You can also view your cookies (I use Firebug and Firecookie). Check out __utmb. The number after the first period is the pageview count. If cross-domain tracking isn’t configured properly, then it will reset when you hit the secondary domain.
__utmz should also maintain the proper referral. If you visit your primary domain directly and then move to your secondary domain, __utmz should still reflect this:
Conclusion
Hopefully this helps you set up cross-domain tracking (and outbound link tracking and file download tracking) on your site easily, without a lot of headaches.
This script was a collaborative effort by myself and Phil Anderson, who did most of the heavy lifting in jQuery. It’s supplied as-is. We’ve tested it internally, but it’s your responsibility to make sure it works on your own sites. And if you notice anything that doesn’t seem to be working, you’re free to correct the code. It’s released under the terms of the GNU General Public License, so feel free to modify and redistribute. We just ask that you keep the copyright notice on the top, and that you share any cool additions with us so we can use ‘em too!



The script link is not set!
Whoops! Should be there now, Fred!
Many thanks for creating this and sharing Michael and Phil! Will look forward to trying it out.
Hi Michael
This is super. Thanks for sharing.
I have “only” 5 questions.
1. Does the script require jquery 1.6 or will it work with jquery ver. 1.4?
2. Does the script handle both _link and _linkByPost?
3. Is there a reason for you to mix the use of ” and ‘ ?
4. Will the script work with multiple GA tracking scripts on the same page?
4. If there are subdomains to be tracked, should
var domains=["lunametrics.com", "lunametricsstore.com"];
then be (with .)
var domains=[".lunametrics.com", ".lunametricsstore.com"];
and
_gaq.push(['_setDomainName', 'domain1.com']);
be (also with .)
_gaq.push(['_setDomainName', '.domain1.com']);
Thanks a lot
Regards,
Chris
Both great and helpful! Thank you so much for sharing.
What kept me from cross domain tracking so far are the advantages of one tracking per domain. So if there are 4 websites I have 4 separated GA accounts with isolated tracking. But of course a cross domain tracking would also be superb …
… so what about maintaining the separated GA installations per site and adding the above code additionally per domain? Best of 2 worlds or big downsides like interfering codes?
Would be interested in reading replies to questions 2, 4 and 5 Chris raises above also…
Hi,
I have tried implementing the script following the instructions but it’s not working and has broken other jquery on the page, any ideas
Hi Chris/embarkd,
To answer Chris’s questions:
1) The script works on 1.4, in fact we’ve tested it extensively with 1.2
2) It does not currently handle linkByPost, that’s a good idea though
3) No, just a result of multiple people collaborating, we’ll clean it up
4) It may conflict with other scripts, if you have an example of something breaking it would help us make it more bulletproof
5) The . before subdomains on _setDomainName is only for lower level subdomains (so specific.blog.lunametrics.com) if you don’t have those without the . should be good, but we haven’t tested this script against lower level sub-domains like that yet
@Phil, thanks for the followup.
+1 on including linkByPost since the use case is often for ecommerce related cross domain tracking.
Just curiosity, but wondering why Google advised for the old tracking code to _setDomainName to “none”
pageTracker._setDomainName(“none”);
versus in the new code having as you have above
_gaq.push(['_setDomainName', 'domain1.com']);
Does it work with a right-click opening new tab?
Do the cookies pass in any case?
Getting an error in the javascipt console related to undefined method for indexOf….
I was having problems with getting this to work on multiple pages of my site but I think I figured out what the problem is:
In the xdomain.js file I have:
var domains=["mysite.com", "classifieds.mysitecom"];
What if I want this to track clicks from mysite.com/somehtml.html and 100+ other /somefile.html pages??
Surely I don’t have to list each page of my site in the xdomain.js file do I??
Can I use a “wildcard” or something here or do I need to list each page here seperately??
Any help on this will be greatly appreciated and I thank you in advance!!
@embarkd: It’s always a good idea, whenever possible to _setDomainName to the actual domain of the site. If a property has multiple domains and subdomains, setting it to “none” will cause problems. Check out this section in the GA documentation for more information.
@Michael: Can you elaborate a bit on when you receive the error or a URL where it’s happening?
@Sam: It’s based on the “onclick” event, so at the moment, it won’t link the domains if a visitor right clicks and opens in a new tab. This is definitely something to consider, though. Thanks!
@Stuart: No, you definitely do not have to list each page. Just each domain.
You mention this should go below the GA snippet. Does it need to go at the bottom of the page, or anywhere below the GA snippet in order to properly tag the links to download files and outbound links?
Thanks!
Mike
The script fails when you have an html tag like where href is undefined. Is there a way to account for this?
Where a tags do not have href defined, the script crashes! my html example didn’t print in last comment so make it sounds odd.
Please see http://stackoverflow.com/questions/9944535/dynamic-google-cross-domain-javascript-broken for potential fix to my previous comments.