Filtering Subdomains in Google Analytics | Subdomain Tracking & Filters
Back to Mingle

Filtering Subdomains in Google Analytics

Posted in Digital Marketing — August 23, 2011

This guide is updated as of August 2012 with new screenshots and code.

Recently I’ve had a spate of sites that I’ve needed to help configure subdomain cross-tracking for in Google Analytics. Even armed with tons of documentation on this (not to mention my own Google Analytics Certification), it’s one of those things that can seem filled with confusing, nitty-gritty details.

So without further ado, here’s my own personal guide of how to set up subdomain profiles – complete with tons of nifty screenshots.

Part A: CUSTOMIZING THE CODE

Here’s how to customize the code for both your main domain as well as the subdomain.  (I get into the specifics of what this means in a separate subdomain post.)  Don’t forget to insert your own UA # and domain (without the www).

_gaq.push(['_setAccount', 'UA-#########-1']);
_gaq.push(['_setDomainName','yourdomain.com']);
_gaq.push(['_addIgnoredRef', 'yourdomain.com']);
_gaq.push(['_trackPageview']);

(Note: This is not the entire code, just the part you need to customize!)

Part B: THE PROFILES

Profile #1: Main Domain
Let’s pretend that so far you only have one profile, and it’s for your main domain name. When you log into Google Analytics and select ADMIN, you should see something similar to this.

Main Profile

We are going to be setting up 2 additional profiles, so let’s get started! To do this, just click on “+ New Profile” as per the screenshot above.

Profile #2: The Subdomain

On the subsequent page, choose:
1. Add a profile for an existing domain
2. Name it something intuitive (subdomain, etc) – this is easy to change later too.
3. Set the time zone

New Subdomain profile

After you hit “Continue”, the next page should refresh with a drop-down that lets you choose between your main domain or subdomain profiles:

profile drop downs

Profile #3: ALL Domains

We also want to create another profile that will combine traffic from both the subdomain and the main domain. So, click “Add New Profile” again and fill out the fields as follows:

1. Add a profile for an existing domain
2. Name “Sub + Main Domain” (or whatever you think sounds best)
3. Set the time zone

sub plus main domain profile

After adding this, when you return to the main Admin page you should see three drop-down choices:

- Main Domain
- Subdomain
- Sub + Main Domain

Part C: THE FILTERS

Now we need to apply filters so each profile only shows traffic on the domain it was set up for. Here is where you can add filters in the Google Analytics Admin:

add filter in google analytics

Filter #1: Main Domain

First, select the drop-down profile for the main domain and then “add filter”. You should come to a screen that looks like this:

base domain filter
1. Select “Add new Filter for Profile”
2. Filter Name: “Base Domain Only” (or your own description)
3. Filter Type: Custom Filter
4. Select “Include”
5. Filter Field: “Hostname”
6. Filter Pattern: (www.|^)yourdomain.com
7. Case Sensitive: No

This filter basically says that we only want to include traffic from www.yourdomain.com. Be sure to get all the regular expression characters in the filter pattern correct. It looks weird, but it’s important to copy exactly as written above (replacing “your domain” with your actual domain name, of course).

Hit “Save Changes” and you’re set!

Filter #2: Subdomain

Go to where you can add a filter for the subdomain and fill out the screen as follows:

subdomain filter only

1. Select “Add new Filter for Profile”
2. Filter Name: “Subdomain Only” (or your own description)
3. Filter Type: Custom Filter
4. Select “Include”
5. Filter Field: “Hostname”
6. Filter Pattern: sub.yourdomain.com
7. Case Sensitive: No

Filter #3: Combined Domains

This profile already shows traffic to both domains by default. But wouldn’t it be nice if Google Analytics displayed the full URL? Otherwise, with reporting that looks like “/home” and “/services” and “/about-us”, it’s hard to tell which page is from which domain.

So, let’s apply a filter to the combined domains profile to display full URLs:

show full url

1. Select “Add new Filter for Profile”
2. Filter Name: “Show Full URLs” (or your own description)
3. Filter Type: Custom Filter
4. Select “Advanced”
5. Field A -> Extract A:
Select “Hostname” from the drop down.
Enter (.*) in the text box.
6. Field B -> Extract B:
Select “Request URL” from the drop down.
Enter (.*) in the text box.
7. Output to Constructor:
Select “Request URL” from the drop down.
Enter $A1$B1 in the text box.
8. Field A Required: Yes
Field B Required: No
Override Output Field: Yes
Case Sensitive: No

