Analytics.js: What does 'auto' argument do on tracking object creation?
Asked Answered
C

2

19

I've just switched to the new Google Analytics Universal Analytics platform, using the new javascript API - Analytics.js

In the first example code snippet from the docs a tracking object is created, then a pageview of the current page is logged using that tracking object.

ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');

Notice the 'auto' argument - I can't work out what that's there for.

I had a look at the method reference and it seems that the relevant parameter is optional and should contain configuration key/value pairs. Examples can be found in the field reference. If the parameter is optional it seems odd to pass 'auto' here unless it does something of significance, so you have to assume it does, but I can't find any mention of this.

Also, in the auto generated code in your account it does a similar thing but passes your domain

ga('create', 'UA-XXXX-Y', 'mydomain.com');

The fact your domain is specifically passed when they have it available again suggests it does something meaningful - otherwise they'd probably just use 'auto' here too. But again, there's no mention of why this is done.

Am I missing something really obvious?

Condillac answered 11/1, 2014 at 18:53 Comment(0)
S
9

Actually when I look in to code it does not pass the name of my property but the standard domain set in the configuration - which makes a lot more sense, since the parameter sets the cookie domain (in case of "auto" to the value of window.location.hostname to the hostname without subdomains). Alternatively you can pass a configuration object as third parameter (which might contain a cookie domain setting). Try and set a wrong parameter - you'll get a "no storage" error in the analytics debugger since the code cannot set a cookie.

Sabbatical answered 11/1, 2014 at 23:6 Comment(2)
Ah, thanks! Setting the cookie domain is exactly what it does. It's pretty confusing that it can be used for both that and for other configurations as well - the docs could be clearer on this. Maybe it becomes obvious why they did it that way once you start using it more?Condillac
Yeah, it's for setting the cookie and helps with tracking across sub domains. They go over in it the Google Analytics December Developer update. You can watch it here: youtube.com/watch?v=nmfUoSyFBSkDelila
E
3

If your concern is only with tracking across subdomains (ex: www.example.com, site1.example.com, site2.example.com), then it's already taken care of with this auto option in universal analytics.

Per the 'Automatic Cookie Domain Configuration' section in this devguide

Automatic Cookie Domain Configuration simplifies cross domain tracking implementations by automatically writing cookies to the highest level domain possible when the auto parameter is used. When used on the domain www.example.co.uk, it will try to write cookies in the following order:

  • co.uk
  • example.co.uk
  • www.example.co.uk

Analytics.js will fail to write a cookie on co.uk but will succeed on example.co.uk. Since a cookie was succesfully written on a higher level domain, www.example.co.uk will be skipped.

Evaevacuant answered 2/12, 2014 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.