Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the values in _ga cookie?

I am using universal analytics. universal analytics creates first party cookie _ga

 _ga=1.2.286403989.1366364567; 

286403989 is clientId

1366364567 is timestamp

what is 1 and 2 in _ga cookie?

like image 655
karthick Avatar asked Apr 19 '13 10:04

karthick


People also ask

What is _GA in cookie?

#1 _ga cookie – used to identify unique users and expires after two years. #2 _gat cookie – used to throttle the request rate, and it expires after one minute.

What is _GID and _GA?

And as i know, the definition of them: _ga: used to identify unique users and it expires after 2 years. _gid: used to identify unique users and it expires after 24 hours.

What is _gcl_au cookie?

First-party cookie. _gcl_au. The People's Pension website. Used by Google AdSense for experimenting with advertisement efficiency across websites using their services. Expires after 3 months.


2 Answers

_ga=1.2.286403989.1366364567; 

1st Field

This is a versioning number. In case the cookie format changes in the future. Seems to be fixed at 1 at the moment. The one above is an old format. Newer cookies have this value set at "GA1"

2nd Field

This field is used to figure out the correct cookie in case multiple cookies are setup in different paths or domains.

By default cookie are setup at path / and at the domain on document.location.hostname (with the www. prefix removed).

You could have a _ga cookie set at sub.example.com and another cookie set at example.com. Because the way the cookie API on browsers works there's no way to tell which is the correct cookie you use.

So the second number is the number of components (dot separated) at the domain.

  • for sub.example.com the number would be 3
  • for example.com the number would be 2

The path defaults to / but you can also change it by passing the cookiePath option to the ga.create method. If you pass it this field becomes 2 numbers dash separated. And the second number is the number slashes in the path.

Using these numbers the analytics.js script can correctly identify the cookie to be used in case there are multiple cookies set.

eg: Imagine that you have a site that lives at sub1.sub2.example.com/folder1 in case you want to store the cookie only on your site and not make it visible to other subdomains or folders you can use the following configs:

ga('create', 'UA-XXXX-Y', {   'cookiePath': '/folder1/',   'cookieDomain': 'sub1.sub2.example.com' }); 

In this case the cookie will look somoething like this;

_ga=1.4-2.XXXXXXXX.YYYYYYY 

3rd Field

This is a random generated user ID. Used to identify different users.

4th Field

It's a timestamp of the first time the cookie was set for that user.

new Date(1366364567*1000) > Fri Apr 19 2013 06:42:47 GMT-0300 (BRT) 

This is also used to uniquely identify users in case of userId collisions.

Worth mentioning that a cookie is not an API. In the future it may completely change. Google doesn't recommend reading/writing the _ga cookie directly. You should interact with Google Analytics through one of the tracking libraries such as analytics.js. There's not a lot of use for this information other than curiosity.

If you are reading/writing directly the cookie you are doing it wrong.

like image 148
Eduardo Avatar answered Oct 01 '22 06:10

Eduardo


I think this would be helpful.

/**  * Get Google Analytics UID  * @return int  */ public function getGAUID() {     $uid = 0;     if ($_COOKIE['__utma'])         list($hash_domain, $uid, $first_visit, $prew_visit, $time_start, $num_visits) = sscanf($_COOKIE['__utma'], '%d.%d.%d.%d.%d.%d');     elseif ($_COOKIE['_ga'])         list($c_format, $c_domain, $uid, $first_visit) = sscanf($_COOKIE['_ga'], 'GA%d.%d.%d.%d');      return $uid; } 
like image 41
dobs Avatar answered Oct 01 '22 05:10

dobs