Behind the Smoke and Mirrors of Cross-Domain Tracking

Throughout my several adventures through the land of Google Analytics Implementations, I've found that cross-domain tracking seems to be one of the largest sources of confusion either while auditing past implementations or reviewing the design for a new one.  While some other tools tend to handle cross-domain tracking through various methods, GA is fairly unique in that it requires the user to make sure it's set up properly.  I'd like to take a moment to go through the 4W1H of cross-domain tracking and remove some of the mysticism around the topic.
 
What is cross-domain tracking?
Put simply, cross-domain tracking is the process of following a visitor as he navigates from one domain to another.  It is the key to stitching the visitor's visit together across two or more domains so the web analyst for the site's company can perform accurate analysis.  I'll get into more detail in the following sections.
 
Why should I care?
 
Let's use a hypothetical example to illustrate the importance of cross-domain tracking.  Company ABC has one website, abc.com.  They sell merchandise on this website, but they use a third-party check-out tool  that is hosted on xyz.com, and the confirmation page with the e-commerce tracking is all located on xyz.com.
 
 
Without any cross-domain tracking, all revenue and transactions will be attributed to a referring site:  abc.com.  Nothing that happens on abc.com, nor anything that leads users to abc.com (and, potentially, a purchase) will be attributed any revenue from any transaction that occurs for Company ABC.  No ROI can be calculated on any campaigns, from paid search to e-mail to banner ads. 
 
 
In general, cross-domain tracking is necessary so the web analytics tool will not treat visits to separate domains as separate visits, ruining all attribution and some segmentation/reporting in the process.  In my opinion, that makes it a pretty important little beastie.
 
When do I need to implement it?
 
 
As illustrated in the "Why", the most important case where cross-domain tracking is paramount is when you have a third-party check-out tool that hosts the e-commerce tracking on a separate domain.  Any instance where important tracking for a single visit occurs on a different domain than the main site is a good candidate for cross-domain tracking.  If a user can easily navigate through multiple different web properties hosted on separate domains, odds are your website will benefit from cross-domain tracking.
 
 
In some instances, you may manage multiple disjoint websites that do not apply at all to one another.  In this case, if a user cannot click on a link leading them from one domain to another, you do not need to worry.  I'll explain in more detail within the "How" section.
 
Where does it all get implemented?
 
 
Cross-domain tracking within Google Analytics requires two components to function properly:  a couple lines within the base tag on each domain that needs to track visitors from other domains, and some mechanism to modify all of the HREF attributes on the relevant anchor tags on the page.
 
How does it all work, anyway?
 
 
I'm going to present the "How" as a tale of two parts:
 
 
♦ For the Business Folks (or Anyone Uninterested in the Gory Technical Details)
In the case of Google Analytics, the JavaScript tag sets a unique cookie value for each visitor to your website.  The analysis engine uses these values to tie page tag log file hits together to form visits.  However, browser cookies are set at the domain level, and JavaScript can only set first-party cookies.  In other words, the GA tag can only set its cookie to the domain of the page it is executing on, and any new domain will set a completely unique cookie value, treating that visitor as unique.
 
The cross-domain tracking in Google Analytics passes these cookie values through links to the other domains, attaching them to the query string.  The JavaScript tag is designed to look in the query string for cookie values, and if they exist, use them.  That way, a single user will have identical cookie values on two separate domains, and the analysis engine will be able to tie all hits from both domains, successfully tracking said visitor across domains.
 
♦ For the Technical Folks (or Anyone Interested in the Gory Technical Details)
Before I start in on my preferred technique, I want to note that Google's most recent documentation notes that the _setAllowHash function is no longer necessary.  I have personally not yet confirmed how exactly this behaves, so I still use the deprecated function.  I have a few tests in progress that I plan on following up with, so I may update this post with a comment on this particular subject.
 
 
The first step in setting up cross-domain tracking for GA involves a few additional function calls within the base tag, seen below:

The italicized text would be configured for your particular site.  The red functions act as follows:

• _setDomainName - Configures the domain field for the GA cookies.  Setting it to the main domain without sub-domains automatically configures tracking across sub-domains as well.  Please note that if tracking across particular sub-domains of the same domain is not desired, this value will need to include the sub-domain specifics.
• _setAllowLinker - Enables the linking functionality used to append the GA cookie values onto the query string of a link's HREF.
• _setAllowHash - Disables the hashing functionality that generates a custom value at the beginning of the cookies based on the domain the cookie is set to via _setDomainName.
 

These functions will need to be set on all domains where cross-domain tracking is desired, with the _setDomainName value properly reflecting each site.
 
With the base tag all set up, the last piece of the puzzle is to simply modify every link on your site that leads to another site that needs to be cross-domain tracked.  One way to do this is to manually set an onclick event on the link that modifies the HREF attribute:
 

Typically, the volume of links to modify puts people off from any manual options, and for good reason.  Instead of going this route, I like to provide a custom JS file that is included on each page with the base tag.  This JS loops through all of the links on a page, checking the domain in the HREF and modifying the URLs.  However, the _link function above performs a redirection, so a separate modification technique is required.  An example can be seen below:
 

The _getLinkerUrl function on the synchronous tracker object simply returns a modified string.  Used in conjunction with a loop and some DOM know-how, the automated tracking can dynamically take care of all the page's cross-domain tracking needs without any manual modifications to current or future links.
 

Check out: CROSS DOMAIN TRACKING PART DEUX: THE TRACKONING

 

 

By Adam McArdell
About the Author:

Adam McArdell is a Senior Technical Consultant at Stratigent.

Contact Us Now