Search code examples
google-analyticsuniversal-analytics

Why does Google Analytics default code remove the sub domain in ga('create'...) function and add the TLD to the Referral Exclusion List?


I want to create a few new Accounts in Google Analytics using some sub domains. Each site is its own and not related to each other except by domain and parent organisation.

Let's say I wanted to create two new Accounts for dev.example.com.au and blog.example.com.au

Previously when creating a new Account, the default code produced the following:

ga('create', 'UA-123456-7', 'dev.example.com.au');

Now it produces the following:

ga('create', 'UA-123456-7', 'example.com.au');

And it automatically adds example.com.au to the Referral Exclusion List.

I had a look at Method Reference, Advanced Configuration and Analytics.js Field Reference but I don't really understand what the third parameter (the domain) in the default code is doing or what impact is has by leaving it as 'example.com.au' or changing back to 'dev.example.com.au'. The code examples either use 'auto' as the value or an object with several properties. Should I change the tracking code to reference the sub domain in the ga() function?

It makes sense that I wouldn't want to see referral traffic from dev.example.com.au to dev.example.com.au but I would want to see the traffic from example.com.au to any of the sub domains, so why is example.com.au automatically added to the referral list?

For reference the full default GA code snippet is below - second last line in particular:

<script>
  (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-123456-7', 'example.com.au');
  ga('send', 'pageview');

</script>

Solution

  • The third parameter is the cookie domain. Ga stores a client id in a single cookie to recognize users during a session or recurring users over multiple sessions. Cookies are domain specific and the cookie domain tells GA at which domain the code is allowed to set cookies. You can also set it to auto (or do not set it all, in which case it defaults to auto) in which case it defaults to the highest writable part of the domain that the currently displayed site runs on (i.e. with dev.example.com.au you cannot set a cookie at .au or com.au but at example.com.au). If the cookie domain is set to the top level domain example.com.au the subdomains can read the cookies, however if it set to a subdomain the top level domain (ir other subdomains) cannot read or write the cook. I.e. if your cookie domain is set to dev.example.com.au and you place that code at example.com.au Google Analytics will fail to work.

    example.com.au is added to the referral exclusion list to avoid self-referrals in the traffic sources report. If you switch between sub- and top level domains your own site might show up as referrer, the referral exclusion list prevents that from happening. The settings is more important in cross domain tracking (where you have to add all domains tracked by the GA propery so they won't show up as referrals to each other and to allow for proper campaign attribution).