Google Analytics: merge the traffic from the whole domain

A couple of days ago I got a small issue while working on a google analytics account, with the site tracked shown as a traffic source without no apparent reason.

 Google Analytics After some investigation, the problem has been identified. The web site was serving its contact module with an SSL protocol under a subdomain. The problem was neither the SSL certificate nor the subdomain; the webmaster was simply unaware that subdomains are treated as different entities into the web analytics platform unless they are instructed to do the opposite. The solution that follows was applied and, as I was not 100% sure, I asked the opinion to a friend of mine, Marco Cilia, that manages GA in 30 seconds (a Google Analytics Italian blog).

All you need is one line of code

A normal GA code block looks like this:

try {
	var pageTracker = _gat._getTracker("UA-XXXXXXX-1");
	pageTracker._trackPageview();
}
catch (err) {}

To change the behaviour, you need to add the following line of code in between the try {} block and for both the main and subdomain website:

pageTracker._setDomainName(".yourdomain.tld");

This will force GA to treat every subdomain as part of the same account, thus preventing any entry into the referrers’ list.

Also, the pages of the subdomain will be merged into the account so that you will have access to information like page length, bounce rate and so on. You need to make the changes in all subdomains that you want to track as a single entity. This is because the Analytics creates an encoded value of the domain specified by the parameter passed into _setDomainName(), and if the parameter is not identical across all domains, you will get conflicts and unexpected reporting behaviours.

It must be noticed that GA is a cookie-sbased system; this means that until the previous visitors’ cookie expires, you may continue to see your domain as a referrers entry. Based on my experience this should only last for between 7 and 14 days, but sometimes it takes a little longer.