December 31, 2009
Cross Domain & Sub-Domain Tracking with Google Analytics
Many of our clients have a need to track their web site visitors across domains and sub-domains.
However, Google Analytics does not automatically track a single visitor across different domains or sub-domains using the default set of tracking code, so some customization of the default code has to take place.
All of my past research suggested this should be easy, and like anything it is, if you have all of the information you need.
After many, many, many, many tests, I have come up with the following guidelines.
(Please note this is for the ga.js tracking code, not the older urchin.js code.)
Sub-Domain Tracking
Let’s start with the easy one.
Premise:
Your main site is www.abc.com.
There are links from your main site to your store, shop.abc.com, and your help section, help.abc.com.
You want to track a single visitor who views the pages on your sub-domains.
Your default tracking code is:
<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">
try {
var pageTracker = _gat._getTracker("UA-xxxxxx-1");
pageTracker._trackPageview();
} catch(err) {}</script>
All you have to do is add in the setDomainName() method for Google Analytics to track a single visitor across all of your sub-domains:
<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">
try {
var pageTracker = _gat._getTracker("UA-213407-1");
pageTracker._setDomainName(”.abc.com”);
pageTracker._trackPageview();
} catch(err) {}</script>
I don’t care what you’ve read elsewhere, PLEASE INCLUDE THE ‘.’ before the domain name setDomainName(”.abc.com”);. I’ve seen this not work without it.
(Please remove pageTracker._initData(); if you are currently using it. It is deprecated. Yes, I still need to remove this method from our code!)
Cross Domain Tracking
Now on to the slightly more involved stuff.
Premise:
Your main site is www.abc.com.
There are links from your main site to a completely different site that you manage, www.def.com.
You want to track a single visitor who visits www.def.com.
There are a few tasks that need to be completed in this situation.
- You need to add three methods to the default Google Analytics tracking code.
- You need to add a method/function call to every link or form button that takes your site visitor to def.com.
- You should place the customized Google Analytics tracking code in the <head></head> section of your page rather than just before the closing </body> tag (normal recommendation).
Alter the default tracking code:
Default code is this:
<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">
try {
var pageTracker = _gat._getTracker("UA-xxxxxx-1");
pageTracker._trackPageview();
} catch(err) {}</script>
Add in three methods:
<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">
try {
var pageTracker = _gat._getTracker("UA-213407-1");
pageTracker._setDomainName(”.abc.com”);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}</script>
You need to add in the above methods in red to every domain that you are tracking.
NOTE: pageTracker._setDomainName(”.abc.com”); should be set to the domain the tracking code is actually on.
So on abc.com:
pageTracker._setDomainName(".abc.com");
On def.com:
pageTracker._setDomainName(".def.com");
NOTE:
If you read Google’s recommendations, they tell you to use this:
pageTracker._setDomainName("none");
From all of my research and tests, which includes talking to many other experts, you should NOT use “none” as the setDomainName() value. It can cause duplicate cookies and therefore inaccurate data.
Add _link() and _linkByPost() methods to your code
In order for Google Analytics to track a single visitor across domains, the cookie information needs to be sent to the other domain(s). This is accomplished by calling the link() method on normal hyperlinks and the _linkByPost() method on form submission buttons.
Let me make something clear up front:
You only have to add these links to the domain that your visitor lands on first.
So if your visitor lands on abc.com first, then all links and form submissions to def.com need the _link() and _linkByPost() methods added to them respectively. You do NOT need to add these methods to links or posts FROM def.com to abc.com.
(If your visitors could potentially land on either domain first and then link off to the other domain, then you would want to add the methods to both sites.)
Here’s how to add the code.
Hyperlinks:
<a href="def.com/mypage.html" onclick=”pageTracker._link(this.href);return false;”>Link Here</a>
NOTE: Do not forget the return false; part of the code!
Form Submissions:
<form name=”post_form” method=”post” action=”mypage.php” onsubmit=”pageTracker._linkByPost(this);”>
Place the Google Analytics tracking code in the <head></head> section of your page
If you are using any _link() or _linkByPost() methods in your code, then the Google Analytics tracking code should be placed in the <head></head> section of your page.
This is because the ga.js file needs to be loaded before the _link() or _linkByPost() methods are called.
Chances are the page would load (and therefore the ga.js file) before a user triggered one of the methods, but better safe than sorry.
NOTE: If you want to track cross domain AND sub-domain traffic, just follow the Cross Domain Tracking guidelines above.
That’s it. Go forth and track.










Hi there,
If I want to avoid any page load problems can I divide the Google code and only initiate the vars in the header and then include the trackers in the footer?
My second question is whether we can use javascript to effectively track linking back and forth between multiple domains instead of tagging each link.
Thanks for this article it is great!
Hi Gwynn,
Yes, you can split up the code.
Every link from the main domain to the secondary domain(s) needs to pass the cookie information in the URL. One way you could ‘automate’ this would be to use jquery and unobtrusive javascript.
Load jquery.js in the head.
Somehow distinguish every href that goes to a secondary domain so jquery can isolate them. e.g. you could give them all a class of “secondary”.
Then place javascript code to add the _link() method to every instance of an href with a class of “secondary”.
$(document).ready(function(){
$(”a.secondary”).click(function(event){
pageTracker._link(this.href);return false;
});
});
Hope this helps. Thanks!
I have a cross-domain analytics problem and I wondered if anyone can help? Our primary domain is ‘harrogateholidays.co.uk’ whilst our secondary domain is ‘bestofbritishholidays.com’ which is an external booking system. We are liaising with the company behind the booking system to implement the tracking. We have tried to follow a tutorial (http://www.analyticsmarket.com/blog/cross-domain-google-analytics) using their linktagger.js file to automatically add analytics to each link.
Our problem is that our analytics stats has completely dropped off since we tried to implement the cross-domain code. http://i48.tinypic.com/qq95sm.jpg - we are a little stuck! If you can help in anyway we would very much appreciate it.
p.s. we tried copying out the ‘.’ as follows:
pageTracker._setDomainName(”harrogateholidays.co.uk”)
//pageTracker._setDomainName(”.harrogateholidays.co.uk”)
this has not worked - only to stop any stats coming through at all.
Hi Richard,
setDomainName on the bestofbritishholidays.com site should be pageTracker._setDomainName(”.bestofbritishholidays.com”);
You need to make sure the cookie information is being passed correctly from your primary domain to the secondary domain. I cannot tell from a quick glance what is going on, unfortunately.
We’d need to take a much closer look and run some tests to determine what is happening. If this is something you are interested in, you can contact my colleague John Evans, (john@closed-loop-marketing.com) about setting up some consulting time. Alternatively, here are a couple of other sources that might be able to help you: http://www.kaushik.net/avinash/ (Avinash Kaushik), http://www.epikone.com/ (Justin Cutroni).
Good luck!
Thanks for getting back to me, I really appreciate it. Surely even if the second domain wasn’t working the first domain would be? We need to formally ask the partner company to implement any changes in code to the second domain. So our aim for the time being is to try to get the first domain tracking again before we contact the company. we are currently giving this a go first. I have checked what cookies are running etc according to iq content but unfortunately we always have to wait a day to really see what our changes actually alter through analytics. I wish it was running in realtime - even if it is all free!
Hi Richard,
Please let me know how how you end up fixing it. Every situation is unique, which is why diving in and systematically testing all of the variables involved is usually required to find a solution. Yes, I wish GA reported in real time too, but it’s a great tool and getting better all of the time.