Google Analytics SubDomains Tracking Part One

Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+0Digg thisEmail this to someone

So I have recently been setting up a rather large implementation of Google Analytics as part of a holistic SEO strategy and had the joy of doing this over a site with a large number of sub domains. There are a number of things to keep in mind when implementing Google Analytics tracking for sub domains:

The basic setup of this site is

What makes this an even more interesting implementation is the conversion process. A user can search for a product on the sub domain sites but is then redirected back to the conversion steps on the main site:

– User searches for product on
– User is referred to
– User goes through steps to

I also had a problem where by the steps on the conversion process were dynamic URLs, so it was a little more complicated to create goals.

Set up our Sub domain Tracking and Funnels

To Do

1. setDomainName: First you need to add the following tracking code to all pages of your subdomains and main domains.

<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-XXXXXX-X”);
} catch(err) {}

2. Virtual Pageviews: You can then setup virtual pageviews for steps in your conversion process that have dynamic URLs


NOTE: Be careful with the above. If you have trackPageview twice on the same page, your referral will be reset and all conversions will show as direct.

3. You need to set up an advanced filter to attach the sub domain to the front of each page. If you don’t do this and you have the same page on both your main site and sub domains, you will not be able to distinguish between either i.e.

will just appear as /page_1

So the custom advanced filter will be as follows:

Field A -> Extract A : Hostname : (.*)
Field B -> Extract B : Request URI : (.*)
Output To -> Constructor : Request URI : /$A1$B1
Field A Required – Yes
Field B Required – No
Override Output Field – Yes
Case Sensitive No

This will now make your sub domain pages appear as

4. Trying to break down the funnels by sub domain proved another obstacle altogether as each sub domain refers back into the main sites conversion process. So for example I have one funnel setup to track the conversion process as follows:

– Step 1 (On main site – required step)
– Step 2 (On main site)
– Confirm Booking (On main site)
– Thank You Page (On main site)

But to break this funnel down by sub domain you need to be a little more inventive. You can do a lot of different things:

a. Create Advanced Segments to do the following
– Track users who landed on a particular page (your sub domain) and went on to convert (using the Ecommerce Affiliation field).

b. Create a funnel using the advanced filter used above. This will allow you to create goal steps as follows:

– The first required step will check to see if a user is on any page of your sub domain (as a regexp) i.e. /subdomain_1\.mainsite\.com/.*
– Step 1 (On main site – required step)
– Step 2 (On main site)
– Confirm Booking (On main site)
– Thank You Page (On main site)

This is not the whole Google Analytics story on sub domains. There is the case of tracking between multiple domains with sub domains and also tracking conversions when users are redirected to a third party shopping cart. I will add these in Part 2.

Any questions on the above .. please ask away …

About Kieran Flanagan

Online Marketer who implements both inbound and paid strategies to help companies grow internationally. Lover of content marketing, SEO, analytics, CRO and strategy. A highly motivated marketing geek high on data crack.

  • Oscar

    Hi I loved your post and I think it is helping me along the way. In terms of getting my subdomains to register in my “top content” reports.

    My funnels are not working properly however 🙁

    But I am running into an issue and thought that maybe you can “spell it out” for me.

    I want to track from any page on a subdomain (so i know the user came in through the sub domain) to the main site….

    so like:

    I have tried a bunch of things and for some reason it never registers the in any of my goals.

    Can you shed some light on this?


  • Hi Oscar

    Do you have your tracking code setup on all pages as follows:

    With the setDomainName used. This will ensure all sub domains are counted as part of your main site.

    Let me know if this helps



  • Sorry Oscar the code I pasted above didn’t get shown. You can see in my post above, I give an example of how the tracking code should look on each page. Under point 1.

  • Tim Day

    I have a quick question in terms of this. We’re hosting one of our sub-domains as, with the extra “www” preceding the subdomain, and for some reason, this site isn’t being tracked. All the code is set up as described above, and one one profile where I limit it to just the subdomain it comes through fine, but not the profile set to track all subdomains. Any ideas why?


  • Tim Day

    Please ignore that last question – I had a typo in my filter constructor. New to this!

  • No problem Tim, let me know if you ever need a fresh pair of eyes to look at anything

  • gil_34

    I am desperate with this filter. Our agancy implemented the additional line pageTracker._setDomainName(“”); to the tracking code but still after integrating the filter we have strange reports. Some sites are shown with domain names and some are not. And is is right that pages with domains appear with / before in analytics like this: / (this is the case when analytics shows the domain)

    Maybe there is something wrong within the tracking code?
    The tracking code in our main domain looks like this:

    <script type=”text/javascript”>
    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(unescape(“%3Cscript src='” + gaJsHost + “' type='text/javascript'%3E%3C/script%3E”));

    <script type=”text/javascript”>
    var pageTracker = _gat._getTracker(“UA-xxxxxx-1”);
    pageTracker._setDomainName (“”);

    I can see that there is no “.” and a space after setDomainDame (…) Could that be the problem? Becaus the code on the sites of our subdomain looks just fine like this:

    <script type=”text/javascript”>
    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”); document.write(unescape(“%3Cscript src='” + gaJsHost + “' type='text/javascript'%3E%3C/script%3E”));
    <script type=”text/javascript”>
    try {
    var pageTracker = _gat._getTracker(“UA-xxxxxx-1”);
    catch (err) { }

    Is the difference with this “.” and no”.” the problem? The filter was implemented just as described.

    Thank you so much for help!

  • searchbrat

    Hi Gil

    Yes, you should have the *.* consistent in all places.

    Also, you should have a filter running that appends the domain name onto the URL. By default Google will show the following:


    You want to change this so it shows as

    Without seeing your site it's difficult to give a definitive answer as to what is wrong. If you want to send me the details privately (via Contact), I will be happy to take a look ?


  • Mitch Allen

    I'm also having a problem where subdomains show up with slash in front of them. For example: / – then when I click on them in GA, it tries to open: mydomain/

    Here is what I have in the header (built by a WordPress plugin – domain names and UA ids changed to protect the innocent. 🙂 ):

    <script type=”text/javascript”>// // Google Analytics for WordPress by Yoast v4.06 |'],['_trackPageview']);
    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl&#39; : 'http://www&#39😉 + '.

    Hey Mitch

    When you say “click on them in GA” do you mean when trying to use “In Page Analytics” i.e. within Google Analytics itself.