Multiple Domains in Google Analytics
Google Analytics uses cookies to store a lot of information about visits and visitors. Because cookies are tied to a single domain, if your site uses multiple domains, Google Analytics will get very confused.
This is what happens when you use multiple domains on your site:
- A user visits your site, and the tracking code places cookies on their machine.
- The user clicks a link to a page of yours on another domain. The tracking code looks for cookies, but it can't read the other domain's cookies. Assuming this is a new visitor, it attributes the visit to a referral from your other domain.
- As the visitor continues to move between domains, their single visit will count as multiple visitors from different sources.
- You will get angry and smash a keyboard.
Parenthetically, a similar thing can happen with multiple subdomains, but the solutions are different.
The Principles of Cross-Domain Tracking
In order to track a visit seamlessly across multiple domains, you need to make two coding changes.
- Modify the basic tracking code on every page of each domain
- Add code to every link where the destination URL is on a different domain
If you want them tracked in the same profile, each of the domains should use the same UA number in their code.
These changes will pass the visitor's cookie information to the other domain where they will be duplicated. From Google Analytics' perspective, it will be one set of cookies because they will carry identical information. In reality, you will be creating a duplicate set of cookies on every domain.
This is worth clarifying. If a link on your site sends a visitor to another domain and you want that domain to be counted as part of the same visit, you must tag the link. It doesn't matter how many domains you have; this principle applies to every link on every site where the domain changes. The only time it's not necessary is if the link sends the visitor to a site you're not tracking.
The emphasis might seem unnecessary, but this fundamental principle is commonly overlooked. Incompletely coding for cross-domain tracking is one of the most common reasons for self-referrals in reports.
Change the Tracking Code
Every domain that needs to be tracked as part of the same visit will need three changes made to the tracking code. These changes are made to the code on every page right before the _trackPageview() method.
Our tracking code generator will make these changes for you automatically for every domain. Otherwise, you can make them manually.
_setAllowLinker()
This method must be set to true: _setAllowLinker(true).
This method essentially tells the code to look for cookie information coming from the other domain. When it sees it, it will duplicate the cookies for the current domain.
This method also enables the code that must be placed on each link. This is why it must be placed on both sending and receiving domains.
_setAllowHash()
This method must be set to false: _setAllowHash(false).
By default, the tracking code ties a set of cookies to a domain by making a hash of the domain name and placing that value in the cookies. If your site utilizes multiple domains, though, this will prevent the code from accepting the other domain's cookies because the hash won't match up. Setting this method to false just inserts the number 1 into the cookies in place of the hash.
_setDomainName()
As explained elsewhere, this method will set the cookie path to the domain root or to a specific subdomain, depending on what you specify. It's best practice to set this for each domain when doing cross-domain tracking.
Adding Code to Each Link
Finally, every link that sends to a different domain must be tagged with code. You can do this automatically with our LinkTagger script, or you can do it manually.
To do it manually, add an onClick event (or equivalent) that executes the _link() method. You must pass this method the full URL that the link redirects to, and it must be followed by return false if it's in an anchor tag. This gathers the visitor's cookie information and appends it in a query string to the link URL; it then overrides the href value and redirects the visitor to the newly constructed URL.
For example,
becomes
Adding Code to Forms
If it's not a link or button, but a form that sends the visitor to a new domain, Google Analytics has a solution for that, too. It's the _linkByPost() method. This method works the same way as _link(), but it gets added to the form tag as an onSubmit event.
For example,
Adding Code to Frames
If you have an iframe that shows a page on another domain that you have code on, you will need to change the src attribute to include the visitor's cookie information using the _getLinkerUrl() method. This accepts a URL, just like _link() and it returns the URL with the addition of the query parameters.
Sometimes it might be necessary to replace the src attribute dynamically with the onLoad event instead.
Adding Code Dynamically
For a site with a lot of links, this can become a tedious process. Strictly speaking, it's possible to create a script on your site that would dynamically tag each link or form. This could be done server-side with some type of search and replace function, or it could be done client-side with JavaScript, for example, to dynamically add onclick events to each outbound link. Our LinkTagger script does this client-side.
Another possibility for the _link() method in an anchor tag is to pass it a dynamic parameter (eg. this.href) instead of manually adding each URL.
Troubleshooting Cross-Domain Tagging
After you've added all this code, expect to see some drop in the number of visits being reported. Incorrect implementation inflates visit counts. The other biggest indicators that you were successful are the traffic source reports.
Referrals
The obvious place to look first is the referral reports.
Change the date range to include only the time after the code was implemented. Then go to the Referring Sites report. Look for any of your own domains. If you see any, click on the top one. Then change the graph to "Compare to Site" and look at what percentage of overall traffic this constitutes.
It's not unusual to see a few self-referrals, even when the code is on the site properly. However, if it's more than a few percentage points, you may have missed some pages or links.
Direct Traffic
Direct traffic is really traffic that the tracking code couldn't identify a source for. Sometimes different browsers under different circumstances will track referrals as direct traffic. If the percentage of direct traffic seems fishy, check the landing pages for it. If those landing pages are unusual URLs, or if they are URLs you know are unlikely to be referenced by third-parties (eg. the beginning of a checkout process), you may have a problem.
Conversions
Another indicator that something is wrong with your cross-domain tracking is if all (or nearly all) of the transactions on your site are attributed to direct traffic and/or a referral from your own site. The missing code is most likely on one of the pages required during the conversion process.
Finding the Gaps
If there are indications that your cross-domain tracking is not set up completely, start by visiting the pages reported as referring sites. Search through the source code to make sure the _link() method is called consistently.
If the _link() method is everywhere it should be, click on a few of the links. When you reach the next page, look in the address bar at the URL. It should contain a long query string with several query parameters that start with __utm. If it doesn't, confirm that the tracking code on each page has been modified and that the _link() method is always followed by return false. Check for syntax errors in the JavaScript.
If all of your own code is set up correctly, then it's possible that the destination page on the other domain is stripping out the extra query parameters. If it's doing a redirect, that is almost certainly the case.
If you still need help, contact us.














