Subdomain Tracking Revisited – Updates & A Closer Look at Code

Topics: Analytics

Since my initial post on subdomain tracking in Google Analytics, I’ve had a lot of questions about the actual code being used, and the code itself is now also sorely out of date now that it’s June 2012. Let me first issue a disclaimer that focus in my original post was not the code, but instead to provide SCREENSHOTS that guided users through a labyrinth of screens and fields in Google Analytics.

So without further ado, let’s take a look at rip apart the original bolded code I suggested:

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

1. _gaq.push(['_setAccount', 'UA-#########-1']);

This is where you stipulate what your GA account is. Pretty simple. Replace ##### with the actual numbers of your account.

2. _gaq.push(['_setAllowLinker', true]);

Okay, so _setAllowLinker() is most often used for eCommerce conversion tracking, in particular, when a submission form takes users to a 3rd party shopping cart on a different domain. It allows you to keep tracking that visitor’s cookie back and forth between the shopping cart and your main site.

As Google explains here, you also need to _linkByPost() on the web form that takes your users to the shopping cart domain. That way, cookies will be read from that POST data rather than from the regular user session information. And if the cart links back to the website again, you also need to use the _link() function on those outgoing links to transfer the cookie data back over.

Do you need this extra bit of code if all you want to do is track between your main domain and a subdomain? NOPE. We included because we had a lot of clients that needed the capability to track both subdomains as well as other domains. In retrospect, this created confusion in our blog post geared towards subdomains, and was something we probably didn’t need to include for that particular post.

3. _gaq.push(['_setAllowHash', false]);

This line of code turns off hashing. Come again? Hashing is where Google Analytics creates a unique ID for a user based on the domain name that helped maintain cookie integrity. Sounds great, but what if users moved from one domain to another – but still had old cookies from a previous visit stored, before the code was updated? Confusion ensued, and the new domain could reject the user’s cookie.

But GUESS WHAT? As of this blog post, hashing is now deprecated. There’s no need to include this line of code at all anymore.

Which is great, because a wise commenter on our original blog also noted that turning off hashing is bad in the first place and was only needed for cross-domain tracking (not subdomain tracking), as this informative post from ROI Revolution likewise notes.

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

This looks pretty self-explanatory: it’s where you set your domain name.

However, let’s take a closer look at the PERIOD before the leading domain. Google had previously recommended placing a period here because this clearly defined the main domain from any subdomains, allowing usage of lower level subdomains as well.

However, the period is not necessary for 99% of sites (who uses lower level subdomains?), and it’s better to leave it off because it can create further complication when tracking between subdomains.

5. _gaq.push(['_trackPageview']);
This line of code isn’t related to subdomains at all, but it’s useful to have in there anyway because it makes it that much easier for you to configure event tracking. It’s something we tend to put on most client sites.

6. gaq.push(['_trackPageLoadTime']);

_trackPageLoadTime also isn’t related to subdomains, but allows used to allow you to enable site speed reporting for pages, but now Google does this automatically.

**********

DEEP BREATH. So I’ve run through the old code and worked through all of the confusing and/or out of date bits. Without further ado, here is new code you can use to enable subdomain tracking. I heavily encourage you to check out Google’s official instructions for subdomain tracking and analytics in general, as there are a myriad of ways your tracking code can be customized.

<script type=”text/javascript”>

var _gaq = _gaq || [];

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

What we’ve done here is deleted out-of-date code, removed lines that were related to cross-domain tracking, and added IN another line of code: _addIgnoredRef. This beautiful little line of code allows you to stipulate a domain you’d like to ignore referrals from, and helps prevent further confusion when tracking between subdomains.

Hope this helps!

