Filtering Subdomains in Google Analytics

Topics: Analytics

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

 

Here is the customized code in context; keep in mind you may have additional reasons to vary from this example depending on your unique set up:

<script type="text/javascript">

_gaq.push(['_setAccount', 'UA-#########-1']);
 _gaq.push(['_setDomainName','yourdomain.com']);
 _gaq.push(['_addIgnoredRef', 'yourdomain.com']);
 _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>

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 URI” from the drop down.
Enter (.*) in the text box.
7. Output to Constructor:
Select “Request URI” 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>

51 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

  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

  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

  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

  5. thank you. :]

    Posted by Pavel, December 9, 2011 Reply

    • :)

      Posted by Sarah, December 9, 2011

  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

  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

  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

    • Hi Ojus,
      I have this problem too, did you managed to fix that ?

      Posted by Uzi, December 19, 2012

    • Try using /$A1$B1 for the constuctor – make sure there is the forward slash (see new comments and updates to instructions).

      Posted by Sarah, December 20, 2012

  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

    • Try using /$A1$B1 for the constuctor – make sure there is the forward slash (see new comments and updates to instructions.

      Posted by Sarah, September 24, 2012

  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

  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

  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

  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

    • Try using /$A1$B1 for the constructor instead. The forward slash may help with this!

      Posted by Sarah, September 24, 2012

  20. In this demo (http://bewebfocused.com/setting-up-google-analytics-to-handle-sub-domain-filtering) it says “Field B Required” is required, but in this demo it says no it is not required.

    ?

    Posted by raph, September 13, 2012 Reply

    • Hi Raph,

      There are a lot of confusing instructions and differing opinions out there! I cannot speak to the other article, but what I do know is the above instructions worked for us…. You could try creating a few different profiles and seeing which works best? Whenever I am not sure about a filter, I create a new profile, test it out, and see how it works. Hope that helps, let me know what you find?

      Posted by Sarah, September 14, 2012

  21. At the google analytics page it’s recommended to start the “$A1$B1″ with a forward slash. So the correct wat would be “/$A1$B1″
    Doesn’t know the real impact of the change.
    http://services.google.com/analytics/breeze/en/v5/domains_subdomainsv22_ad1/

    Posted by Simon, September 21, 2012 Reply

    • GOOD catch. Google doesn’t include the slash here: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite

      But in the video, they say, “Note that the constructor must match exactly what is shown in the slide, starting with the forward slash. The filter works by appending the hostname to the request URI. As a result, you’ll be able to distinguish between identically named pages within your subdomains.”

      I have updated the above instructions to note this.

      Posted by Sarah, September 24, 2012

  22. In the “base domain only” instruction, the Filter Pattern: (www\.|^)yourdomain\.com says only include traffic from http://www.yourdomain.com

    What if I don’t use www in my urls and simply use yourdomain.com as the base domain?

    Does the same pattern applly

    Posted by Millard Baker, October 2, 2012 Reply

    • Yup, that should work too. If you don’t use www URLs, I’d just be sure that all the www versions of your pages are 301ing to the non-www version.

      Posted by Sarah, October 4, 2012

  23. FYI – it’s Request URI (as in Universal Resource Identifier), not Request URL.

    Posted by Chris, October 22, 2012 Reply

    • Good catch – typo updated, thanks.

      Posted by Sarah, October 22, 2012

  24. Sarah,

    Thank you so much for this helpful post! I went through 30 websites to find this simple answer.

    I do have one small question. I noticed other sites suggest this hostname filter for the subdomain profile: ^sub\.yourdomain\.com$
    whereas you suggested: sub\.yourdomain\.com

    Would you be able to tell me which is correct? Or what is the difference?
    THANK YOU SO MUCH!
    Johnny

    Posted by Johnny, February 2, 2013 Reply

  25. Hi Johnny,

    I am glad the post helped! So – I looked into this, and I believe that the caret and the dollar sign can be placed at the beginning and end of the expression (respectively). They just indicate “expression begins here” and “expression ends here”. I think both versions should work, though I haven’t tried the version with framed with those additional characters myself.

    Posted by Sarah, February 6, 2013 Reply

  26. Here a Google exemple for Full page URL
    http://support.google.com/analytics/bin/answer.py?hl=en&answer=1012243

    Posted by David, February 20, 2013 Reply

  27. Quick question, my default Tracking Code looks like this:

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

    and you’re suggesting to change it to this:

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

    adding, obviously, that 3rd line of _gaq.push(['_addIgnoredRef', 'yourdomain.com']);

    My question is simply, what is that 3rd line doing? Within the new GA interface, as long as you enable “Subdomains of yourdomain.com”, isn’t GA doing the work to track subdomains automatically?

    Thanks!

    Posted by Michael, April 12, 2013 Reply

  28. This is fantastic. I have been battling with Google Analytics data integrity forever

    I think a little bit more info around _gaq.push(['_addIgnoredRef', 'yourdomain.com']); would really help your readers. I had to do a little bit more digging to truly understand its purpose.

    Thanks so much for the post!

    Posted by Kyler, October 2, 2013 Reply

  29. Hi Sarah,

    I’ve just followed your instructions, god help me as my webmaster in on vacation and I’m trying to get this to work. Two questions…

    1. I set up the sub domain view. (Google recently renamed their profiles to views). Within the view settings, do I list the website’s url as our website proper http://www.mydomain.com or our subdomain, go.mydomain.com?

    2. I see reference in your post to tracking code. Do I need to alter our tracking code or does setting up the views and filters do this for me? I’m trying to report on our full website traffic (website proper domain + sub domain). Right now we’re reporting on these separately and when we are asked how many website visitors we’re getting, we only report on our http://www.mydomain.com as we don’t have a combined report.

    Thanks in advance Sarah.

    -Karen

    Posted by Karen, October 17, 2013 Reply

    • HI Karen,

      1. Within the view settings you should be able to give it any name you want, whatever sounds best to you. You don’t even have to write the “www” domain name per se, you could even write MAIN DOMAIN and then SUBDOMAIN or something like that.

      2. Yes, you will need to alter your tracking code on both the subdomain and the main domain, as well as set up reviews and filters.
      Keep in mind that if your subdomain is already using a different code with a differnent UA#, moving forward, your subdomain will start to report in a new area in Google Analytics, and will no longer be tracking in the old area under the old UA#. There are pros and cons to this – the pro that moving forward is it’s easier to see everything all in one place. The con is that it could be harder to compare subdomain performance to previous months / years because you’ll have to toggle between the two different accounts.

      Here’s an example of subdomain tracking code implemented within an entire Google Analytics tracking code. Some websites may have additional customizations added beyond just subdomain tracking, so this may or may not be right for you, but hopefully this example helps:

      Posted by Sarah, November 4, 2013

    • See above – I edited the blog post to include “customized code in context”. ;)

      Posted by Sarah, November 4, 2013

  30. Hi Sara!

    Great article!

    One question:

    Im using GA to track multiple domains and have set the setDomainName to ‘none’.

    I would like to use the filter to display Full URLs. It seems to work, because I can see that the real time report is sending me data and URLs are full ie http://www.mysite.com/page1 and another visitor from mysite.net/page24.

    Can I say that it is working then? Because when I look in the Behaviour->Site Content->All pages it still only show /page24. Maybe I have to wait a while to see if it works there? I have just implemented the filter.

    BR,
    David

    Posted by David, December 19, 2013 Reply

Leave a Comment (*Required Fields).

© 2010-2014 Big Daylight. All Rights Reserved. Privacy Policy | Site map