Search code examples
javascriptdnsgoogle-analyticssubdomainreferrals

Google Analytics counts visits for all my subdomains but lists my subdomains as referrals


I have a site which consists of mydomain.com (and URLs derived from it such as mydomain.com/the/rest/) and some subdomains like first.mydomain.com and second.mydomain.com. Right now, I'm using the same Analytics' snippet of code in all of the site's pages.

The problem is Google Analytics shows me mydomain.com and .mydomain.com as referrals, but it seems the visits are counted for all.

So I guess I have two options, none of them I know how to achieve:

  1. Make Analytics understand that mydomain.com and all the .mydomain.com are all part of the same site, so it doesn't count links between them as referrals.

  2. Isolate mydomain.com and all the .mydomain.com so I can have stats for each of them separately.

How can I achieve each of those options and which one you think is more appropriate?

Thank you.


Solution

  • Google Analytics, unless told otherwise, sets its cookies on the document.domain level. ie, it thinks that www.foo.com and sub.foo.com are completely separate entities, for all intents and purposes. While this seems odd, think about co.uk domains or services that sell services at the subdomain.

    As a result, the cookies set on www.foo.com aren't visible once you land on sub.foo.com, because they're not set at the right domain. So, Google Analytics says "this is a brand new visit! And they're being referred from www.foo.com

    So, there are 2 (or 3) solutions.

    • (Best) Implement cross-subdomain tracking. Basically, instruct GA to override the default domain settings. You do this with the _setDomainName directive, which you need to declare before your _trackPageview calls on all domains and subdomains. This will solve the cross domain tracking issue.

    That looks like this:

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

    Other less ideal but workable alternatives:

    • You could give them entirely different tracking codes. ie, tracking foo.com and sub.foo.com using different accounts.

    • You could keep them on the same account and create separate filters (filtering by Hostname, in profile settings, for each). This will allow you to separate the data, but will not solve your self-referral issue.

    Your best choice is #1. It will totally resolve the self referral issue.