Tracking Subdomains in Google Analytics

By April 27, 2012 Analytics 2 Comments

 

Although information similar to this is available in the Google Help Centre – in my opinion the help is not that great because it fractures the two steps in tracking subdomains in Google – the code change and the setting up of a filter. Even now, with the new help centre at Google Developers they don’t give you all the advice you need in one place.

 

So – here we will outline the very easy steps to identifying subdomain traffic in your analytics account.

 

You might notice that in the normal Google Analytics set up, your content reports show your pages from the trailing / after the domain.

 

So, pageviews of

moomumedia.com/services

will show up as

/services

 

This is no good if we have subdomains. For example, say I had an English subdomain and a Chinese subdomain, and both had the same subfolder

 

cn.moomumedia.com/services

en.moomumedia.com/services

 

Google Analytics would put that traffic for both these pages together under

/services.

 

To be able to separate them out you need to take 2 steps

1. Change your site-wide analytics code snippet

2. Set up a filter

 

Change to Analytics Code

If you are using the new asynchronous code (not so new anymore), then you need to insert the part in red

 

<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXX-1′]);
_gaq.push([‘_setDomainName’, ‘.moomumedia.com.au’]);
_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);
})();

 

If you are using the old code

 

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-XXXXXXX-X”);
pageTracker._setDomainName(“.moomumedia.com.au”);
pageTracker._trackPageview();
}catch(err){}
</script>

 

 

Create a Filter

 

Then what you need to do is create the filter. I recommend creating a new profile  (and then using it in all subsequent profiles you create), just because I like to create new profiles when ever I use a new filter.

 

The new filter should look like this;

 

Filter Type: Custom filter -> Advanced
Field A -> Extract A: Hostname -> (.*)
Field B -> Extract B: Request URI -> (.*)
Output To -> Constructor: Request URI    /$A1$B1
Field A Required: Yes
Field B Required: No
Override Output Field: Yes
Case Sensitive: No

 

 

This will mean the URLs will now show up in Google Analytics as the complete URLs ;

en.moomumedia.com/services

cn.moomumedia.com/services

 

If you wanted to isolate the traffic to each subdomain, and make them show in separate profiles, you should make multiple profiles using the above filter and then you can use custom filters on each one of those profiles, to include or exclude the subdomain traffic as you need;

 

Filter Type: Custom filter -> Include
FIlter Field: Hostname
Filter  Pattern:  ^en\.moomumedia\.com.au$
Case Sensitive: No

  • http://www.candide.ca Candide

    Thanks … I was looking for a simple walkthrough about this, to separate subdomain (English language) of my e-commerce website.

    Do the filters work also on older data?

    • admin

      Hey – Google Analytics filters actually don’t work retrospectively – so it is important to get them working as soon as possible.

One New Idea To Help Achieve Your Sales Goals REQUEST YOUR FREE SPARK