如何获取Google Analytics(分析)客户端ID

时间:2022-05-15 14:32:39

When you are creating a new instance of analytics.js by running

当您通过运行创建analytics.js的新实例时

ga('create', 'UA-XXXXXXX-Y', {'cookieDomain': 'none'});

ga('create','UA-XXXXXXX-Y',{'cookieDomain':'none'});

GA creates a unique client Id. I want to fetch this id and use it for my own purposes, but I can find only setter for this parameter but can't find any getter method to get it.

GA创建一个唯一的客户端ID。我想获取此ID并将其用于我自己的目的,但我只能找到这个参数的setter,但找不到任何getter方法来获取它。

GA send it later in a parameter called &cid=123123.232323

GA稍后在名为&cid = 123123.232323的参数中发送它

Does anyone knows how do I get it?

有谁知道我怎么得到它?

3 个解决方案

#1


57  

Google does have some documentation on getting the client id.

谷歌确实有一些关于获取客户端ID的文档。

Looks like this:

看起来像这样:

ga(function(tracker) {
  var clientId = tracker.get('clientId');
});

I've used this before, too:

我以前也用过这个:

ga.getAll()[0].get('clientId');

EDIT: If you have more than one tracker on the page, it may be probable that at index 0 there is not the one you want, so an alternative function should be the following:

编辑:如果页面上有多个跟踪器,则索引0可能不存在您想要的跟踪器,因此备用函数应如下所示:

function() {
  try {
    var trackers = ga.getAll();
    var i, len;
    for (i = 0, len = trackers.length; i < len; i += 1) {
      if (trackers[i].get('trackingId') === "ID-PROPERTY") {
        return trackers[i].get('clientId');
      }
    }
  } catch(e) {}  
  return 'false';
}

where ID-PROPERTY is the id of your Property (i.e. UA-XXXXX-XX).

其中ID-PROPERTY是您的财产的ID(即UA-XXXXX-XX)。

#2


5  

First create the Google Analytics ga object to create a tracker object, by passing it a "Ready callback" function, then use the tracker to call other methods.

首先创建Google Analytics ga对象以创建跟踪器对象,方法是将其传递给“Ready callback”函数,然后使用跟踪器调用其他方法。

The ga() command queue provides an interface for doing almost everything you need to do with the analytics.js library.

ga()命令队列提供了一个界面,用于执行您对analytics.js库所需的几乎所有操作。

"function(tracker)" is a callback function to be invoked when the analytics library is fully loaded and ready to be interacted with. The function is invoked with the default tracker object as it first argument. If no default tracker has been created, the first argument is/will return undefined.

“function(tracker)”是在分析库完全加载并准备好与之交互时调用的回调函数。使用默认跟踪器对象作为第一个参数调用该函数。如果没有创建默认跟踪器,则第一个参数是/将返回undefined。

Note: when the callback function is invoked, all ga object methods are available for use. Including the one you want tracker.get('clientId')

注意:调用回调函数时,所有ga对象方法都可以使用。包括你想要的tracker.get('clientId')

Replace the UA-XXXXX-Y in the code below with your UA code from Google Analytics.

使用Google Analytics中的UA代码替换下面代码中的UA-XXXXX-Y。

// Queues a tracker object for creation.
ga('create', 'UA-XXXXX-Y', 'auto');

// Once the tracker has been created, log the
// client ID to the console.
ga(function(tracker) {
  console.log(tracker.get('clientId'));
  /* Your other code here */
});

Alternatively for lines 1 & 2, use the code below to create a named tracker.

或者对于第1行和第2行,请使用以下代码创建命名跟踪器。

// Queues a named tracker object for creation.
ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker');

#3


2  

Although the author explicitly said he is using Javascript, others (like me) may be searching for a way to get this information from the server-side, like PHP.

虽然作者明确表示他正在使用Javascript,但其他人(像我一样)可能正在寻找一种从服务器端获取此信息的方法,如PHP。

I found that you can easily check for the _ga cookie, which looks like:

我发现你可以轻松检查_ga cookie,它看起来像:

_ga=GA1.3.475128143.1522318100

In the exemple above, the user id is "475128143.1522318100".

在上面的例子中,用户id是“475128143.1522318100”。

So, in PHP I can fetch it quickly as:

所以,在PHP中,我可以快速获取它:

