I'm cleaning up the Google Analytics tracking for a complex corporate website.

The website is spread across a bunch of subdomains (www.example.com for the main content, dev.example.com for developer information, pages.example.com for legacy content, and so on) and I've found that each subdomain uses its own tracking cookie, so Analytics treats them all as separate sites (when tracking referrals, for example) even though the data all feeds into the same web property.

As Google recommends, I'm going to fix this by adding _gaq.push(['_setDomainName', 'example.com']); to the tracking code on each subdomain, so that they all use the same tracking cookie. However, the site is very complex and several different groups maintain their own subdomains, so it's going to be difficult to coordinate the switchover.

In particular, this comment gives me pause:

Yes you have to use setDomainName(".mydomain.com") on all www pages as well. Otherwise cookies might get overwritten everytime you change subdomains and data loss will occur.

So, given that:

  • the bare domain (example.com) just redirects to the www subdomain, and
  • some of the subdomains currently have the _setDomainName method explicitly set to 'none', but some don't have it set at all,

do we risk any data corruption if we stagger the tracking code update across the subdomains?

1 Answer 1


This relates to how cookies are shared across sub-domains. A cookie set on your main site with the domain www.example.com can't be accessed by pages.example.com, however a cookie set with the domain example.com can be accessed by both. It doesn't matter that example.com just redirects to your main site, your main site can still use example.com as the domain with cookies that it sets (as www.example.com is a subdomain of example.com). Sounds more complicated than it is!

So, I'd suggest:

  • Modify the tracking code on your main domain first (to add the _gaq.push(['_setDomainName', 'example.com']);
  • Then modify your sub-domains as and when you can

If you don't do this, then someone who arrives at www.example.com and then clicked on a link to pages.example.com would be registered as two visitors, because Google Analytics wouldn't be able to track that they're the same person. So that's what the comment means by "data corruption".

  • Thank you! I still didn't quite understand how Analytics chose which domain to set its cookies on, so I cleared the site's cookies in my browser and watched them repopulate as I visited various subdomains. Apparently, all the subdomains except www, whether _setDomainName is explicitly set to none or left as the default, set their cookies on their full domain (for example, .pages.example.com or .dev.example.com). Only www set its cookies on .example.com, which supports what you say about its cookies being the only ones at risk of being overwritten. Jun 14, 2013 at 17:33
  • Also, out of curiosity, do you know how Google Analytics knows that www is the "main" subdomain and should set its cookies on the domain? Is it because www.example.com is set as the web property's default URL in Analytics, or is it just a fundamental assumption of the web? Jun 14, 2013 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.