Tracking Between Subdomains

Tracking between subdomains doesn’t have to be complicated. You don’t need to check that box in the Google Analytics admin section for cross-domain or cross-subdomain tracking to work. All it does is edit the snippet in the copy/paste box:
Tracking Between Subdomains

All that does is add a line of code in the “Tracking Code” section. It should look something like this:

 

<script type=”text/javascript”>// <![CDATA[
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-#######-#’]);
_gaq.push([‘_setDomainName’, ‘mydomain.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 problem is that it doesn’t necessarily cover every case. If you have a domain that has multiple levels of subdomains – like sub1.sub2.mydomain.com – it will not work. To solve for this, you will want to add an additional period before your domain:

<script type=”text/javascript”>// <![CDATA[
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-#######-#’]);
_gaq.push([‘_setDomainName’, ‘.mydomain.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>

My site is jimalytics.com – my GA code sets the _setDomainName parameter as .jimalytics.com. I’m not using 2 levels of subdomains, but you never know when something comes up and you might suddenly need to provision for it. It’s better to be safe than sorry.

Keep in mind – this code does NOT change between subdomains either. You will use the same code snippet on your top level domain as you do on your subdomains. Don’t make it complicated!

Leave a Comment