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 to, 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 that takes a person to 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:

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 and, I’d modify line 5 to read:

And if I wanted to automatically track downloads for PowerPoint presentations, I’d add .ppt to the list on line 6:

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:

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:

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:


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!

Michael is a former LunaMetrician and contributor to our blog.

  • fred

    The script link is not set! 🙂

  • Johann de Boer

    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=[“”, “”];

    then be (with .)

    var domains=[“”, “”];


    _gaq.push([‘_setDomainName’, ‘’]);

    be (also with .)

    _gaq.push([‘_setDomainName’, ‘’]);

    Thanks a lot



  • Jeanette

    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…

  • Missy


    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 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”


    versus in the new code having as you have above

    _gaq.push([‘_setDomainName’, ‘’]);

  • sam

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

    • @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!

  • Michael

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

    • @Michael: Can you elaborate a bit on when you receive the error or a URL where it’s happening?

  • Stuart

    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=[“”, “classifieds.mysitecom”];

    What if I want this to track clicks from 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!!

    • @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?


  • Anis Ahmed Chohan

    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

    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
  • Dane

    Thanks guys this is great

  • Please kindly help
    1. If I want to track outbound link from to

    in google analytic, I have to set tracking as
    _gaq.push([‘_setDomainName’, ‘’]);
    and in xdomain.js
    set var domains=[“”, “”];
    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

    Hi Thanawat, it sounds like you are looking for outbound link tracking, as described by Google here

    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

    Google’s GA Debug Tool is also useful for checking whether you’ve correctly set up cross-domain tracking:

  • david


    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, then from, if they want to buy my product, they go to on top of passing cookie data from A to B, I would also like to know which page and link they clicked on that sent them to 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 website to the GA code in my header

    _gaq.push([‘_setAccount’, ‘UA-for‘]);
    _gaq.push([‘_setAllowLinker’, true]);
    _gaq.push([‘_setDomainName’, ‘’]);

    Then i added the following to my website GA code:

    _gaq.push([‘_setAccount’, ‘UA-for‘]);
    _gaq.push([‘_setAllowLinker’, true]);
    _gaq.push([‘_setDomainName’, ‘’]);

    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, 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!

  • Interesting script. Thanks for sharing it.

  • 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.


    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

    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.


    • Robbin Steif

      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.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187