Tag Archives: asynchronous code

Adding a Subdomain to Google Analytics for Tracking

Look at that – a real first post! As I was finishing configuring this new internet marketing blog, I realized that I set up a sub-domain: seomarketingblog.greenskydevelopment.com for it and that I want to make sure that the Google Analytics’ asynchronous code that I have on the pages tracks the sub-domain for reporting within Analytics. I then realized that this would make a great first post – and true to my word – it’s a sort of tutorial. I hope you find this helpful.

I have Google Analytics asynchronous code on my pages, and “out of the box” it looks like this:

<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-10861556-1’]);
_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>

This code tracks traffic for www.greenskydevelopment.com. Now I want to make sure that it tracks seomarketingblog.greenskydevelopment.com as well.

I need to add 3 more “_gaq.push(); statements to make it work:

_gaq.push([‘_setDomainName’, ‘.greenskydevelopment.com’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_setAllowHash’, false]);

So the end piece of code looks like this:

<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-10861556-1’]);
_gaq.push([‘_setDomainName’, ‘.greenskydevelopment.com’]);
_gaq.push([‘_setAllowLinker’, true]);
_gaq.push([‘_setAllowHash’, false]);
_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>

Dealing with tracking a sub-domain of your domain name with Google Analytics is fairly simple. Tracking cross domains (domain names not related to the base web site) or sub-directories on different domain names can get trickier. Luckily, Google has an excellent “how to” document for this.