During the last several years if we have found one big problem that a lot of users face is Google Analytics Cross domain tracking OR Google Analytics Multiple Domain Tracking & how to handle pushing cookies to different domains or sub-domains. The reason for this is large corporates would have many websites for the same purpose and have several subdomains catering to specific needs. Often the solution is to get the cookie’s value passed via URL. At times such problems can be solved by structuring multiple trackers, but not always. This blog post aims to provide a solution to automate the cookie value passing via form or link using Jquery.
Why do we need cross-domain tracking?
Take a look at the above image, which represents a case when a visitor lands on the page xyz.com and he navigates to subdomain.xyz.com or abc.com and/or submits information on the form which is passed to abc.com. Here, Google Analytics cross-domain tracking is not enabled and all the websites are tracked under the same UA id of Google Analytics. One would not be able to visualize the data and would have duplication of data for the same visitor across all the domains.
What is required to enable cross-domain tracking?
- We require all domains, subdomains, and 3rd party domains tracked under 1 UA ID
- Use _setDomainName “none” – to enable cookie information to be passed across all domains, subdomains, and 3rd party domains.
- Use _setAllowLinker “true” before the call of _trackPageView
- We require all links and forms which are interconnected to the main domain/sub-domain or 3rd party domain appended with _link() or _linkByPost() function
Above steps would enable you to set up your Google Analytics for Multiple domains & Across different subdomains tracking under one UA id (UA-XXXXX-XX)
Now, one would need to update all links with the _link() function and all forms with _linkByPost() which are passing information to other domains. It will be a supremely time-consuming job to implement and verify through all domains. A quick easy solution is automated appending cookie values to all external domain links and forms.
Automated cross-domain/Multiple domain tracking for Google Analytics
Now, we have come up with an automated process to enable passing cookie information to all such links and forms which are external domains i.e. sub-domains or 3rd party domains.
Here are the 3 simple steps that you’ll need to do.
- Include default Google Analytics asynchronous tracking code with _setDomainName “none” and _setAllowLinker “true”
- Include the crossdomaintracking.js – Download the crossdomaintracking.js
Your code will look like this.
What will crossdomaintracking.js do?
It will automatically identify all external multiple domains from a given page i.e. if you are on the page xyz.com and links of subdomain.xyz.com and abc.com would be appended with _getLinkerURL function of ga.js and it will append cookie information such as
A Jquery Framework (If you are already using a jquery library you don’t need to copy the jquery library reference)
The script needs to be pasted through all pages of your website, where you want to track external links and mail to links.
Feel free to write your feedback if you have any about it and let us know if we can improve it for you!
Hey really good article.
_setDomainName(none) does not need to be set for cross domain tracking. It is best to leave that out.
Thanks for your comment. The context of using _setDomainName(‘none’) was to enable tracking among multiple top level domains, their subdomains and additional 3rd party domains in a unified way. Hence we tried to make the code simpler by using the _setDomainName(‘none’) across all situations and then automated the cookie passing information (for links/forms) via js.
Does this need to go on the receiving domain or sending domain?
Yes code should be loaded on sending and receiving domains. Thanks.
Yes this will go on both receiving and sending domains.
Exactly the info I was looking for, thanks!
Yes it would work, however this script will do cross domain tracking data to default tracker object. You may need to modify script to support cross-domain tracking for all tracker objects.