Tracking Sub Domains with Google Analytics

November 19, 2007 by Justin Cutroni

Tracking websites that have multiple sub domains may not seem like a complicated thing. In fact, the GA support docs clearly outlines two ways to track sub domains. You can track all sub domains in a single profile or you can track sub domains in separate profiles.

But what if you want more flexibility? What if you want to track one group of sub domains in one profile and a different group of sub domains in another profile? Or what if you want to create new profiles for a different groups of sub domains in the future?

I like to use an approach that let’s you decide how you want to group sub domain traffic. This setup will give you the most flexibility now and in the future. Some of the benefits include:

1. It let’s you collect all data in a master profile. This facilitates ‘roll up’ reporting and gives you an easy way to measure traffic to the primary domain and all of the sub domains.

2. This technique lets you track each individual sub domain in it’s own profile which is useful for deeper analysis.

3. You can easily create new profiles in the future based on groups of sub domains.

Here’s the exact process to get this working.

1. Create Profile for The Primary Domain

The first step is to create a master profile. All other profiles for the website, including profiles for the various sub domains, will be based on this profile. When you enter the domain for the website use the primary website domain. In the example below, I’m using www.sitedomain.com as the website domain.

Google Analytics Master Profile Settings.

After creating the profile GA will display the tracking code for the site. Before you add it to your pages it must be modified. You need to force GA to use the primary website domain for the cookie domain. This modification will let you track each unique visitor across all sub domains. This in turn let’s us collect all site data in a single profile and do ‘roll up’ reporting.

Old urchin.js tracking code:


<script type="text/javascript">
_uacct = "UA-XXXXXXX-1";
_udn = "sitedomain.com";
urchinTracker();
</script>

New ga.js tracking code:


<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-1");
pageTracker._setDomainName("sitedomain.com");
pageTracker._initData();
pageTracker._trackPageview();
</script>

Use this profile as a way to measure overall traffic across all of your sub domains. To facilitate page analysis in this profile add a filter that concatenates the hostname to the Request URI, which is the piece of data in the Top Content report. This will help you differentiate traffic to pages with the same URI that may reside on different sub domains.

20071118-hostname-request.png

A handy report that you’ll want to use in this profile is the Visitors > Network Properties > Hostnames report. This report will show traffic to the primary domain and all of the sub domains. It’s an easy way to compare traffic to the various sub domains.

2. Create Profiles for All Sub Domains

Once you have created the master profile you can start creating profiles for each of the sub domains. Remember, each sub domain profile is based on the master profile, that we created in step 1. Here’s how to set up the new profile:

20071118-sub-profile.png

Now that you’ve created the profile, make sure you add the tracking code to the pages on the sub domain. The tracking code for the sub domain is the same as the tracking code that goes on the primary domain (it’s the code from step 1).

3. Add Filters to Sub Domain Profiles

The next step is to add a filter to the sub domain profile so it only contains data for the appropriate sub domain. This is done using an include filter based on the hostname. Once this filter is in place you’ll have the master profile which will contain data for all sub domains, and this profile that will contain data for a specific sub domain.

Filter to create a GA sub domain profile

To create additional profiles for other domains just repeat steps 2 and 3. Obviously the settings for the filter used in step 3 will depend on the sub domain that you want to include in the profile.

Creating Additional Profiles

Let’s say you need to create a profile for two sub domains: 1.website.com and 2.website.com. This can easily be done because the same tracking code is on both sites. All you need to do is add an include filter with the following settings:

GA Filter settings for 2 sub domains.

While Google’s documentation describes a good way to track sub domains, I think this approach is better due to it’s flexibility. Plus, because the same tracking code is used all all sub domains you don’t need to worry about placing the wrong tracking code on a sub domain.

Good luck with your setup!


Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Technorati
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Ma.gnolia
  • YahooMyWeb
  • co.mments
  • Reddit
