Revised Sub-Domain Tracking & Cross-Domain Tracking Codes in Google Analytics

Google seems to have finally updated its documentation for sub-domain & cross-domain tracking. The major changes have been detailed below:



_setAllowHash()

The _setAllowHash() object method has been deprecated. This method reduces the domain hash value to an arbitrary number (usually ‘1’) so that cookies from other tracked domains do not get rejected. This method was never required for sub-domain tracking to work; but Google’s earlier documentation had, quite redundantly, included this for tracking sub-domains.



The revised sub-domain tracking code

<script>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_setDomainName', '.domain-name.com']);
_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>

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXX-1']);

_gaq.push(['_setDomainName', '.domain-name.com']);

_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>



The revised cross domain tracking code

<script type=”text/javascript”>

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXX-1']);

_gaq.push(['_setDomainName', '.domain-name.com']);

_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>


Of course you still need to tag your links/forms directing users from one tracked domain to the other (say domain-name-1.com & domain-name-2.com) through onclick & onsubmit functions. The tagging has been explained below,

Tagging for link from doman-name-1.com to domain-name-2.com:


<a href=”http://www.domain-name-2.com/test.html”

onclick=”_gaq.push(['_link', this.href]); return false;”>link to domain-name-2</a>


Similarly, links from domain-name-2.com to domain-name-1.com will also need to be tagged.


For forms directing users from domain-name-1 to domain-name-2 or vice versa, the following onsubmit function needs to be appended,


<form … onsubmit=”_gaq.push(['_linkByPost', this]);”>


Wait there is more …

The good folks at Google have also added a note on appending the hostname to the URIs seen in the GA dashboard. You can read all about it here .


Honestly this update should have come sooner. It clears up a lot of ambiguity surrounding the sub-domain & cross-domain tracking codes in Google Analytics. Guess it’s better late than never.


Note: We did manage to briefly test the code without the setAllowHash() function. It works perfectly. However, removing this method could reset the _utma cookie for returning visits & therefore skew the returning visitors metric.


Comments are closed.