27 Comments

  1. This is a great and timely update! Is the information in your last post that pertains to setting up Profiles still relevant? I’m trying to do this for the first time navigating around the new GA and it’s hard not to feel lost.

    Posted by Catherine G, July 5, 2012 Reply

    • Hi Catherine,

      Yes, the other post should still do the trick of guiding you through the GA interface. The only thing that I’ve changed is just my recommendation of the actual code to use. Hope that helps!

      Posted by Sarah, July 5, 2012

  2. Great article and very helpful. I can now take this code and place it on the primary domain and each of the subdomains.

    One question…, as I read up on tracking subdomains with this single bit of code, I continually see information about needing to create filters to track each of the subdomains separately.

    Can you please shed some light on if I need to do this or not. If not, what is the best way to track the data for the subdomains as individual entities while still using this single tracking code.

    Posted by Steve S, August 1, 2012 Reply

  3. Hi Steve,

    Yes – you do need to apply filters to different profiles that you set up. Check out this other post: http://www.bigdaylight.com/analytics/filtering-subdomains-in-google-analytics/

    Hope that helps!

    Posted by Sarah, August 1, 2012 Reply

    • Hi Sarah,

      Thanks for the quick response. Ya know.. I had already seen that post on your site but it was showing screen grabs from the old Analytics interface.

      Not sure how to do the same thing within the newest Analytics interface. Thoughts?

      Posted by Steve S, August 1, 2012

    • Steve – the interface works in a very similar way. But I will work on updating the blog posts with screenshots soon!

      Posted by Sarah, August 3, 2012

    • UPDATE: As of August 14, the original post with screenshots has been updated: http://www.bigdaylight.com/analytics/filtering-subdomains-in-google-analytics/

      Posted by Sarah, August 13, 2012

  4. great Article Sarah, I am working on a confusing problem like this one too now..
    I wanna ask,
    this below script,

    var _gaq = _gaq || [];

    _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);
    })();

    where should I put it into? my main domain? or my sub domain? or both?
    thanks in advance… :)

    Posted by Jeff, August 2, 2012 Reply

    • Hi Jeff,

      You want to put this exact same code on every page of all sites, main domain and subdomain.

      Posted by Sarah, August 3, 2012

  5. Why set _gaq.push(['_addIgnoredRef', 'yourdomain.com']); ? I think it is interesting which subdomain receives traffic from which subdomain.

    And why is the . removed from _gaq.push(['_setDomainName','.yourdomain.com']); when you use

    a.domain.com
    b.domain.com

    Posted by Stef, September 26, 2012 Reply

  6. Hi Stef,
    You don’t have to configure it that way, but remember that you want to see which subdomain referred which, that then your visitor counts could become inflated each time someone moves back and forth between the different domains. You could end up with a ton of self-referrals, and not be able to fully track user paths across both sites in one session. But if you WANT it to be that way, that’s fine.

    The leading period causes COOKIE RESETS- as explained above, “let’s take a closer look at the PERIOD before the leading domain. Google had previously recommended placing a period here because this clearly defined the main domain from any subdomains, allowing usage of lower level subdomains as well.

    However, the period is not necessary for 99% of sites (who uses lower level subdomains?), and it’s better to leave it off because it can create further complication when tracking between subdomains.” Unless you are trying to track something like a.b.subdomain.com or b.b.subdomain.com (etc, etc) you don’t need to include the period.

    Posted by Sarah, September 27, 2012 Reply

    • Thanks for your answer :) very clear. Will make the changes. To bad I have many subdomains (countries up to 100). Is there a workaround for it?

      Can’t it be fixed bij advanced segments or Custom Reports? what is wise?

      Posted by Stef, October 4, 2012

  7. The next alternative would be to use Advanced Segments. I would try that before a Custom Report, because that way you can still segment traffic no matter which part of Google Analytics you are browsing around to.

    Posted by Sarah, October 4, 2012 Reply

    • Thanks I’m working on it, last question now :). In my profile I have the /$A1$B1 filter. But I also want to use segments this (www\.|^)yourdomain\.com. For some reason it mix up different subdomains and the main domain through the reports. Does it have something to do with the filter that shows full url’s?

      Posted by Stef, October 5, 2012

  8. Hi Sarah I really hope you can help I’m having terrible trouble trying to track two websites. Site one is http://www.mary.co.uk and Site two is mary.allocate.co.uk. Both sites use the same UA ID.

    One site http://www.mary,co.uk one I have this code:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
    _gaq.push(['_setDomainName', 'mary.co.uk']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_setAllowHash', false]);
    _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);
    })();

    And on Site two mary.allocate.co.uk I have

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
    _gaq.push([‘_setDomainName’, ‘.allocate.co.uk’]);
    _gaq.push([‘_setAllowLinker’, true]);
    _gaq.push([‘_setAllowHash’, false]);
    _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);
    })();

    What do I need to do in order to be able to have one profile showing only the data that for mary.allocate.co.uk and not both http://www.mary.co.uk and mary.allocate.co.uk?

    Many thanks
    Lorraine

    Posted by Lorraine, October 23, 2012 Reply

  9. Hi Sarah,

    Great posts, both of them (/analytics/filtering-subdomains-in-google-analytics/), but I’ve run into a problem and I hope you can help me with it!

    First question:
    I followed your instructions to a T, and made one assumption, but I’m not getting any tracking in my subdomain profile. This is what I put under Profile Settings -> Website’s URL -> subdomain.mysite.com. Am I supposed to put just http://www.mysite.com? Hopefully this fixes my tracking issue for this particular profile.

    Second question:
    When I look at Site Content -> All Pages I’m getting combined URLs, these are the only 2 URLs showing (no others):
    http://www.mysite.comwww.mysite.com
    subdomain.mysite.comsubdomain.mysite.com
    I followed the directions for the filters to show the Full URLs, but this isn’t how it’s supposed to look, right?
    Field A -> Extract A: Hostname (.*)
    Field B -> Extract B: Hostname (.*)
    Output To -> Constructor: Request URI $A1$B1
    Yes
    No
    Yes
    No

    Thanks in advance!!!

    Posted by Timothy, October 31, 2012 Reply

    • Try entering in /$A1$B1 instead of only $A1$B1. As for your first question, it’s a little hard to understand what you are asking, but I would try taking a closer look at some of the screenshots!

      Posted by Sarah, November 2, 2012

  10. I forgot to put this in my previous question field, do you also need to put link() or linkBy() on all links to the subdomain?

    Thanks again!

    Posted by Timothy, October 31, 2012 Reply

  11. Hi Sarah,

    Great post, one note that I would like to add is that if a webmaster currently has a . in front of _gaq.push([‘_setDomainName’ then they should continue to use it. Deleting this period will destroy your existing cookies and reset all of the cookie historic data.

    Hope that helps,
    Phil

    Posted by Phil Caines, November 19, 2012 Reply

  12. I am currently running Analytics on a site that has years of historical data. The code has been set like this for some time now:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
    _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);
    })();

    By adding the single line of code so we can begin tracking subdomains:

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

    is there any possibility of losing our historical data? Before making this change, I need to be 100% certain that there will be no loss of historical data. Thanks in advance!

    Mike

    Posted by Mike Snyder, December 20, 2012 Reply

    • Hi Mike,

      There is no way to ever really “lose” historical data unless you completely delete your Google Analytics account so you can never log back into it again. Even if you were to wildly mess up the tracking code, all the historical data would still be there. The thing that would get messed up is your NEW DATA, moving FORWARD. :)

      I’d just be sure to follow instructions (http://www.bigdaylight.com/analytics/filtering-subdomains-in-google-analytics) and double-check everything before putting into place, and then keep a close eye on performance so if there are any issues, you can immediately address them. You should know within a day if everything looks okay. I have on occasion even set up tracking for testing domains, just to be sure I can test code out on a domain that isn’t “important” before finally putting it live.

      Last but not least: when making changes to setting *INSIDE* Google Analytics (such as filteres, etc), always set up a new profile to apply changes too and test things out. Always try to have at least one profile that has no filters or any special settings applied to it, where the data is totally raw/clean.

      Posted by Sarah, December 20, 2012

  13. Hey Sarah, Great post..I think creating a MEGA post of generating code + filter would be good :) just a suggestion.. anyways this post is useful..

    Posted by Search Ramble, December 26, 2012 Reply

  14. Hi Sarah,

    thanks a lot for your precious help on this matter.
    Your post is very clear and helpful.

    I have a question for you : how do you deal with international website with various country extensions ?
    Does the SetDomain (ex: domain.COM) works with domain.ES, domain.FR or domain.DE ?

    thks !

    Julien

    Posted by Julien, July 22, 2013 Reply

    • Hi Julien,

      Instead of subdomain tracking, you will want to enable cross-domain tracking, which associates completely different domains under the same Google Analytics account. That is a separate type of configuration, but I did find this Moz article with some additional tips: http://moz.com/ugc/google-analytics-cross-domain-tracking-made-easy-14596

      Posted by Sarah, July 23, 2013

  15. Hi Sarah,
    You are awesome. Do you have a boyfriend :) JKing. If you don’t mind I have a bit of a conundrum.
    I have three profile views for one account:
    1. ALL Data (includes both #2 and #3)
    2. Subdomain Data (This subdomain is not LIVE or collecting data yet)
    3. Main www. Data

    The metrics for #1 and #3 seem to be incorrect. Since applying the subdomain tracking the unique visits for my pages are very different. I assume since I am not yet tracking #2 that #1 and #3 should show the same results?

    How do I figure out where the extra “visitors” are coming from, or is there an error with the code? (I think the developers have other subdomains, they use. But, I’m not tracking those.)

    Subsequently, my “code” doesn’t look like yours. Mine is like this: (view below) Why is that? Is this the newest GA code available?

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,'//www.google-analytics.com/analytics.js’,'ga’);

    ga(‘create’, ‘UA-XXXXXXXX-1′, ‘domain.com’);
    ga(‘send’, ‘pageview’);

    Posted by Ravey, October 2, 2013 Reply

Leave a Comment (*Required Fields).

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