Google Analytics: merge the traffic from the whole domain

A couple of days ago I get in a small issue working on a google analytics account and the referrers showed into the traffic sources. GA was showing the domain monitored as a traffic source without no apparent reason.

Google AnalyticsAfter a bit of investigation, the problem has been identified. The web site was serving its contact module with an SSL protocol under a subdomain.
The problem wasn’t the SSL Certiicate nor the subdomain; the webmaster was simply unaware that subdomains are treatead as different entities into the web analytics platform unless they are instructed to do the opposite.

The solution that will follow is extremely simple, but to being completely sure I asked the opinion to a friend of mine, Marco Cilia that manage GA in 30 seconds (a GA Italian blog).

The solution you need for case like this is very simple: all you need is one line of code into the GA tracking script for all your pages for both the main and subdomain website.

A normal GA code looks like this

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

that you must change into

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

Basically introducing the following line pageTracker._setDomainName(“.yourdomain.tld”);

This will make every sub domain (www, subdomains) part of the same account, thus preventing any entry into the referrers’ list. In addition the pages of the subdomain will be merged into the account, so you will have access to information like page length, bounce rate and so on.

You need to make the changes in all domain / sub-domains 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 behaviors.

It must be noticed that GA is a cookie based system; this mean 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.

Technorati Tags: google analytics, domains, tracking

4 thoughts on “Google Analytics: merge the traffic from the whole domain”

  1. 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?

    1. Hi Garry,

      and thanks for your comment.

      This is a typical case in which you can use this technique for merging subdomain traffic. However, take on mind that if you will have duplicate file name between subdomains, your traffic won’t be properly identified until you dont differentiate the source.

      You can solve this attaching the hostname to the Request URI with an advanced filter:
      Field A: Hostname -> Extract A: (.*)
      Field B: Request URI -> Extract B: (.*)
      Output To: Request URI -> Constructor: $A1$B1

      Hope this helps.
      Andrea

  2. 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.

    pageTracker._setDomainName(“.yourdomain.tld”);

Comments are closed.