[筆記] Universal Analytics and Cookies


原文摘錄自:http://www.optimizesmart.com/google-analytics-cookies-ultimate-guide/

Universal Analytics and Cookies

Q1. What types of cookies are set by universal analytics?
Ans.  Universal Analytics set a single first party cookie called ‘_ga’

Q2. Why universal analytics need cookie?
Universal Analytics (UA) does not really need any cookie to collect data. It can collect data even without any cookie through analytics.js JavaScript library and through the measurement protocol.

Q3. Why analytics.js uses only one cookie?
In order to significantly reduce the number and size of cookies that are sent.

Q4. What is _ga cookie?
It is the only cookie used by UA to store information about visitors, sessions and campaigns.

Q5. When _ga cookie is created?
As soon as you visit a website on which Universal Analytics tracking code is installed.

Q6. How you can create _ga cookie?
By creating a tracking object through ‘create’ command.
Syntax: ga(‘create’, ‘UA-XXXX-Y’);

Q7. What are the various attributes of _ga cookie?
_ga cookie has got following attributes:
  1. Cookie name – which is obviously ‘_ga’ by default.
  2. Cookie domain – it is the domain at which the cookie is set up. By default the _ga cookie is set on the top level domain. So if you have set cookie at top level domain like www.optimizesmart.com then the cookie domain would be ‘optimizesmart.com’.  (without ‘www.’ Prefix)
  3. Cookie Path – it is the path at which the cookie is set up. By default the _ga cookie is set on the top level domain with the root level (/) path.
  4. Cookie Expires – It is the time in seconds after which the _ga cookie will expire. By default _ga cookie expires after 2 years from the data it was last refreshed.

Q8. In case of the domains www.example.com, fun.example.com, more.fun.example.com which is the highest domain level (or top level domain)?
 It is ‘example.com’
‘www.example.com’ and ‘fun.example.com’ are the second highest domain levels and ‘more.fun.example.com’ is the third highest domain level.
Note: analytics.js cannot set up cookie at ‘.com’ level.

Q9. How you can modify _ga cookie?
By changing the attributes of _ga cookie.
For example by changing the cookie name, by changing the cookie domain, cookie path and/or cookie expiry date.
Note: Google recommends not to directly access the _ga cookie as the cookie format might change without warning which could lead to script errors and incorrect data.

Q10. How you can rename the _ga cookie to say ‘himanshu’?
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieName': ‘himanshu’,});

Q11. How you can set the _ga cookie at sub domain level like analytics.optimizesmart.com?
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘analytics.optimizesmart.com’,});

Q12. How you can end up setting an incorrect value for ‘cookie domain’?
By using a cookie domain which is not the ancestor of the current domain.
For example, if the current domain is ‘analytics.optimizesmart.com’ then you can set the cookie at ‘optimizesmart.com’ as ‘optimizesmart.com’ can be considered as an ancestor of ‘analytics.optimizesmart.com’. But you can’t set cookie on some other website say example.com.
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘optimizesmart.com’,}); // correct
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘example.com’,}); // incorrect

Q13. What happens when you set an incorrect value for ‘cookie domain’?
The _ga cookie will not set.

Q14. How you can modify the _ga cookie path from the default ‘/’ to say ‘/lib’?
ga(‘create’, ‘UA-XXXX-Y’, {‘cookiePath': ‘/lib/’});
Note: Google does not recommend changing the cookie path.

Q15. How you can set the _ga cookie to expire after 1 second?
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieExpires': 1});

Q16. What happens when you set the _ga cookie to expire after 0 second?
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieExpires': 0});
In this case the cookie turns into a session based cookie and expires once the current browser session ends.

Q17. How you can set _ga cookie on a local host?
By setting the value of ‘cookie domain’ to ‘none’. For example:
ga(‘create’, ‘UA-XXXX-Y’, ‘none’);

Q18. When you should disable _ga cookie?
When you want to send data directly to Universal Analytics servers without using any cookie.

Q19. How you can disable _ga cookie?
By disabling the cookie storage.
ga(‘create’, ‘UA-XXXX-Y’, {‘storage': ‘none’,’clientId': ‘35009a79-1a05-49d7-b876-2b884d0f825b’
});
Note: you have to supply your own clientId

Q20. How you can retrieve the clientId value?
Through ‘get’ command. For example:
ga(function(tracker) {var clientId = tracker.get(‘clientId’);});

Q21. Does deleting _ga cookie and then returning to your website means, you will be labelled as a new visitor by Universal Analytics?
There is no guarantee because UA does not need to rely only on ‘_ga’ cookie for collecting data and can collect visitor information through other methods like measurement protocol.

Q22. How you can track traffic across all sub domains in case of Universal Analytics?
By default you can track traffic across all sub domains in Universal Analytics. This is accomplished by configuring tracking objects to set cookies at the highest domain possible (which is a default setting anyways).
For example, if you have a website with 3 subdomains: one.example.com, two.example.com and three.example.com, you would configure their tracking objects as follows:
// Configuration for one.example.com
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘example.com’});

// Configuration for two.example.com
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘example.com’});

// Configuration for three.example.com
ga(‘create’, ‘UA-XXXX-Y’, {‘cookieDomain': ‘example.com’});

Q23. How you can configure tracking objects to automatically set cookies at the highest domain possible (top level domain).
By using the auto parameter.
For example, if you have a website with 3 subdomains: one.example.com, two.example.com and three.example.com, you could configure their tracking objects as follows:
// Configuration for one.example.com
ga(‘create’, ‘UA-XXXX-Y’,’auto’);

// Configuration for two.example.com
ga(‘create’, ‘UA-XXXX-Y’,’auto’);

// Configuration for three.example.com
ga(‘create’, ‘UA-XXXX-Y’,’auto’);

Q24. How _ga cookie is able to work for all sub domains of your website?
This is because by default _ga cookie is set at the top level domain.

Q25. How Universal Analytics define visitor as unique?
UA assign unique ID to each visitor on your website. This ID is called the ‘user ID’ (or client ID). The user id is made up of random unique ID and the first time stamp.

Q26. What is the biggest drawback of _ga cookie?
You can’t dig out session or campaign specific data from _ga cookie as all of these calculations happen in the backend on UA servers.

Q27. How you can view the _ga cookie set by Universal Analytics when you visit a website and how you can interpret it?
The process of viewing the cookie is same as it is in case of GA. You can view the cookies through chrome advanced settings:
universal analytics cookie

_ga cookie is made up of 4 fields:
ga cookie
First field is version number (GA1). It seems to be fixed at the moment and may change if the cookies format changes in the future
The second field is number of components at the domain separated by dot. By default the _ga cookie is set on the top level domain with the root level (/) path.
So if you have set cookie at top level domain like optimizesmart.com then the second field would be 2 as there are 2 components separated by dot. One component is ‘optimizesmart’ and the second component is ‘com’
If you have set cookie at sub domain level like analytics.optimizesmart.com then the second field would be 3 as there are now 3 components separated by dot. First component is ‘analytics’. Second component is ‘optimizesmart’ and the third component is ‘com’
The third field is random unique ID (randomly generated number).
The fourth field is first time stamp i.e. the time when the cookie was first for the user.