Comments
Tracks only one site from the two
Hi!
I have 1 site and a blog that I want to track in a single profile in GA -
Site A - www.abc.info
Site B - www.def.com
I use the following codes -
Site A -
<script type="text/javascript">var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1234567-8']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '.abc.info']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowHash', false]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Site B -
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1234567-8']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '.def.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowHash', false]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
In Google Analytics, I have chosen custom tracking code -
I have saved the below code -
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1234567-8']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '.abc.info']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowHash', false]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Problem area -
Site A is being tracked but Site B is not tracked at all.
I am not sure why this should happen. For site B, should I change - _setDomainName', 'none'
Any help is appreciated. Many thanks in advance.
Hide URL Parameters
Is it possible for someone to hide all the new __utm parameters from the address bar when following a _link() method? If this is done via javascript does this mess up google analytics tracking?
Re: Hide URL Cross-Domain Parameters
The Google Analytics code looks in the URL for those query parameters to duplicate the cookies. If it sees the query parameters, the default action is overwritten, and the pageview is seen as a continuation of a current visit, and the new cookies are set with the values in the URL.
This process happens once the script is executed (usually upon page load). Once the cookies have been set, it doesn't matter if the values stay in the address bar.
Following on from my previous
Following on from my previous comment - yesterday we did try copying out the '.' as follows:
pageTracker._setDomainName("harrogateholidays.co.uk")
//pageTracker._setDomainName(".harrogateholidays.co.uk")
but that hasn't worked either. the tracking is now showing zero for yesterday. So at least i know that it isn't working at all.
cross-domain tracking using an external booking system
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 uploaded the linktagger.js according to your tutorial.
Our problem is that our analytics stats have 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. anyone can see the second domain by choosing some random options from the 'find a property' panel on the harrogateholidays site.
Still not working
I implemented your automatic link tagger script but I'm still getting incorrect reports in Google Analytics.
I'm running an ecommerce site with a few domains (not subdomains): site1.com, site2.com and site3.com. To maintain the same sessions, I've set up sessions sharing like at this tutorial which maintains the query string throughout the redirecting. When I hover a link to one of my other domains, I can see all the GA data added to the query string, and at the end of the redirecting it still has the GA data in the query string (in the address bar) - but I'm still showing roughly 4x the amount of visitors to my site; which wrong. (There is HEAPS of referring traffic from within all the domains)
Any ideas?
Here is my all my GA/LinkTagger code...which is right before the /body tag.
<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-XXXXXXX-1");
pageTracker._setDomainName('none');
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}</script>
<script type="text/javascript" src="/includes/linktagger.js><script%gt;
<script type="text/javascript">
var linkTagger = new _olt("ga.js"); linkTagger.setCrossDomains('site1.com|site2.com|site3.com');
linkTagger.tag()
</script>
I also setup a filter in Google Analytics according to this article. And that's not showing reports with hostnames like it is suppose to either.
Thanks!
Re: Still not working
I would make one change to the code you listed. Instead of calling
_setDomainName('none'), I would pass it the hostname of the site the code is on.Aside from that, as long as that tracking code appears on every domain and you are seeing the cookie data passed in the query string, the visit should be tracked seamlessly. You can double-check that it is by looking at the cookies after the domain switch to make sure they are identical to the information in the query string.
I would look for the specific pages creating self-referrals like this article explains and then troubleshoot the code on those pages.
CROSS DOMAINS COOKIE
I'm having a problem.
In the change of domains(http to https), the cookies are not being transferred.
The setting is right. The problem can be on my site? Where?
Thank you.
Cookies on a domain
Leo-
If the domain name is the same, but the protocol has changed from http to https, you shouldn't need to do anything. Cookies are only set to the hostname, regardless of the protocol.
If the domains are different and the protocols are different, the steps above still apply.
Hope that helps!
Really clear explanation
This was so much clearer than the Google help, and you included a troubleshooting section! I'm wishing I had found this blog earlier now.
+1
+1
Great article, it sums it all
Great article, it sums it all up.
No need to search the clumsy GA Help pages again with this bookmark!
_Link
one thing is still not clear to me
how to use _link for parent domain and its sub domains.
now i have domain1.com; sub1.domain1.com; sub2.domain1.com and domain2.de.
1 i should NOT place _link for links that go from domain1.com to sub1.domain1.com & sub2.domain1.com
but that i should place _link for links that go from sub1.domain1.com & sub2.domain1.com to domain1.com (their parent domain)
or
2 DO NOT need _link or _linkByPost on any links to and from sub-domains with the parent domain
which one is correct?
thanks
;)
_link() with subdomains
Tracking multiple subdomains is not done with _link() at all. This topic is discussed in a separate post: http://www.analyticsmarket.com/blog/subdomains-google-analytics
The _link() method is only used when the parent domain changes. So, going from domain1.com to domain2.com requires it. Going from sub1.domain.com to sub2.domain.com requires a different (and simpler) fix.