what are the values in _ga cookie?

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?

 

回答1

_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.

 

回答2

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;
}

 

回答3

Written in NodeJS with ES6 Syntax. Might help someone?

// Example: GA1.2.494614159.1574329064
const gaCookieGeneration = ({ version = 1, domain, rootpath = '/' }) => {
  const subdomains = (domain.match(/./) || []).length + 1;
  const rootpathDirs = (rootpath.match(/\//) || []).length;
  const cookiePath = rootpathDirs > 1 ? `-${rootpathDirs}` : '';
  const uniqueId = Math.random().toString().substr(2, 9);
  const timeStamp = (+new Date()).toString().substr(0, 10);
  return `GA${version}.${subdomains}${cookiePath}.${uniqueId}.${timeStamp}`;
};
const gaCookie = gaCookieGeneration({
  domain: '.example.com',
});

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-07-20 The framework 'Microsoft.WindowsDesktop.App', version '5.0.0' was not found
2020-07-20 Detailed ASP.NET MVC Pipeline
2018-07-20 Map dependencies with code maps
2016-07-20 CurlSharp
2015-07-20 git推送本地分支到远端 以及删除远端分支的 命令
2015-07-20 git撤销提交到remote的commit
2014-07-20 C#中的异步编程模式
点击右上角即可分享
微信分享提示