Upcoming LunaMetrics Events
San Francisco, Apr 28-30 Los Angeles, May 12-16 New York City, May 19-23 Chicago, Jun 16-18

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!

http://www.lunametrics.com/blog/2011/12/01/automatic-cross-domain-tracking-revisited/

31 Responses to “Automatic Cross Domain Tracking Revisited”

fred says:

The script link is not set! :)

Whoops! Should be there now, Fred!

Johann de Boer says:

Many thanks for creating this and sharing Michael and Phil! Will look forward to trying it out.

Chris says:

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

Jeanette says:

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?

embarkd says:

Would be interested in reading replies to questions 2, 4 and 5 Chris raises above also…

Missy says:

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

Phil says:

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

embarkd says:

@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']);

sam says:

Does it work with a right-click opening new tab?
Do the cookies pass in any case?

Michael says:

Getting an error in the javascipt console related to undefined method for indexOf….

Stuart says:

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.

Mike Belasco says:

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

Anis Ahmed Chohan says:

The script fails when you have an html tag like where href is undefined. Is there a way to account for this?

Anis Ahmed Chohan says:

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.

Anis Ahmed Chohan says:

Please see http://stackoverflow.com/questions/9944535/dynamic-google-cross-domain-javascript-broken for potential fix to my previous comments.

Dane says:

Thanks guys this is great

Thanawat T. says:

Please kindly help
1. If I want to track outbound link from http://samuiontour.com to http://samuiontour.com.r24.asia/677495/Easy-Time-Resort&cur=USD&lg=en
or http://www.worldnomads.com/

in google analytic, I have to set tracking as
_gaq.push(['_setDomainName', 'samuiontour.com']);
and in xdomain.js
set var domains=["samuiontour.com.r24.asia", "worldnomads.com"];
is it correct setting?

2. how does google analytic report
is it in even report? (what is the category and action)

Thanks you in advance for your help.

Phil Anderson says:

Hi Thanawat, it sounds like you are looking for outbound link tracking, as described by Google here http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55527

What we are describing in this article is how to carry referrer/visitor information from one domain to another, such that the referrer from domain1, would still show as the referrer when the visitor passed to domain 2.

Rob says:

Google’s GA Debug Tool is also useful for checking whether you’ve correctly set up cross-domain tracking: http://bit.ly/TQXgnU

david says:

phil,

Hi, i just found this article and I am hoping you could please help me with my cross domain tracking issues.

Just a quick recap, I buy adwords traffic and send them to http://www.A.com, then from A.com, if they want to buy my product, they go to http://www.B.com. on top of passing cookie data from A to B, I would also like to know which page and link they clicked on A.com that sent them to B.com. I can actually track these parameters by using campaign variables and assigning each link its own campaign tracking links.

I set up everything pretty much how google told us to set it up. I made sure that i added the code below on my http://www.A.com website to the GA code in my header

_gaq.push(['_setAccount', 'UA-for http://www.B.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', 'www.A.com']);

Then i added the following to my http://www.B.com website GA code:

_gaq.push(['_setAccount', 'UA-for http://www.B.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', 'www.B.com']);

I then add the onClick code to the links i wanted to track ( categorize, and label). I copied one below, please notice the utm campaign variables:

so when i tested my links by clicking on them and when i was brought to the next page, i was able to see that the URL contained my campaign variables AND the other utm variables such as __utmb and _utmz, etc.

the only problem is that its not working lol. When i go to the GA account for http://www.B.com, and i look under Advertising >”Adwords” section , i see the number of visitors is correct but everything, and i mean everything is a (not set). I dont know why its not working but its very frustrating.

Is it possbile to do campaign variable tracking AND cross domain tracking in conjunction ?I hope to get some while i still have some sanity left! thank you so much!

Tollenaere says:

Interesting script. Thanks for sharing it.

PageMax SEO says:

Google Analytics is one of the most powerful analytical tools available for your website. What’s even better is it’s free. Knowing what traffic on your website is and isn’t doing should be an essential part of your business.

michuzijr.blogspot.ru says:

Hi there just wanted to give you a quick heads up. The text in your

article seem to be running off the screen in Ie. I’m not sure if this is a

format issue or something to do with web browser compatibility but I

figured I’d post to let you know. The layout look great
though! Hope you get

the issue solved soon. Thanks

Mario says:

I have two websites: One is a blog with lots of information, the second is for the sale of an item related to the information. I plan to use the information on multiple campaign tracking codes using Google’s URL Builder in order to track which campaigns are working better than others. However when it comes to buying the product, every customer hits the same buy now button and gets routed to the second website for the actual purchase. The question is, how can I transfer the campaign tracking code gathered by the first site over to the second one I would need this in order to keep track of actual conversions.

Your answer would greatly be appreciated.

Mario

Robbin Steif Robbin Steif says:

Mario, you have to do the cross-domain tracking and have the same set of code across both sites.

Interesting topic, thanks for sharing. we found it later but great reading.