Subdomain Tracking Revisited - A Closer Look at Google Analytics Code
Back to Mingle

Subdomain Tracking Revisited – Updates & A Closer Look at Code

Posted in Search & Social — June 28, 2012

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!

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

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

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

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

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

Leave a Reply

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