Analytics Blog
Cross Domain Tracking in Google Analytics
Cross Domain Tracking Overview
UPDATE: The recommended way that cross-domain tracking is performed has changed since this post was originally written. Google Analytics code no longer enforces the domain hash, so you no longer have to use _gaq.push([‘_setAllowHash’, false]);
Cross domain tracking is required anytime you want to track a single GA session between multiple domains that you control.
A common example is a 3rd party checkout system that resides on a different domain. For the purpose of keeping our examples consistent throughout this blog post, let’s fictitiously assume that your scenario involves your primary domain (www.primarydomain.com) and a 3rd party checkout provider (www.3rdpartycheckout.com).
Additionally, all code referenced in this document is using the asynchronous syntax. It is important to note that if you have not yet migrated over to the asynchronous format that the example code provided will not work as is.
Impact of Not Using Cross Domain Tracking
If you don’t use cross-domain tracking or you have it improperly configured, you’ll end up with meaningless data that shows self-referrers in the visit reports and a lack of proper attribution towards your conversion points. As a visitor moves from your primary domain to the other, they will start a brand new session in Google Analytics.
To understand why this happens, you must first understand how Google Analytics works. Google Analytics uses first-party cookies that are attached to the visitor’s browser. Those cookies contain data about when the visitor last visited the site, what page it was, and a variety of other data. When the user clicks between pages or comes back at a later date, the ga.js javascript looks for the existence of that first party cookie. If it doesn’t find a cookie, then it views that visitor as a brand new visitor (that has NEVER been to your site). First party cookies are great (the best type), but for security reasons, they do not transfer between domains. The first party cookie is linked directly to the domain that set it and will not be accessible by any other domain.
If the visitor views a few pages on www.primarydomain.com and then clicks a link or submits a form that takes them to www.3rdpartycheckout.com, the ga.js script looks for a first party cookie tied to www.3rdpartycheckout.com. Without having cross-domain tracking properly configured, it will not find a cookie and will start a brand new GA visit as a new visitor. Now, of course, if you’ve had this problem for a while, you’ll see returning visitors because they’ve visited the www.3rdpartycheckout.com in the past.
As you can see, this is a HUGE problem. If a visitor clicks an ad or performs and organic search and ends up viewing a page on the www.3rdpartycheckout.com after viewing pages on www.primarydomain.com, you lose ALL data about how that user arrived and the complete picture of what they did. If they end up converting, you will only know that they came from www.primarydomain.com (which is not helpful at all).
Solution for Cross Domain Tracking
To properly track a visitor as they move from www.primarydomain.com to www.3rdpartycheckout.com, you will need to make code modifications to the code on both sites.
Steps & Code for www.primarydomain.com:
- On all pages of the site, set _setAllowLinker to true via _gaq.push([‘_setAllowLinker’, true]);
This enables domain linking and allows the use of _link() and _linkByPost(). - On all pages of the site, turn off domain hashing via _gaq.push([‘_setAllowHash’, false]);
Domain hashing needs to be turned off to allow the cookie to be properly read by the www.3rdpartycheckout.com ga.js script. - On all pages of the site, set the current domain name via _gaq.push([‘_setDomainName’, ‘.primarydomain.com’]);
Note that there is a leading ‘.’ - Send the current GA cookies over to the domain via parameters in the link. Google makes this quite easy by using either _link() or _linkByPost() functions in your code. Both will transfer the cookies from one domain to the querystring of the resulting link. The ga.js script on the other domain landing page will detect the parameters in the URL and assemble a new GA cookie that references the same browsing session.
Here’s the full tracking code snippet along with an example link and form that pushes the GA cookies over to the other domain:
[code lang=”js”]<script type=”text/javascript”>var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-9999999-9’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_setDomainName’, ‘.primarydomain.com’]);
_gaq.push([‘_setAllowHash’, false]);
_gaq.push([‘_trackPageview’]);
(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>
…
<a href=”http://www.3rdpartycheckout.com/checkout.html” onclick=”_gaq.push([‘_link’, this.href]);return false;”>Checkout Now!</a>
…
<form name=”formName” method=”post” onsubmit=”_gaq.push([‘_linkByPost’, this]);”>
</form>
[/code]
Steps & Code for www.3rdpartycheckout.com:
- On all pages of the site, set _setAllowLinker to true via _gaq.push([‘_setAllowLinker’, true]);
- On all pages of the site, set _setDomainName to ‘none’ via _gaq.push([‘_setDomainName’, ‘none’]);
Note: When you use none as the domain name, it automatically turns off domain hashing. - Use _link() and _linkByPost() to send GA cookies back to the www.primarydomain.com if links exist to bring the user back. This is especially important if the initial landing page can be on the www.3rdpartycheckout.com domain.
Here’s the full tracking code snippet along with an example link and form that pushes the GA cookies over to the other domain:
[code lang=”js”]<script type=”text/javascript”>var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-9999999-9’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_setDomainName’, ‘none’]);
_gaq.push([‘_trackPageview’]);
(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>
…
<a href=”http://www.primarydomain.com/products/” onclick=”_gaq.push([‘_link’, this.href]);return false;”>Continue Shopping</a>
…
<form name=”formName” method=”post” onsubmit=”_gaq.push([‘_linkByPost’, this]);”>
</form>
Conclusion
If properly setup, cross domain tracking will maintain the proper referral and other session data for visitors on your site. You will begin to accurately track bounce rates, conversion sources, and other metrics.
The example code we’ve provided satisfies the most common cross-domain setup. There are specific circumstances and many variables that warrant different code and strategy.
Whether you need Google Analytics Help to check to ensure that the code you have in place is working correctly or you need help setting up tracking in another situation, we can help. Give us a call at 1(888)252-7866.