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

Filtering Subdomains in Google Classic Analytics

Posted in Search & Social — August 23, 2011

Update for 2016: I now recommend using Google Tag Manager for subdomain tracking. It’s much easier because you don’t have to customize the code.

NEED GOOGLE UNIVERSAL INSTRUCTIONS? We have a new post on subdomains for Google Universal Analytics.

NEED TAG MANAGER INSTRUCTIONS? We also have a new post on subdomains for Google Tag Manager.

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.


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([‘_addIgnoredRef’, ‘‘]);

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


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


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

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


(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);



  1. Thank you for the great article!
    I’m just checking, my domain has a – symbol in the URL (
    When adding the Filters, would I be right in just adding (www.|^)
    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.



      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’, ‘’]);

    Note the missing “.” before “”

    Your example would have me change the code to “”

    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?


    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: 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’, ‘’]);
    _gaq.push([‘_addIgnoredRef’, ‘’]);

    I got it from this blog –

    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:

    and in my reports I want to see:

    Instead of 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: with subdomain To track visits from these two sites, I only have to include the SetDomainName to, don’t I?

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

    Many thanks,


    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([‘_addIgnoredRef’, ‘’]);

      Posted by Sarah, June 21, 2012 | Reply

  12. Hi Sarah,

    Can you please guide me how to setup a customer filter (regex pattern) so that data from these two sub-domains should be captured into the reports and rest everything to be excluded.?

    Many Thanks.

    Posted by amardeep, June 22, 2012 | Reply

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

    Keep posting buddy 🙂

    Posted by Vineet, July 6, 2012 | Reply

  14. 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: / or / in the reports

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


    Posted by Ojus Naravane, July 18, 2012 | Reply

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

    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

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

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

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

    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

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

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

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

    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

  21. Gearing up for a big subdomain tracking and reporting project and came across this awesome site – thanks for posting.

    Right now, the company only has their set up on GA with no major tracking, filters, or anything (and not the full urls). They have 100+ different clients under,, etc… The need is to segregate analytics and reporting for each subdomain client, as well as certain event and goal tracking.

    1) Based upon your post – I’m assuming we do the same setup here, correct?

    2) Once the segregated subdomain reporting & analytics is setup – how would one go about providing secured segregated reporting to each of the respective clients with their own private login?


    Posted by Chris, December 10, 2015 | Reply

    • Hi Chris,

      You can assign permissions at the VIEW level for each subdomain, so users can’t see property level or account level info for other subdomains at all.

      Depending on what you’re trying to do, you may want to consider giving each client their own UA# for each subdomain. This makes it easier for them to have more continuity of data and control over time if they ever plan to move to a different domain configuration.

      Finally – you might want to consider upgrading everything to Google Tag Manager! That way, you can put the same code on all pages (no subdomain tracking needed or extra configs needed). This will also make it easier to create events and rules, etc, based on the subdomain.

      Posted by Sarah Mackenzie, December 11, 2015 | Reply

  22. (Part C: THE FILTERS) What would the code look like if www has been removed from the base domain and my base domain is

    Posted by Raj, December 12, 2015 | Reply

    • You can use the same filter – the regular expression is built so it works with or without the www.

      Posted by Sarah Mackenzie, December 14, 2015 | Reply

  23. Hi, Sarah
    Great post.
    i am not that good in GA, and have a small question, i have domain and sub domains (about 30 of them))))
    All of them work well, the only issue i got is with the new one, just launched,so to make it simple domain and existed, then i got, and the problem i have that in real time, i can see the information from, but in behavior and Dhrilldown pages, is the the information that belongs to, and not to
    The UA is different and unique for each sub the issue is somewhere else.GA code is correct as well, for each sub domain, any thought about it?

    Posted by Benjamin, February 15, 2016 | Reply

  24. Hey, awesome article, huge life saver! 😀

    just one small question,

    When adding the main domain part – on your text, you explain that the filter pattern should be like this: (www.|^)yourdomain\.com

    But on the image you’ve added the filter is like this (www\.|^)yourdomain\.com – with the backslash after the www – are both ways correct?

    Posted by bob, February 17, 2016 | Reply

    • Good catch! You need to escape the period, so the slash is needed.

      Posted by Sarah Mackenzie, February 17, 2016 | Reply

  25. Hy, Sarah!
    Thank you for the amazing article!
    We have problem with subdomain tracking for mobile version of site in GA.
    Desktop version includes few subdomains eg, etc
    And the mobile version of site also include subdomains and it looks like that:,

    Is it possible to use filter in GA to see traffic for mobile version of site for all subdomains in mobile version and for each subdomain in mobile version separately? Without traffic from desktop version of site.

    Could you, please, advice something?☺

    Posted by Ryna, April 25, 2016 | Reply

    • Hi Ryna,

      These guidelines will work for multiple subdomains as well. The tracking code won’t be much different – you’ll just want to create several different views in GA, all with different filters, that allow you to easily separate out traffic to all of your different subdomains.

      I’d also encourage you to upgrade to Google Tag Manager, as this can help make subdomain tracking even easier.

      Posted by Sarah Mackenzie, April 25, 2016 | Reply

  26. Hi Sarah!

    Great article and walk-through! I wish everyone were this thorough.

    I have an instance where I need to have two different sets of GA tracking. Ours and client. Our code developer is on vacation and I am not a coder. I can do way simple things.

    I duplicated what our developer created. I think I did it right. I can’t verify because it could just be possible that there isn’t any site traffic…

    Would you look at this code? And give me a shake or nod?

    Posted by Virginia, May 25, 2016 | Reply

Leave a Reply

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