I'm looking for the best solution to track my website's traffic using Google Analytics. I have a website that (currently) has two subdomains. The subdomains are about very different content, the TLD shows some basic information about me and also shows excerpts from the two underlying subdomains. I'd like to be able to see information for the whole site, but also be able to specify per subdomain.

Obviously I'd like to use the more recent Asynchronous Google Analytics script.

I couldn't find any decent and more importantly, recent solutions or implementations for this. Any tips or pointers would be appreciated.

2 Answers 2


I use this to track subdomains. Put this code at the end of your template before the </body> tag.

 <script type="text/javascript">
//Tracking code customizations only
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'your analytics acct #']);
      _gaq.push(['_setDomainName', '.your_domain_here.com']);
      _gaq.push(['_setAllowHash', false]);

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

The you'll need to setup multiple filters in the analytics interface to see the data combined and separate.

EDIT to add filtering information :

Easy tutorial for setting up subdomain filters

  • This is the asynchronous solution. You can also split the script up by placing the top portion in the <head> and the bottom portion before </body>. If you do that include open and close script tags around both sets of code.
    – JMC
    Jun 7, 2011 at 13:58
  • JMC, could you expand a little on what the benefits of doing this would be and where to split?
    – WouterB
    Jun 7, 2011 at 14:10
  • JMC, I very much like your example code. Could you let me know how to set up these filters as well?
    – WouterB
    Jun 7, 2011 at 14:14
  • @WouterB - There's a performance benefit in splitting the code if there's a delay in loading the rendering portion of the script. By putting it after the rest of the page loads, you avoid the delay. You've probably seen bad implementations on webpages where the page doesn't render until the ga code finishes loading and you stare at page where only the background has loaded. Break the code up right before (function()
    – JMC
    Jun 7, 2011 at 14:27
  • @WouterB - For the filters you'll need to create a profile for each subdomain then apply a filter. I'll modify my answer with an article link that details the process clearly.
    – JMC
    Jun 7, 2011 at 14:41

I think this article describes what you want http://www.google.com/support/analytics/bin/answer.py?answer=55524. It does say it's for the old version of the script, but the same principles - using cross domain tracking, and filter to get the subdomain stats - should apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.