Tracking Sub Domains with Google Analytics

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 gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
   document.write("<script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'>" + "</sc" + "ript>");
</script>

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

Like this post? Share it:


And how about checking out these related posts:

  1. Google Analytics Configuration Mistake #3: Third Party Domains
  2. How Google Analytics Tracks Third-Party Domains
  3. Tracking Clicks with GA Pt. 3: Advanced Implementation
  4. Tracking Offline Advertising With Google Analytics
  5. Event Tracking Pt. 2: Implementations

Comments

  1. Pierre says:

    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.

  2. Jenny says:

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

  3. Scott says:

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

    Any ideas?

  4. Justin says:

    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

  5. Scott says:

    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

  6. Roos says:

    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”?

  7. Roos says:

    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”

  8. 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

  9. 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

  10. 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

  11. 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.

  12. jonathan says:

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

  13. 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

  14. Roos says:

    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?

  15. 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.

  16. 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();
    }

  17. Jim Galley says:

    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.

  18. Roos says:

    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

  19. 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

  20. Steve Weller says:

    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?

  21. petr says:

    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.

  22. 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

  23. Chuck says:

    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

  24. Hey Chuck,

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

    Justin

  25. Tim says:

    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.

  26. Vladimir says:

    Hello Justin,

    I have a question regarding a situation that looks like sub domain tracking but is more like the opposite of it.

    One of our clients created a specific zone on his domain. This zone can be considered a “seperate website”. http://www.site.com/web.aspx?p=1234 is the “homepage” of this homepage.

    Google analytics won’t accept creating a seperate profile for this kind of url. So I guess, they only way to make this work is to create a seperate profile for the main domain (www.site.com) and applying some filtering to include only the pages (web.aspx?p=1234 etc.) of this zone (aka seperate website on the same domain).

    But we really need to know the range of numbers (p=x) the content management system will create in order to apply a RE filter and make this work.

    What do you think?

    Vladimir

  27. Hi Vladimir,

    You could also create the profile based on some other attribute, like the page title. If this section of the site is different then try to find some filter field that has a unique attribute for that section.

    Another alternative would be to prograatically manipulate the urchinTracker() function and pass it a different value to create a unique pageview for that section. You can read more about manipulating urchinTracker() in my series on Tracking Clicks.

    Hope that helps,

    Justin

  28. Peter says:

    Hello Justin
    I have had so many problems with google tracking (I could write a book) over the last 18 months that I don’t really know where to start, but my first point is that I try to track domains (not subdomains) parked with my website selme.com
    I have found tracking scripts for the old urchin tracker on the web, but not for the new tracker.
    My first question is if I implement _udn=”selme.com”; in the old urchin script call, what is your recommended way of coding analytics filter to pick up ALL selme.com traffic AND other domain traffic?
    By this I mean that some domains have DNS pointers to selme.com and point at the same website but with their appropiate domain name.
    For example http://www.lovingdays.com has DNS pointing at http://www.selme.com and I need to improve on the method I use to try and capture analytics activity analysis.
    Any help you can give would be much appreciated
    Thanks
    Peter

  29. Peter,

    Google Analytics is a JavaScript based tracking tool, which means that is will only track information that is available in the browser. So, if you have a number of domains that are all directed to one site GA will not differentiate traffic to the individual domains. Remember, it can only identify the domain name that appears in the browser when the site loads.

    If you’re interested in the traffic to all of your aliases then you may want to consider changing the way the DNS resolves. However, you can’t have it both ways. In this case you would see data for all domains in a single profile, and then be able to create individual profiles based on a Hostname include filter.

    Thanks for the question,

    Justin

  30. Peter says:

    Justin
    basically I add a reference to DNS parked domains (pre-process and single redirect before landing page) i.e. http://www.ekisses.co.uk/ is given a ref=xxxxxx in its URL and I see the goals (reverse goal path) in my analytics output for these. I use free goal trackers I found (asclick and astrack – both for urchin.js only) but I do not specify _udn=”selme.com”;
    I also use ‘head match’ for goals..

    My question is if I implement _udn=”selme.com”; goal tracking, will it only pickup url calls from http://www.selme.com and exclude http://www.ekisses.co.uk?ref=xxxxxx??? for ‘head matches’?

    I am trying to understand the business of when/when not to specify _udn=”selme.com”; and the effects.. If specified, does it only pickup up domains beginning selme.com??

    It has been trial and error and I just wondered if you could enlighten me on what you know.

  31. Viper007Bond says:

    Thanks so much for this post! It’s a lot more clear and easier to understand than Google’s FAQ. Kudos to you!

  32. Peter,

    You only nee to add the _udn=”domain” if you wish to track unique visitors across multiple sub domains. Changing udn modifies the GA tracking cookie domain, thus allowing the cookie to be read by multiple sub-domains. Changing udn has no effect on goal tracking, it just effects the cookies.

    If you site is not using sub domains OR if you are NOT tracking visitors across multiple domains, then you should not tweak udn.

    I hope this helps. It is difficult to troubleshoot some of these issues via comments.

    Thanks for reading,

    Justin

  33. Jahangir says:

    Justin,

    What if we already have multiple profiles for different subdomains with different UA number for each and we want to integrate all these subdomains under a single UA number, how can we do it without loosing all the analytics data we have so far with the current profiles.

    Is there a way that we can implement what you suggested here and still retain the data from our previous profiles??

    Thanks,
    Jahangir

  34. Hi Jahangir,

    Unfortunately no, there is no way to implement this solution and retain historical data. The reason is that the data has already been spread out to all the different account numbers. There is no way to “move” the GA data between account numbers once it has been split.

    Thanks for the question,

    Justin

  35. Lucy Temple says:

    Hi Justin
    I want to filter traffic for several directories under one profile – specifically all traffic to:
    http://www.derby.ac.uk/online
    http://www.derby.ac.uk/e-learning, and
    http://www.derby.ac.uk/elearning

    Google says they don’t recommend you adding more than one directory include to one profile – so can I use custom filters to do this?

    Lucy

  36. Hi Lucy,

    You can absolutely create a profile to include those three directories. You’ll need to use a custom include filter with a regular expression for the filter to work correctly.

    It should look something like this:

    ^/(online|e-learning|elearning)/

    Make sure you test the filter on a test profile before you apply it to some “real” data.

    Justin

  37. christopher Evans says:

    Thank you for this article but I have a question that I not sure if this will work for me.

    I want be able to create a sub domain for each user and give them information about how many people come to that page and from where, etc. On there own page.

    The site would be laid out as bob.mysite.com or bill.mysite.com etc.

    If I understand how this setup works for each sub domain I will have to create a new profile in GA.

    And to get the report data on the users page I will need to have GA email the XML report, for each sub domain. Then I will have to parse each sub domain’s XML file.

    I don’t mind doing the work to create a script that parse the XML data. Thats a one time task, but having to create a GA profile for each sub domains is a lot of work. If I have 10,000 users, I will have to create 10,000 profiles.

    My question is there an easier/better way have GA track each sub domain and not have to create a profile for each user?

    Thanks for your time.

  38. Kevin says:

    For the people who have followed this article and are gettin “_gat is undefined” error, check if you have the http://www.google-analytics.com/ga.js included.

    Because it was not explicitely mentioned in this article. Check http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55524 for the article on the official site. Following code should be added:

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

    Hope this helps …

  39. Kevin,

    Thanks for the comment. I should have pointed out that you need to modify the tracking code, not just add the code in my article to the site.

    In general, the code on this site is just an example and should be customized and tested for your specific implementation.

    Thanks again for the comment.

    Justin

  40. Justin,
    I’m in a university environment and want to use a single tracking code across multiple sub domains AND sub directories. Is this possible with a single set of code as described in your original example or would we need one for the sub domain sites and a slightly different version for the sub directory sites? If the versions need to be different would you also comment on any needed changes to profile set-up? Many thanks.

  41. Therese,

    You can use the implementation technique that I describe in the article above. This will let you track all traffic to all of your sub domains and all of your sub directories in a single GA profile. It will also give you the flexibility to segment the data into additional profiles based on the sub domain or the sub directory.

    Creating additional profiles can be especially helpful when you want to limit the data that certain users have access to. For example, you could create an ‘alumni’ profile and then restrict access to those that work with alumni. It all depends on your needs.

    Hope that helps and thanks for reading.

    Justin

  42. Hi Justin

    I’ve got a problem with tracking a sub domain and hope you can help! Your instructions above are great.

    We have the main domain: gosh.org and a subdomain shop.gosh.org
    We are using the old code. I have the same tracking code on both sites.

    I wasn’t using the “_udn” bit of the code to get a single view, but am now in the process of implementing that. It’s just gone live on shop.gosh.org but not on gosh.org yet.

    Anyway – the problem is that I’m not getting any stats for the shop and can’t see why? I have it coming into my main profile obviously and have also set up a seperate profile with an include filter by Hostname.

    Can you think of any reason why the tracking wouldn’t be working?

    We have some pages on a different domain at donate.gosh.org and they are working OK.

    Thanks!
    Rachel

  43. David Ordal says:

    Justin-

    Just wanted to let you know about a quick correction in your code above. You have

    pageTracker._setDomainName(“sitedomain.com”);

    This should be
    pageTracker._setDomainName(“.sitedomain.com”);

    Per the google docs:
    http://code.google.com/apis/analytics/docs/gaJSApi.html#_gat.GA_Tracker_._setDomainName

    “Be sure to use a leading “.” in front of your domain name, as illustrated here. The leading period ensures that the cookie will be accessible across all hosts. Otherwise, the cookie is accessible only in example.com.”

  44. David: Thanks for pointing that out. There has been a lot of confusion on the exact format as some Google documents do not show the leading period. In our tests we have not see any problems with omitting the period. It does not appear that the period affects the cookie domain.

    Rachel: If you’re not seeing any data I would double check your filters. It may be that your include filter is incorrect. Also check the Hostnames report. Are you seeing the shop.gosh.org domain in that report? If not, then the issue is probably due to the tracking code implementation or a filter.

    Thanks to you both,

    Justin

  45. Jenny says:

    Hi there,

    I have, somewhat, successfully set this up. I have 1 main domain, and several groups of sub-domains.

    The problem I am having is that each profile has a a visitor count, however the sub-domain profiles don’t have anything else, there is no data in the content sections, traffic sources, etc.

    Can anyone assist with this issue?

    Thanks in advanced.

  46. Jenny,

    Just to be clear, you’re seeing visits in the sub domain profiles but no other data, like pageviews? That’s seems really strange, I’ve never come across that problem before.

    My initial reaction was that your sub domain specific profiles don’t have any data. In that case I would suggest checking your filters to make sure they’re correct.

    But I’m not sure why you would be seeing some metrics, like visits, and not others, like pageviews, etc.

    Sorry, that’s a new one on me.

    Justin

  47. Jenny says:

    Hi Justin,

    That is correct, I am seeing visit data but nothing else.

    This is what is displayed on the main screen of one of my sub-domain setups:

    2,929 Visits
    0 Page Views
    0.00 Pages/Visit

    Very odd!

    Actually, both of my sub-domain setups are reporting the same amount of visits.

    My Filter Setup:
    Sub Domain 1 Setup: Include / Hostname / sub1.maindomain.com
    Sub Domain 2 Setup: Include / Hostname / sub2.maindomain.com

    Any assistance would be greatly appreciated.

    Cheers,

    Jenny

  48. Jenny,

    My guess is that you’re doing something with custom segmentation. That’s the only thing that I can think of. Sometime, when custom segmentation is used, GA will create visits but not pageviews.

    That’s my best guess without actually being able to work on the site.

    Justin

  49. John says:

    Hi,
    We installed the code & the filter and it is working properly. However, one problem that we are having is in the Content Overview report, analytics now distinguishes between “www.domainname.com” and “domainname.com”, depending on how the visitor enters the site. What do i need to do so that the report will treat these as the same page?

  50. mike says:

    I have a something.site.com. This is the shopping cart. I also have http://www.site.com. These are all the content pages. All the content pages send users to the cart. All the transactions that are recorded come in with a referral of http://www.site.com which logically makes sense, but in reality there are other referrals.

    I have tried to set the pageTracker._setDomainName(“site.com”);

    so that there is a consistent cookie on both the subdomain and the domain, however, all the transactions still come in with the referral being http://www.site.com

    Anything I am missing?

Trackbacks

  1. [...] Tracking Sub Domains with Google Analytics [...]

  2. [...] Go to Analytics Talk to find out the benefits of Justin’s approach! [...]

  3. [...] The question came from David Culbertson over at  LightBulb Interactive and the answer I left included a reference to this page on AnalyticsTalk – Tracking Sub Domains with Google Analytics [...]

  4. [...] Tracking Sub Domain Traffic: As I showed in the profiles above we have athletics.wofford.edu split out in a separate profile so that we can look at data just to that sub domain.  To do this requires two extra filters.  One filter is applied to the sub domain profile while the other is applied to the default profile.  Instead of trying to explain all this in detail Justin Cutroni over at Analytics Talk put together a post on Tracking Sub Domains with Google Analytics back in November that I followed then and have been enjoying the results since. [...]

  5. [...] Let’s consider the websites for Major League Baseball. Each team has their own site located on a subdomain. There is also an MLB store and different micro sites dedicated to things like the All Star Game and the World Series. [...]

  6. [...] helpful resource: epikone.com var addthis_pub = ‘karolzielinski’; var addthis_language = ‘en’;var addthis_options = ‘email, [...]

Speak Your Mind

*