Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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]);
      _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);
      })();</script>

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

share|improve this answer
    
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 '11 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 '11 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 '11 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 '11 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 '11 at 14:41
show 2 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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