Subscribe:
  1. 29 Responses to “Tracking Sub Domains with Google Analytics”

  2. Hello,

    I implemented GA on a big portal website. I did it the same way but I added some separate profile for each subdomains (add a profile for a new subdomains). It allows me seeing each subdomains as an independent website. Interesting at traffic source level.
    JS for a subdomain look like this:

    _uacct = “UA-xxxxx-15″;
    urchinTracker();
    _uff = 0;

    _uacct = “UA-xxxxx-1″;
    _udn=”domain.com”;
    urchinTracker();

    The problem is that you have to manage several tracking codes.
    Note the _udn=”domain.com”; that avoid creating virtual traffic on domain.com due to the tracking code of the specific subdomain.
    And _uff=0; that allows adding a second traffic code.

    But I think that there is a problem at entry and exit page level as the entry page will not be logged for the second tracking code and the exit pages will not be logged for the first tracking code.

    By Pierre on Nov 22, 2007

  3. Hi there, since our site has added _udn = “domain.com”, the referrers from our own domain (”domain.com” ) have disappeared, which is what I expected. However, our subdomain is still showing up as a referrer. Is it supposed to be like that??

    By Jenny on Nov 22, 2007

  4. It seems to be causing a javascript error (_gat is not defined)

    Any ideas?

    By Scott on Nov 23, 2007

  5. Hi Scott,

    Hmmmm… Although the new tracking code is still in Beta, I’m running it on a number of sites without any issues. I would check to insure that the GA code is not interfering with existing JS code.

    Justin

    By Justin on Nov 26, 2007

  6. Hi Justin,

    I searched all of the GA code and found no reference to _gat - am I missing a file? The website in question is http://www.casobi.com

    By Scott on Nov 28, 2007

  7. I wanna change the tracking code, but I am not sure if the first line

    is needed in the new code as well. Or do I have to change “urchin.js” in “ga.js”?

    By Roos on Nov 29, 2007

  8. Hi Scott,
    when I set up the profiles as you did it, then “sitedomain.com” is the profile with all the stats. How do I set up a profile only for the traffic from sitedomain.com? With hostname filter with “sitedomain\.com”?

    The next questions is, when I want to view the traffic from forum.sitedomain.com/test, I set up that filter “forum\.sitedomain\.com/test”

    By Roos on Nov 30, 2007

  9. Hi Jenny,

    It may be that the traffic from the sub domain is legacy traffic, people that still have that value stored in their cookies.

    Justin

    By Justin Cutroni on Dec 2, 2007

  10. Hi Scott,

    The new code is still in beta, it could be that it is not working with some code on your site. I have seen some minor issues with certain sites using array prototypes, but no widespread issues. I would check the interaction with the code on your site.

    There is no info on _gat because this stuff is really new. There’s little to no info out there.

    Hope that helps,

    Justin

    By Justin Cutroni on Dec 2, 2007

  11. Hi Roos,

    I would hold off on changing your code if you don’t understand the instructions. It’s still in beta so there is very little support available if things don’t work out.

    Sorry!

    Justin

    By Justin Cutroni on Dec 3, 2007

  12. Hi Justin,

    if the site is already running and the main domain is already coded, do we have to include the _udn in it as well? Or adding _udn to the subdomains is enough?

    Thank you.

    By Daniel Waisberg on Dec 4, 2007

  13. Hi Justin,
    I have been experiencing problems with the advanced profile you described earlier. After implementing the filter, the url’s in my content reports are no longer functional. I believe by telling GA to place the hostname before the request uri, the content line item appears as - /www.domain.com/domain.com/widgets.com. If the client were to want to view this link, it would return a 404 page not found, since the domain name duplicates itself.
    Any insight to this??

    By jonathan on Dec 4, 2007

  14. Jonathan,

    Yes, you are correct. The advanced filter will break the ‘click-through’ capability for that profile. Unfortunately the only solution is to remove the filter. However this could make the data more difficult to understand. You’ll have to choose which feature is more important.

    Sorry about the confusion and I hope that helps. Thanks for reading.

    Justin

    By Justin Cutroni on Dec 4, 2007

  15. Hello,
    I’m now using the original code, but I have some problems.

    I’m using GA for testing at moc.ingame.de, doom3.ingame.de and forum.ingame.de/sims.

    The global profile is ingame.de. There I have all the traffic from those three “sites” like I want it. Also the profiles for “moc.ingame.de”, “doom3.ingame.de” and “moc.ingame.de, doom3.ingame.de” are working well.

    In my global profile I’m getting the data from forum.ingame.de/sims, but in the seperate profile nothing is to see. I’ve set up this filter for the seperate profile:

    Custom filter | include | Hostname | forum\.ingame\.de/sims | no

    Can you tell me where the mistake is?

    By Roos on Dec 12, 2007

  16. Roos,

    It sounds like you need to set the _udn variable so you have a unified cookie across all the sub domains. The way you have it set up now your main profile will have incorrect data for unique visitors. You want one cookie for each visitor and you want that cookie to be used by each sub domain. That’s where the _udn variable comes in.

    Once you have a unified cookie across all the sub domains you should be able to see the appropriate hosts as I describe in the post.

    Hope that helps,

    Justin

    Hope that helps.

    By Justin Cutroni on Dec 18, 2007

  17. Hello,

    I was getting errors from the Firebud FF extension as well. It complained, saying that “_gat is not defined” until I added the following type check:

    if (typeof(_gat) == ‘object’)
    {
    var pageTracker = _gat._getTracker(”UA-xxxxxxx-x”);
    pageTracker._initData();
    pageTracker._trackPageview();
    }

    By Jorge Garcia de Bustos on Dec 19, 2007

  18. Scott,

    Most likely the error you’re getting is due to a content blocker - adblock plus, perhaps? - in your browser. I had the same issue, turned off adblock (for my site, at least), and the problem went away.

    By Jim Galley on Dec 19, 2007

  19. Hi Justin,
    would it solve my problme if I use the new ga.js tracking code with the followinh code line?

    “pageTracker._setDomainName(”sitedomain.com”);”

    Or should I add this line

    “_udn = “sitedomain.com”;”

    to the old tracking code?

    Sincerly Fabian

    By Roos on Dec 19, 2007

  20. Fabian,

    You should use the code that is appropriate the version of the GATC that you are using. Either way, setting the domain name to sitedomain.com is the first step to fixing the problem. Once you update the code you’ll need to create the appropriate profile filters to segment the data.

    HTH,

    Justin

    By Justin Cutroni on Dec 19, 2007

  21. I have created a profile in order to give an advertiser access to my stats. However I don’t want to grant access to *all* the stats. is it possible to control what reports are available to a profile?

    By Steve Weller on Dec 31, 2007

  22. Scott,Jim: _gat blocked problem solved.
    Gat object is not defined, because google analytics javascript is not loaded. Try putting http://www.google-analytics.com/ga.js in your browser address bar. If you don’t get javascript code, that’s it. I.e. my free wifi provider blocks whole analytics domain, so I get “content blocked” page instead of javascript code.

    By petr on Jan 4, 2008

  23. Hi Steve,

    Unfortunately, no. There are only two levels of access to GA: user and administrator. If you give someone user access they can see all of the reports in a profile.

    Thanks for reading,

    Justin

    By Justin Cutroni on Jan 13, 2008

  24. So to be clear on this whole thing…

    If I use the above implementation I should be able to get visitor stats (#unique visitors and their demographics, etc) for each subdomain PLUS a grand tally when looking under the primary domain?

    Thanks
    Chuck

    By Chuck on Jan 15, 2008

  25. Hey Chuck,

    You’re right on the money. Thanks for the comment and thanks for reading.

    Justin

    By Justin Cutroni on Jan 20, 2008

  26. Thanks for the tutorial. The GA doc is really lacking. Yes, they do give you instructions, but absolutely no examples or context in which to apply the instructions. The reasons this is so important is it takes time to test these permutations on GA since the reporting isn’t in realtime. You’ve saved me some work. Thanks!

    PS seems to work well. I haven’t updated to the new tracker yet.

    By Tim on Feb 5, 2008

  1. 4 Trackback(s)

  2. Nov 25, 2007: Bloglinks der Woche (KW 47) | Webanalyse & SEO - News
  3. Nov 30, 2007: Analytics Talk - Tracking Sub Domains with GA
  4. Mar 1, 2008: Tracking SubDomains within Google Analytics « The Analytics Guru
  5. Mar 24, 2008: Google Analytics 101: Some Basic Setup Tips | .eduGuru

Post a Comment