Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Digital Analytics

Cross Domain Tracking in Google Analytics

February 1, 2011

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.

Note: If you can get your provider to place their system as an alias onto a subdomain of your regular domain, this simplifies the setup process immensely. Also, users won’t look at the URL in their browser and wonder what site they are on (especially in cases where you can’t control the look & feel).

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:

  1. 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().
  2. 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.
  3. On all pages of the site, set the current domain name via _gaq.push([‘_setDomainName’, ‘.primarydomain.com’]);
    Note that there is a leading ‘.’
  4. 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:

  1. On all pages of the site, set _setAllowLinker to true via _gaq.push([‘_setAllowLinker’, true]);
  2. 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.
  3. 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>

[/code]

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.

Joe Christopher
About the Author

As Vice President of Analytics at Blast Analytics, Joe leads a team of talented analytics consultants responsible for helping clients understand and take action on their vast amounts of data, to continuously improve and EVOLVE their organizations. With over 20 years of experience in analytics and digital marketing, Joe offers a high-level of knowledge and guidance to clients across all industries. He is an expert in all major analytics platforms including Google Analytics and Adobe Analytics, as well as various tag management systems such as Tealium and Adobe Launch. He also consults on data visualization, data governance, and data quality strategies. Having extensive expertise in many areas, has enabled Joe to become a well known thought leader and speak at industry events such as Tealium’s Digital Velocity series. Joe remains on the pulse of various information technology, programming languages, tools and services, keeping Blast and its clients on the leading edge.

Connect with Joe on LinkedIn. Joe Christopher has written on the Blast Digital Customer Experience and Analytics Blog.