Automatic Cross Domain Tracking


(Update: We have an automatic cross domain tracking script for the asynchronous version of Google Analytics. Check it out!)

Whenever a visitor crosses from one domain to another, the information contained in the cookies must be passed in the query string to the second domain. Google Analytics documentation recommends using the provided method of _link().  This looks like:

<a href=”” onclick=”pageTracker._link(‘’);return false;”>Go to our sister site</a>

Following this link will cause the visitor to arrive on the page . . .&__utmv=. . . .&_utmk=123456789&__utma=1.1234567.1234567. . .&utmz=. . .

With the values of those query parameters used by the GA code on the new site to create the necessary tracking cookies.

But when you have a very large number of links that cross from one domain to the other, it is difficult to find and modify every one, and to continue to maintain and update new links as they are created in the site.

To manage a situation like this you can use JavaScript to automatically determine which links are cross-domain links.  Once the script has determined which links are cross-domain links, it can create event listeners to wait for someone to click on that link.  When they click, the event listener will trigger the GA code that adds the cookie information to the query parameters.

There are several examples of scripts like this, and sometimes the script will need to be modified slightly to suit a particular web site or particular needs.

iQ Content published an autotracking script in this blog post:

An example of a script that I used for a particular client:

You can also find script examples in the books:

* Advanced Web Metrics by Brian Clifton (
* GA Short Cuts by Justin Cutroni (

If you have an example of an automatic tracking script for GA you’d like to share, link to it in the comments below.

John is a former LunaMetrician and contributor to our blog.

  • Is this to say I can track where that last user is coming from and use that as relevant information when monetizing?

  • John

    Yes. Normally you can see this in GA by default in the Traffic Sources reports.

    When the website crosses domains, and you want to know where they came from originally, and you want to treat more than one domain as a single website, then the sites need to implement cross domain tracking in order to see the original referrer information.

    It is that implementation that this post talked about.

  • So I was looking at the differences in you script and Peter’s. I’m trying Peter’s route first, but I wondered why your code was so much more complex. If I’m following it correctly, you check first to see if it is a specific filetype, not really a link, then you check to see if its an external link. Now are you tracking external links too.
    Is that the difference, you script also tracks downloads and external links?

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