简体   繁体   English

Google Analytics测量协议:如何从服务器上的cookie获取客户端ID?

[英]Google Analytics Measurement Protocol: How do I get the client-id from cookie on the server?

I want to send a google analytics event from the server using the measurement protocol. 我想使用测量协议从服务器发送一个谷歌分析事件。

The documentation states the cid is required, and should be a UUID ( https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid ). 文档说明了cid是必需的,并且应该是UUID( https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid )。

However, I want to use the same client id as what is stored in the _ga cookie. 但是,我想使用与_ga cookie中存储的内容相同的客户端ID。 But according to this , I shouldn't parse the id out of the cookie directly since it could change without warning. 但根据这一点 ,我不应该直接解析cookie中的id,因为它可能会在没有警告的情况下改变。

Furthermore, the string returned by tracker.get('clientId') in the browser is not a standard UUID string, so even if I retrieved the value in the browser and sent it to the server, it still isn't in the format the documentation says it should be. 此外,tracker.get('clientId')在浏览器中返回的字符串不是标准的UUID字符串,所以即使我在浏览器中检索了值并将其发送到服务器,它仍然不是格式化的文档说它应该是。

So, what do I need to do to get the client-id from the cookie in a forward compatible way? 那么,我需要做什么才能以前向兼容的方式从cookie中获取客户端ID?

Use ga.getAll()[0].get('clientId'); 使用ga.getAll()[0].get('clientId'); to get the clientId. 获取clientId。

Regarding their documentaiton, Google just doesn't want you to grab the cookie directly, and parse it out, as the cookie could change in the future. 关于他们的文档,谷歌只是不希望你直接抓取cookie,并解析它,因为cookie将来可能会改变。

The method they outlined is the preferred way to grab the clientId. 他们概述的方法是获取clientId的首选方法。

Regarding the clientId and using UUID, you can pretty much pass in any value that anonymously identifies somebody (including using GA's clientId). 关于clientId和使用UUID,您几乎可以传递匿名识别某人的任何值(包括使用GA的clientId)。

What we've done is use ga.getAll()[0].get('clientId'); 我们所做的是使用ga.getAll()[0].get('clientId'); to grab the client and send it to the server where we can put the clientId into the require parameter for the measurement protocol, and send back a server-side measurement protocol request to GA with transaction data like Cost of Goods sold, etc. Using the clientId allows you to connect the transaction to the source/medium, etc. 获取客户端并将其发送到服务器,我们可以将clientId放入测量协议的require参数,并将服务器端测量协议请求发送回GA,其中包括销售成本等交易数据等。使用clientId允许您将事务连接到源/介质等。

The right way to get ClientID is to ask tracker by passing a callback. 获取ClientID的正确方法是通过传递回调来询问跟踪器。

It has to be called after ga("create",...) and the request is: 它必须在ga("create",...)之后调用ga("create",...)并且请求是:

ga(function(tracker) {
  // very similar to internal method like ga.getAll()[0].get('clientId');
  var clientId = tracker.get('clientId'); 
  // now you can pass client id to your internal system
  myInternalMeasurementProtocolSetter(clientId);  
});

After this you have to call ga("send"...) to run the batch of previous settings. 在此之后,您必须调用ga("send"...)来运行之前的一批设置。

If you want to call this out of the initial setting queue, you have to verify, that GoogleAnalytics are already initialized. 如果您要将其调出初始设置队列,则必须验证GoogleAnalytics是否已初始化。

You can ask ga.getAll(); 你可以问ga.getAll(); if there is any tracker. 如果有任何跟踪器。 If yes, then you can pass callback. 如果是,那么您可以传递回调。

If you will use any other way, you can get in troubles early or late. 如果您将使用任何其他方式,您可以提前或延迟陷入困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Javascript从Cookie中提取Google Analytics广告系列数据? - How do I extract Google Analytics campaign data from their cookie with Javascript? 客户端到服务器端Google Analytics MP客户端ID - Client side to server side google analytics MP client ID 如何访问服务器上的httponly cookie(由客户端设置)? - How do I access a httponly cookie (set by the client) on the server? 如何在 2021 年在 gatsby 中设置 google-analytics 和 cookie 同意? - How do I setup google-analytics & cookie consent in 2021 in gatsby? 并非所有客户ID都会通过GTM中的第一方Cookie发送到Google Analytics(分析) - Not all Client ID's are sent to Google Analytics through 1st Party Cookie in GTM 存储在 cookie 中的 Google Analytics ID 未定义 - Google Analytics ID stored in cookie is undefined 如何删除客户端上的 HttpOnly Cookie? - How do I delete a HttpOnly Cookie on the client? 如何从PHP到javascript获取cookie? - How do I get a cookie from PHP to javascript? 您如何在客户端请求标头(getServerSideProps)中读取从子域域上的服务器发送的 cookie? - How do you read cookie sent from server on subdomain domain in client request headers (getServerSideProps)? 从PHP读取Google Analytics(分析)Cookie - Reading Google Analytics cookie from PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM