More on setDomainName, sub-domains, and internal referrals

Subscribe to our monthly newsletter to get the latest updates in your inbox

A recent forum discussion I was involved in raised a great question about just how Google Analytics goes about setting its traffic source cookie incorrectly when you have sub-domains and have followed the steps for tracking sub-domains properly in your page tags.  I wrote out a quick reply on the forum and decided it would be good fodder for the blog, so I've brought some of it over here for your reading enjoyment. The discussion I was involved in reference an article on the fantastic ROI Revolution blog about internal referrals in Google Analytics.  The point raised in question was this:  

If your site has no subdomains the Google Analytics Tracking Code is able to detect when a visitor's session has expired between pageviews and avoid overwriting their existing referral information with a self-referral or internal-referral from your own site.

That safe guard is removed, however, when you have subdomains, even if your code has the standard subdomain tracking code. This can result in a rather high percentage of self referrals, even though it seems like you've done everything right.

  My response: a bit of light on the very murky details of how the Google Analytics ga.js goes about setting referring site information.
The routines within ga.js for updating the __utmz cookie are based on two factors:
  1. Detecting when there is a document.referrer that does not match the current document.domain
  2. When a new session starts
In a trans-sub-domain scenario there is a document.referrer, this the referral evaluation routines get kicked into gear.  If the session has ended naturally, or if it has died an untimely death by any of the means we are all too familiar with (bad tag settings), then a new session will be started and the domain of the document.referrer will be set into utmz. Even when proper _setDomainName settings are in use you'll see the effect of this most often due to session expiration.  A user sites on for 40 minutes, then clicks a link to where ga.js runs and detects a difference in document.referrer and document.domain.  Since there no active session, a new session is started and utmz is updated accordingly. The really nasty secret is that this can get all kinds of broken when other scripts modify the document.domain property, which several known tracking systems do.  IMO ga.js should not use document.domain but rather the safer location.hostname, but it doesn't.  If a script on forces document.domain to "" then session timeouts will result in "internal referrals" when going from to  Throw some sub-domains into the mix and things get even worse. Hope this provides some clarity on the issues surrounding internal referrals, sub-domain tracking, and using  _setDomainName in Google Analytics! For many cases I've found using _addIgnoredRef() doesn't solve the problems.  In those cases, there's a heavy-handed work-around using _setReferrerOverride() that I've used with great success... but it's late and I can't get into that right now.  Next time!