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");
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:


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.


 Started by Garry  23 March 2010

So, should I continue to set up new analytics for new subdomains?

My employer's website has 2 or 3 new subdomains per week, it's a bit of a pain setting up individual analytics for each one.

If I can, I'd be inclined to just put the one piece of tracking code into the template for the micro-sites and let the rest take care of itself, would that work? would I still need to create a new analytics entry for the new subdomain, and edit the template?


 Started by Corey  07 February 2012

fyi, your example code is missing the period before _setDomainName() even though your instructions include it. :) Thanks for the tip, though. Solves a problem for me.



 Leave a Comment