THAT’S IT.
After 24 hours your profiles should be reporting correctly on traffic to all three profiles. If there’s one thing I’ve learned about Google Analytics, it’s that small mistakes and typos can mess up reporting. Be sure to double check all of your regular expressions and settings – even one character can make or break a filter. Hopefully I didn’t make any typos on this post. Double checking… again….

Also – you may want to create one more profile that has NO filters applied to it whatsoever. This just ensures you have a clean profile with all data in it, that hasn’t been changed in any way.

Happy data mining!

Written by .

***********
NOTE: Below is the old code I had suggested – included here for reader reference as per older comment threads below and related to my subsequent post.

***********

<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-#########-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowHash', false]);

_gaq.push(['_setDomainName','.yourdomain.com']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

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

Leave a Reply

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

29 Comments

  1. Thank you for the great article!
    I’m just checking, my domain has a – symbol in the URL (ap-photography.com.au)
    When adding the Filters, would I be right in just adding (www.|^)ap-photograhy.com.au
    I don’t need to put any symbols before/after the – ?

    Thanks again!! Great help :)

    Posted by Anthony (AP Photography), September 23, 2011 | Reply

    • Hi Anthony, I believe you’ll to use a backslash to escape the dash. This turns it into plain text that will be interpreted literally (as opposed to how a dash might otherwise function inside RegEx). We already had to put backslashes in front of all the periods in your domain name, so we’d want to do the same for a dash.

      Ex:

      (www.|^)ap-photography.com.au

      Experiment with that and let us know how it works out?

      Posted by Sarah, October 3, 2011 | Reply

  2. You are my new favorite person. I have been looking for something like this for weeks and all the solutions given were far too complicated…filters are simple and work.

    Posted by Nick Budden, October 10, 2011 | Reply

    • Yay! Thanks Nick, glad we were able to help.

      Posted by Sarah, October 12, 2011 | Reply

  3. This is amazing. The other help articles I’ve seen are all super complicated and inconsistent. Well done.

    Two questions, if you don’t mind:
    1. When selecting “One domain with multiple subdomains” in Tracking Code, Google provides this in the code:

    _gaq.push(['_setDomainName', 'mydomain.com']);

    Note the missing “.” before “mydomain.com”

    Your example would have me change the code to “.mydomain.com”

    Just want to double check that Google’s code is incorrect?

    2. Re: Filter #1, would an “Exclude” filter work just as well? To remove my subdomain from the top level profile?

    Thanks!
    Jake

    Posted by Jake Levine, November 16, 2011 | Reply

    • Hi Jake,

      So sorry for the late response here but better late than never!

      1. Google’s code with the period is not an error, but just a different way of configuring it. Our understanding is that you would need to use the period if you want to be able to track lower-level subdomains, such as: sub2.sub1.yourdomain.com. When researching potential configurations, we learned that it’s best NOT to use a period unless you have need to track more complex subdomains, since the period can cause issues with resetting cookies.

      2. I don’t see why you could not use an exclude instead of an include! However: disclaimer: I have not personally tested this method yet. ;)

      Posted by Sarah, November 29, 2011 | Reply

  4. I can’t tell you how many articles I have read to try and get the filter for the main domain (& how many didn’t work). Great job and many thanks.

    Posted by Ali, November 29, 2011 | Reply

    • Thanks Ali! Glad to have helped, we needed this write up to also help US keep the process straight.

      Posted by Sarah, November 29, 2011 | Reply

  5. thank you. :]

    Posted by Pavel, December 9, 2011 | Reply

    • :)

      Posted by Sarah, December 9, 2011 | Reply

  6. Great summery. After searching Google’s help pages and help forums, I often found that each page was attempting to answer multiple questions. Thus, if you try and use their sub domain help pages as a tutorial, you are given conflicting info as you move from how-to-code to how-to filters. And none use screenshots for guidance.

    I do have one unresolved question: the field in GA where you enter the “Filter Pattern” no longer accepts a slash (/ or ) to escape a character. Could this be a ‘feature’ to simplify the process or do I need to copy and paste the backslash for the filter to work?

    Posted by Mat, January 2, 2012 | Reply

    • I hadn’t tuhoght about setting up a second or third profile for all the filtered results. That’s an excellent idea.I noticed when I set up the second profile it had the exact same UA string. I tuhoght it would add a -2 or -3 at the end. Is it OK if it’s the same string? I guess technically it is the same tracking code I’m simply filtering on Google’s end when I view reports. Is that correct?

      Posted by erika, February 9, 2012 | Reply

  7. Hi Mat,

    I’m not sure why you cannot enter in a slash – when we use the new version of Google Analytics we are able to add a new filter and type in a slash? Did you select “custom” filter? I may need to repost this entire article with updated screenshots soon, given that Google is apparently going to switch everything over to the new version any day now…grrr!

    Posted by Sarah, January 3, 2012 | Reply

  8. Hi Sarah,

    Excellent article, thanks for posting! I have a question for you though. We have a site with two sub domains that we are currently tracking on GA. I set it up using the following code…

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-########-1']);
    _gaq.push(['_setDomainName', 'domain.co.uk']);
    _gaq.push(['_addIgnoredRef', 'domain.co.uk']);
    _gaq.push(['_trackPageview']);

    I got it from this blog – http://www.roirevolution.com/blog/2011/01/google_analytics_subdomain_tracking.php

    I notice that it is quite different from the code you have in the article. Can you tell me if the code I am using will work with the filters described above? I have seen so many different explanations of sub doman tracking I just don’t know who to believe!

    Posted by Alan, January 20, 2012 | Reply

  9. Hi Alan,

    That article makes some REALLY good points! I am now considering tweaking the above code to implement those suggestions about turning off hashing, etc. Let me fiddle with this some and I will post another response, but in the meantime, I’m sure that ROI Revolution’s recommendations are superb and work very well. FYI, one of my goals in writing this post was not only to provide the code, but also to help guide people through the entire process of slugging through the GA Interface, which can be confusing….

    Posted by Sarah, January 20, 2012 | Reply

  10. Hi sarah,

    wonderful article. I have a question regarding the Filter #3: Combined Domains. How would you tweak it so I only see the subdomain part of the hostname in my reports

    let’s say I have:
    subB.domain.com
    subA.domain.com
    domain.com

    and in my reports I want to see:
    subA/about-us
    subA/about-us
    /about-us

    Instead of subA.domain.com/about-us etc.

    any idea how I could modify the regular expression? thanks much

    Posted by kenn, March 2, 2012 | Reply

  11. Hi Sarah,

    Thanks a lot for this resource. I’ve bookmarkerd it for furture reference.

    The thing I’m wondering is: If I have one website with one subdomain.. For example: http://www.mysite.com with subdomain subdomain.mysite.com.. To track visits from these two sites, I only have to include the SetDomainName to .mysite.com, don’t I?

    I mean that I don’t neccesarily need to include the AllowLinker etc, right?

    Many thanks,

    Hans

    Posted by Hans, June 19, 2012 | Reply

    • Hi Hans,
      You are correct! We had included that just because it was standard for many clients that needed cross-domain as well as subdomain tracking, but if all you are doing is trackign subdomains, you don’t need it.. Stay tuned for a new post that will go into the coding details (rather than miles of screenshots from GA) soon. :)

      However, here is the updated code that I am now going to suggest people use:
      _gaq.push(['_setAccount', 'UA-#########-1']);
      _gaq.push(['_setDomainName','yourdomain.com']);
      _gaq.push(['_addIgnoredRef', 'yourdomain.com']);
      _gaq.push(['_trackPageview']);

      Posted by Sarah, June 21, 2012 | Reply

  12. Thanks, your tips really helped me a lot..

    Keep posting buddy :)

    Posted by Vineet, July 6, 2012 | Reply

  13. Finally a post which made complete sense regarding this!. Thank you so much! It’s working for me finally.

    I have just one small unresolved issue. For the ‘only sub domain’ profile I see the URLs starting with / for example: /subdomain.example.com/ or /subdomain.example.com/something/ in the reports

    I’m not sure how I can get rid of extra / in the start

    Regards,
    Ojus

    Posted by Ojus Naravane, July 18, 2012 | Reply

  14. I have the Filter #3: Combined Domains filter set up and wonder if it is creating a problem I cannot solve.

    In reports in GA there are links to the pages on my website (in between the URL and the metric). You are supposed to click on this little image be taken to your website. But instead of the of the URL being
    http://www.domain.com/page
    it becomes
    http://www.domain.comwww.domain.com/page

    I suspect this comes from this filter, which adds the domain one more time. Is there a work around for this?

    Posted by Rich, July 26, 2012 | Reply

  15. Hi Rich and Ojus,

    I might just double check that the filter you have set up (to show the entire domain name and not just the directory) is 100% correct?

    This is working for different accounts we’ve set up so I know this does indeed work – but sometimes, even one misplaced period or * or parentheses can cause all sorts of weird things to show up in Google Analytics. Those regular expressions are awfully picky.

    Posted by Sarah, July 27, 2012 | Reply

  16. Thanks for the awesome post, very helpful.

    I do, however, have a rather simple question to ask. For the subdomain filter, you have put the code:

    sub.yourdomain.com

    I understand that “yourdomain” should be changed to my domain, but for the “sub” part, should that stay the same, or be changed to the name of the actual sub-directory, ie “blog.mydomain.com”?

    Posted by Jeff, August 1, 2012 | Reply

    • Hi Jeff,
      Great question. You should REPLACE “sub” with the real name of whatever your subdomain is!

      Posted by Sarah, August 1, 2012 | Reply

  17. Hi Sarah, I want to ask about filtering subdomains,
    After I follow this steps, I will have 4 profile on 1 website in Google analytics rite?
    So, after few days then I check out my analytic again, it seems that the profile “clean one” (that has no filter at all) vs “sub + main domain” has the same number of Visitors Overview.
    is that right?
    because, I think that the total number of visitors for “sub + main domain” is from “sub domain” number of visitors + “main domain” number of visitors.

    Posted by Jeff2, August 7, 2012 | Reply

    • Hi Jeff,

      Yes, that’s correct! The only difference is that you may have additional filters applied to your sub+main domain profile (such as the filter to display full URLs), so it’s just an extra, extra careful precaution to have one additional domain with NO filters at all, that keeps all data in its original state. But in most cases, the traffic to the unfiltered domain profile and the sub+main domain profile should theoretically be the same.

      Posted by Sarah, August 8, 2012 | Reply

  18. Hi Sarah,

    I have a unique situation where I work in that the subdomain is generated when a customer signs up for a home site. The dynamic code on the home site automatically places our Google Analytics code on each of these home sites.

    So on any given day new subdomains are being automatically generated. My question is: Is there anyway to set this up where it shows all of the currently available subdomains rather than having to specify each subdomain?

    Thanks for your help!

    Posted by Todd, August 17, 2012 | Reply

    • Hi Todd,

      Interesting question! Well – you can always create a Google Analytics profile that shows traffic to all subdomains, and then use the “Show Full URL” filter as described above. This way, you’ll see all traffic under one Google profile (including for recently added subdomains), and you should also be able to see the entire URL. Just be sure that people are using the appropriate GA code on their subdomains – since if everyone isn’t using the same code, then things may not report as expected. Hope this helps?

      Posted by Sarah, August 24, 2012 | Reply

  19. Sara,

    Thanks for breaking it all down clearly. I agree that htere was a lot of out-of-date and unclear information published on the topic.

    I am having the same problem as Rich above (posted July 26) whenever I try to preview a ULRL from within GA or when I use an In-Page view. The profile filter has appended the domain name to the URI (as it should, as the data is written out at the end of the day), but the In-Page or Preview function trying to do the same. This make the URL effectively http://www.mydomain.com/www.mydomain.com/my-page

    It his an unavoidable by-product of the filter strategy?

    By the way, this affects URL-based Goals as well, so perhaps we should warn the readers who invoke this profiling strategy. They must rewrite their URLs to include the domain now, even though the GA instructions tell you not to.

    Thanks for the clear article.

    Posted by Steve, August 20, 2012 | Reply