$gaUserId = preg_replace("/^.+\.(.+?\..+?)$/", "\\1", @$_COOKIE['_ga']);

You can also use Javascript to retrieve the cookie in a single line, without using ga() functions:

您还可以使用Javascript在一行中检索cookie,而不使用ga()函数:

var gaUserId = document.cookie.match(/_ga=(.+?);/)[1].split('.').slice(-2).join(".")

This is working for me.

这对我有用。

#1


57  

Google does have some documentation on getting the client id.

谷歌确实有一些关于获取客户端ID的文档。

Looks like this:

看起来像这样:

ga(function(tracker) {
  var clientId = tracker.get('clientId');
});

I've used this before, too:

我以前也用过这个:

ga.getAll()[0].get('clientId');

EDIT: If you have more than one tracker on the page, it may be probable that at index 0 there is not the one you want, so an alternative function should be the following:

编辑:如果页面上有多个跟踪器,则索引0可能不存在您想要的跟踪器,因此备用函数应如下所示:

function() {
  try {
    var trackers = ga.getAll();
    var i, len;
    for (i = 0, len = trackers.length; i < len; i += 1) {
      if (trackers[i].get('trackingId') === "ID-PROPERTY") {
        return trackers[i].get('clientId');
      }
    }
  } catch(e) {}  
  return 'false';
}

where ID-PROPERTY is the id of your Property (i.e. UA-XXXXX-XX).

其中ID-PROPERTY是您的财产的ID(即UA-XXXXX-XX)。

#2


5  

First create the Google Analytics ga object to create a tracker object, by passing it a "Ready callback" function, then use the tracker to call other methods.

首先创建Google Analytics ga对象以创建跟踪器对象,方法是将其传递给“Ready callback”函数,然后使用跟踪器调用其他方法。

The ga() command queue provides an interface for doing almost everything you need to do with the analytics.js library.

ga()命令队列提供了一个界面,用于执行您对analytics.js库所需的几乎所有操作。

"function(tracker)" is a callback function to be invoked when the analytics library is fully loaded and ready to be interacted with. The function is invoked with the default tracker object as it first argument. If no default tracker has been created, the first argument is/will return undefined.

“function(tracker)”是在分析库完全加载并准备好与之交互时调用的回调函数。使用默认跟踪器对象作为第一个参数调用该函数。如果没有创建默认跟踪器,则第一个参数是/将返回undefined。

Note: when the callback function is invoked, all ga object methods are available for use. Including the one you want tracker.get('clientId')

注意:调用回调函数时,所有ga对象方法都可以使用。包括你想要的tracker.get('clientId')

Replace the UA-XXXXX-Y in the code below with your UA code from Google Analytics.

使用Google Analytics中的UA代码替换下面代码中的UA-XXXXX-Y。

// Queues a tracker object for creation.
ga('create', 'UA-XXXXX-Y', 'auto');

// Once the tracker has been created, log the
// client ID to the console.
ga(function(tracker) {
  console.log(tracker.get('clientId'));
  /* Your other code here */
});

Alternatively for lines 1 & 2, use the code below to create a named tracker.

或者对于第1行和第2行,请使用以下代码创建命名跟踪器。

// Queues a named tracker object for creation.
ga('create', 'UA-XXXXX-Y', 'auto', 'myTracker');

#3


2  

Although the author explicitly said he is using Javascript, others (like me) may be searching for a way to get this information from the server-side, like PHP.

虽然作者明确表示他正在使用Javascript,但其他人(像我一样)可能正在寻找一种从服务器端获取此信息的方法,如PHP。

I found that you can easily check for the _ga cookie, which looks like:

我发现你可以轻松检查_ga cookie,它看起来像:

_ga=GA1.3.475128143.1522318100

In the exemple above, the user id is "475128143.1522318100".

在上面的例子中,用户id是“475128143.1522318100”。

So, in PHP I can fetch it quickly as:

所以,在PHP中,我可以快速获取它:

$gaUserId = preg_replace("/^.+\.(.+?\..+?)$/", "\\1", @$_COOKIE['_ga']);

You can also use Javascript to retrieve the cookie in a single line, without using ga() functions:

您还可以使用Javascript在一行中检索cookie,而不使用ga()函数:

var gaUserId = document.cookie.match(/_ga=(.+?);/)[1].split('.').slice(-2).join(".")

This is working for me.

这对我有用。