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

New ga.js tracking code:

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.


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:


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

Be Sociable, Share!

    Like this post? Sign up to get posts delivered to your inbox.


    1. says

      Hi Mike,

      Seems like you’ve done everything you need to. I know this sounds silly, but you might want to adjust the date range to view traffic for the days immediately after the change. It may be that the historical data is pushing the real referral traffic down.

      Hope that helps and thanks for the question.


    2. mike says

      Thanks for the attention Justin.

      I have also used your Trans Mods (the custom filters) to see where my transactions are coming from. I made the change on Thursday and it’s Monday. I still see ALL the transactions on like this


      The Analytics keep seeing as the referral. I looked at the cookies. It says the same thing.

    3. says


      The only other thing I can think of is that there is something interfering with the cookies. It’s tough to say without seeing the exact site.

      Sorry I can’t be more help,


    4. says


      If each domain resolves to a different domain name, then you don’t need to worry about anything. Google Analytics will use a first party cookie to track visitors. So, when a visitor lands on then they will receive a cookie, specific to, Then when they go to they will get a different cookie for Just make sure that you put a different tracking code on each site or all of the data will end up in one profile, which could be fine if you want all the data for all the sites to be aggregated.

      Hope that helps,


    5. says

      Justin, thanks again for such a great resource.

      Thanks to your posts and guide, we are almost fully migrated from HBX to GA for our social network. We utilize subdomains (about 30 total) for various sections and communities.

      We have one global instance of the js code on all pages and using _setDomainName and filters, we have a rollup profile and one for each subdomain.

      The rollup is working fine but the subdomain profiles are counting visits incorrectly. Within the subdomain profiles, GA is showing that there are more visits then there are pageviews. Looking at yesterday’s data – 11,500 visits but only 4,500 pageviews.

      If it makes any difference, we are using _setVar to pass in info about our members (age, gender, etc.).

      The filter is setup as described above: Custom Filter – Include – Hostname – subdomain\.root\.com

      If we can get this figured out, we’ll be fully moved over from HBX!


    6. Hamish says

      Hi Justin, I have a problem with tracking visitors across multiple subdomains and root domains.
      My website has many subdomains such as, etc. From the subdomain pages, there is a form that sends visitors to another domain
      How do I successfully track visitors to both the subdomains and through to the other domain and still keep accurate referral data. At the moment, referral data for credits

    7. Hamish says

      In addition to my previous question, I use setallowlinker(true) and setdomainname(none) on both websites. However the setdomainname(none) creates problems with the subdomains on
      Also, i have added an onsubmit linkbypost(this) code to the php form (I’m assuming the form uses method=post – it actually doesn’t state whether it is a get or a post) but the cookie data isn’t showing up in the posted url. I am at my wits end here – any help would be appreciated.

    8. says

      Great post, it’s the main post in Google for this kind of search which shows its worth..!

      One quick question. We have this setup running quite nicely, but my only niggle is that the referrers report shows the main domain and the sub-domains as referrers. Having not used GA on another website properly before, does this usually happen in GA where it names the name of the website/websites that are tagged as top referrers?

      It doesn’t really give an accurate view of where your traffic is coming from – is there a suggested filter which can get round this problem?

      Thanks – Chris

    9. says


      If you’re seeing the website, or one of its sub domains as the main referrer then there could be an issue with the tracking cookies OR there may be some type of frame or iframe on the site. While it’s impossible to completely eliminate self referrals, they usually are not #1 and #2 in the referral list.

      Thanks for the question,


    10. says

      Hi Justin,
      I’ve found your tutorial really helpfull. I’ve found also a similar implementation reported by Brian Clifton on “Advanced Web Metrics with GA”.
      It seems working but I’m still experiencing problem with the referral as everybody here in the post. Do you think we should post bug report to GA dev team? Check on but I think implementation is right..

    11. says

      just a quick help for people still looking for help.
      I’ve solved the referrer problem, may be it’s not the clean way but just add a call to your tracking system like this:

      Good luck everybody

    12. Matthew says

      Hey Justin!

      Great tutorial!

      I just set it all up and on my site I have ALOT of subdomains. Ive just finished doing most of them but its made my account very messy. I thought the whole idea of adding a profile to an existing domain was to make it neater and have it “nested” almost. When as to get to them you have to click on the main domain.


      At the momment every single one is listed on my front page of my Analytics account and is getting messy!

      Anyway around that?



    13. says

      I have a main site using Joomla. I have embedded the GA code at the right spot and it is tracking. We have decided to put up a WordPress blog in a subdirectory. Should I copy the GA Code into the right part of the WordPress theme as well?

      Thanks!! Great site.

    14. says

      Hi Justin

      Is it:




      or does it matter?

      Your article and Brian Clifton’s book don’t have the ‘.’ but the Google help page does!

      Thanks for any help.

    15. Nez says


      I set up my analytics profiles based on your tutorial although I have one question regarding the sub-domain profile.

      Should the website URL under settings match the sub-domain (ie or should it remain the same as the main profile (


    16. says


      If you don’t add the tracking code then you won’t track the WordPress section with GA. If you don’t want any insight into what happens on the WordPress section then leave the GA code off ;)


    17. says


      Great question. You should add the sub domain to the website URL setting. This way the site overlay report will work for your sub domain profiles.

      Thanks for the question,


    18. Chris says

      Great article!

      A couple of follow up questions.

      1. what if I don’t want a master profile that rolls up all the subdomains? for example – I want to create another profile that is used only for testing purposes in a QA / Dev environment. In this case, do I not add the additional line of code – pageTracker._setDomainName(“”) ?

      2. On the production profile, can I use the same method used for creating a subdomain profile to exclude the subdomain from the production profile? So instead of include, I create a filter that says exclude – hostename – subdomain\.mydomain.\com?


    19. says


      1. If you want to create a toally separate profile, that will never have the data, then no, you don’t need to the _setDomainName line of code. Just remember that when you do roll into production, and you want to have all the sub domains rolled up, then you will need to add that extra line of code.

      2. Yes, creating an exclude filter would follow the same process as an include filter.

      Thanks for the questions and thanks for reading the blog.


    20. Jeremy says

      Wow! Thanks Justin!
      Just want to add my praises for your succint and brilliantly illustrated tutorial. It even worked for a plough-horse like me ;)
      You’ve been a great help to me today – so I’m sending you all the positivity I can >>mmmmh<< – there you go! Hope it arrives,


    1. […] Tracking Sub Domain Traffic: As I showed in the profiles above we have 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. […]

    2. […] di distinguere i singoli siti; di default invece Ga non ti segnala la radice di ogni contenuto. Te lo spiega qui alla fine del paragrafo 1. Rispondi Citando « Discussione […]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>