Google Analytics subdomain tracking, custom script & filters

Posted by on May 17, 2009 in Analytics

There are a few pro’s and con’s to setting up individual Google Analytics website profiles for subdomains VS using a custom script to track to track your visitors across a website’s various subdomains. This post will cover using a custom Google Analytics script to track your visitor data across various subdomains within your website. If you are concerned on how you will be able to differ between the various subdomains from your analytics data, not to worry, i will cover that further within this post.

I recently had the task of creating a new analytics account for a client, where the website had a few key subdomains, each with their own unique content, however the parent domain was simply a landing page, used to direct users to the relevant subdomains depending on the content needs. Now as the parent domain performs well within search engines (as well as the central point of contact for any navigational search), the need to track visitor data from the parent domain and then across all subdomains became a necessity.

Custom GA tracking script

After creating your analytics account and your first website profile (for your parent domain), you will need to make a small change to the GA tracking script, which you will be installing on the parent domain and across all subdomains and their pages.

setDomainName tracker for custom analytic script

In the above screenshot, we have added an additional line of code to the script (directly under the UA tracking line), pagetracker._setDomainName(“”);. This allows Google Analytics to use the same cookies across the various subdomains to track visitor data. The downside to this would be that you will not be able to differ between similar page names within the subdomains, such as and (Analytics reports only display the request URI). Adding custom filters to your profiles will however allow you to to differ between the subdomain pages.

Create additional website profiles for each of your subdomains

create website profiles in analytics for each subdomain

Creating custom filters

Now it’s time to start setting up filters for each of your subdomain website profiles. The current tracking script customisation tracks visits across all subdomains, therefore we will need to add a filter to each subdomain profile to only include traffic from that specific domain for your reports. Select the edit option associated with the subdomain profile and add a new filter. I only want to see visitor data associated with the subdomain profile i’ve created, now create a custom filter to include only from the subdomain associated with our profile.

custom filter for subdomain website profiles

As seen in the above screenshot, i am filtering all traffic to display only traffic associated with the specific subdomain website profile within the filter pattern. You will need to create a custom filter for each of the subdomain website profiles you have created within your Google Analytics account to track individual subdomain visitor data. Take note that additional subdomains can be added to the pattern filter if needed by simply separating them with a pipe (|).

Google Analytics subdomain profiles with custom filters

Finally, our parent domain profile will also require a filter, as mentioned earlier, Google Analytics only displays the request URI, therefore it will be difficult to differ between pages with the same name. For this i need to create an advanced filter for the parent domain website profile that will append the hostname to the request URI to display in the analytics reports.

Advanced filter on parent domain website profile in analytics

Ensure that the field entries for the advanced filter are entered in identically to the ones within the screenshot above. Save the filter and we are now complete. Your Analytics account setup should look similar to the diagram below.

Google Analytics website profiles for domains including filters

Now you are ready to track subdomain specific data within the individual website profiles created, as well as see an overview report from the parent domain profile for all data across the subdomains and parent domain for all your reporting needs.

5 Comments on Google Analytics subdomain tracking, custom script & filters

By Anupama Joshi on August 3, 2009 at 10:45 am

Great Post!The Sreenshots and Diagrams made it very easy to understand how to go about it. However, I am still unclear on one thing – will this work for subdomains with different UA numbers? e.g. Parent domain has the GA code with UA-12345-1 and sud-domain with UA-12345-2. What will happen in such a case?

By Brett Pringle on August 3, 2009 at 10:53 am

Hi Anupama,

Technically if you add the setDomain variable into the script and make use of the filters it should not be an issue that you have 2 different UA profile tracking numbers. As this would then allow the subdomains and parent domain to use the same cookie information. Personally i’ve never tried this tracking sub-domains with different UA codes (you’ve given different profile numbers however, which should be fine), however you should be able to gather the data needed by using the same cookie and then applying the filters to the profiles.

By Sky on May 26, 2010 at 10:01 pm

Nice post!
Easy to understand.
Thank you!

By Mike on June 16, 2010 at 3:53 pm

Hi Brett

just a few queries regarding subdomain filters:
i have setup filter for a site with

Have created a master profile and individual profile for each of the sub domains. have also applied an included filter and a sub domain filter (similar to yours) to each of the sub domains. In the reporting, i am getting errors with shown the ‘visit this link’ and also overlay links.

if you can help out, that would be awesome :)

thanks heaps in advance

By Brett Pringle on June 17, 2010 at 4:36 pm

Hi Mike,

Sounds like your analytics is “stuttering” a bit there (if i understand correctly). Possible to see your filter setup for appending the hostname to the URI?

Comment on Google Analytics subdomain tracking, custom script & filters

Previous Post

Next Post