Google Analytics Cross Domain OR Multiple Domain Tracking – Automatic

During last several years if we have found one big problem that 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 same purpose and have several subdomains catering specific needs. Often the solution is to get the cookies value passed via URL. At times such problems can be solved by structuring multiple trackers , but not always. This blogpost aims to provide solution to automate the cookie value passing via form or link using Jquery.

Why we need cross domain tracking?

Google Analytics cross domain tracking

Take a look at above image, which represents a case when visitor lands on page of xyz.com and he navigates to subdomain.xyz.com or abc.com and/or submits information on form which is passed to abc.com. Here if Google Analytics cross domain tracking is not enabled and all the websites are tracked under same UA id of Google Analytics. One would not be able to visualize the data and would have duplication of data for a same visitor across all the domains.

What is required to enable cross domain tracking?

  1. We require all domain, subdomains and 3rd party domains tracked under 1 UA ID
  2. Use _setDomainName “none” – to enable cookie information to be passed across all domains, subdomains and 3rd party domains.
  3. Use _setAllowLinker “true” before call of _trackPageView
  4. We require all links and forms which are interconnected to main domain/sub-domain or 3rd party domain appended with _link() or _linkByPost() function

Above steps would enable you to setup your Google Analytics for Multiple domain’s & Across different subdomain’s tracking under one UA id (UA-XXXXX-XX)

Now, one would need to update all links with _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 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’s the 3 simple steps that you’ll need to do.

  1. Include Jquery javascript library reference
  2. Include default Google Analytics asynchronous tracking code with _setDomainName “none” and _setAllowLinker “true”
  3. Include the crossdomaintracking.js  - Download the crossdomaintracking.js

Your code will look like this.

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']); //set your account profile id
_gaq.push(['_setDomainName', 'none']); //set domain name none
_gaq.push(['_setAllowLinker', true]);
_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>

<script type="text/javascript" src="crossdomaintracking.js" ></script>

What will crossdomaintracking.js will do?

It will automatically identify all external multiple domains from a given page i.e. if you are on page of 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

http://subdomain.xyz.com/index.html?__utmb=….&__utmv=….&_utmk=88886746&__utma=1.789654.321654987…&utmz=..

Requirement

A Jquery Framework (If you are already using a jquery library you don’t need copy jquery library reference)
Script needs to be pasted through all pages of your website, where you want to track external links and mail to links.

Note:
When you use it with multiple javascript frameworks necessary safeguard needs to be added for the code. If you are using javascript functions to redirect to external links it wouldn’t work.

Feel free to write your feedback if you have any about it and let us know if we can improve it for you !

Google Analytics Cross Domain OR Multiple Domain Tracking - Automatic by
Suketu Parikh

Suketu Parikh

Suketu Parikh working with Tatvic as Tech Lead., has more than 5 years of experience as developer. Google Plus Profile: Suketu Parikh

More Posts

Follow Me:
Twitter

  • Test

    Hey really good article.

    • http://www.tatvic.com Suketu Parikh

      Thanks.

  • Pingback: Google Analytics Cross Domain Tracking | SFWEBDESIGN.com

  • http://twitter.com/AnalyticsNinja Yehoshua Coren

    _setDomainName(none) does not need to be set for cross domain tracking. It is best to leave that out.

    • http://www.tatvic.com Suketu Parikh

      Hi Yehoshua,

      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.

  • Gues

    Does this need to go on the receiving domain or sending domain?

    • http://www.tatvic.com Suketu Parikh

      Hi Gues,
      Yes code should be loaded on sending and receiving domains. Thanks.

    • http://www.tatvic.com Suketu Parikh

      Yes this will go on both receiving and sending domains.

  • http://www.macjunky.nl/ Kianoush Kamgar (MacJunky)

    Exactly the info I was looking for, thanks!

    • http://www.tatvic.com Suketu Parikh

      You’re welcome.

  • Maxime

    Is this JavaScript would still work if we are using Multiple Trackers?

    • http://www.tatvic.com Suketu Parikh

      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.