Analytics & Testing

Filter Subdomains in Google Analytics

With software as a service (SaaS) vendors like Compendium, you delegate a subdomain and host your blog on a different subdomain than your website. Commonly, this is accomplished with blog.domain.com and www.domain.com. Typically companies implement a totally separate account in Google Analytics to monitor the blog subdomain. It’s actually not necessary.

Google Analytics will allow you to monitor multiple subdomains within a single profile. To do this, you simply add a line of code to your current Google Analytics script:

New Google Analytics Script

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-XX']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

(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>

Old Google Analytics Script


<script type="text/javascript"> try {
var pageTracker = _gat._getTracker("UA-XXXXXX-XX");
pageTracker._setDomainName(".example.com");
pageTracker._trackPageview();
} catch(err) {}
</script>

You’re not done yet! If you simply do that, you run the issue of identical paths being measured under a single URl in Google. So – if you have index.php on your blog and www subdomains, they’ll both be measured as index.php. That’s not good. As a result, you have to do some fancy advanced filtering in the account!

Login to Google Analytics and click Edit on your Google profile. Scroll down the page where you can add a filter and add an advanced filter with the following settings:
ga.png

Now your profile should distinguish the subdomain throughout all the Analytics Account.

Download a Sponsored Marketing Whitepaper:
5 Ways Service-Based Experts & Leaders Create an Endless Source of Ideal Clients Without the Burnout and Hustle

5 Ways Service-Based Experts & Leaders Create an Endless Source of Ideal Clients Without the Burnout and Hustle

Are you tired of working many hours on your marketing, hustling nonstop, and getting lackluster results from all the work you're putting in? Download Now

13 Comments

  1. 1
  2. 2
  3. 3

    The “paste the code” section on Google Analytics now has two steps:

    1. What are you tracking?
    A single domain (default)
    Domain: marketingtechblog.com

    One domain with multiple subdomains
    Examples:
    http://www.marketingtechblog.com
    apps.marketingtechblog.com
    store.marketingtechblog.com

    Multiple top-level domains

    and then a checkbox for Adwords Tracking

    Here’s one for you: why is my safari browser for PC showing the no google features but doesn’t give me an option to check Updates (social site updates) and the like?

  4. 4
  5. 5
  6. 7
  7. 8

    Hi Doug,

    I added the above script but it doesn’t seem to be working. Any thing i slipped which you guys are aware of? 

    Will be very helpful if you can take me ahead on this. 

    Thanks and Regards,
    Nishanth T

    • 9

      A couple of things, @google-1f23c56cd05959c64c268d8e9c84162e:disqus . First (and most obvious) is to ensure that your UA code is properly set.  I hate to write that, but sometimes we copy and paste and forget.  Second… it will take many hours to actually catch up.  Give it a day and then see!

      • 10

        Hey @douglaskarr:disqus – Thanks a lot for the reply. Very much appreciated- the UA code is perfectly set. Checked it again too. I have been tracking it with this code for over a month now. The microsites/sub-domains do not show up in the GA. 

        Cheers…

  8. 11

    Thanks! Very helpful. I have the same code executing in different domains, depending on whether http or https is used (mostly to segregate cookies, because I also have a couple of different back-end packages & I want to avoid replay-style accounts), but the javascript changes were pretty minor.

  9. 12

    Hey there thanks for this tutorial it was very helpful! So once I add the code to all my sub domains are the stats that analytics shows going to include traffic from my subdimains?

  10. 13