使用 Measurement Protocol 向 Google Analytics 4 发送事件时,client_id 应该是什么?

我在客户端上使用 Google Analytics 4 (GA4) 来跟踪一大堆不同的事件。但是,有两种情况我无法涵盖客户端:

  1. 用户在第三方托管的支付页面上完成结账(本例中为 Stripe)。
  2. 支持团队退款。

这些事件由服务器使用 webhook 处理。对我来说,这似乎是最直接的解决方案,就是让服务器将事件发送到 GA4(而不是客户端发送它)。我相信 Measurement Protocol 应该用于此。

对于通过 Measurement Protocol 提交的每个事件,都需要一个 client_id 。当客户端提交事件时,这是一个自动生成的 ID,用于跟踪特定设备。

因此,我的问题是,在提交事件服务器端时 client_id 应该是什么?

是否应该将相同的 client_id 用于所有事件,以便将服务器识别为一个设备?我读过一些人建议为每个事件使用随机生成的 client_id ,但这会导致每个服务器端事件都识别一个新用户......


编辑:其中一个答案建议使用 client_id ,它是作为 cookie 的请求的一部分。但是,对于上面给出的两个示例,此 cookie 不存在,因为请求是由第三方 webhook 而不是用户发出的。

我当然可以将 client_id 存储在数据库中,但第二个示例中的退款由支持团队提供。因此从概念上讲,将该事件与用户的 client_id 关联起来感觉很奇怪,因为 client_id 只是识别用户设备的一种方式? IE。此处触发退款事件的不是用户的设备。

另一个退款事件示例是,当用户 A 与用户 B 进行购买时,用户 B 在一周后对该购买进行了退款。在这种情况下, client_id 应该是用户 A 还是用户 B?同样,在这里使用存储的 client_id 感觉很奇怪。因为,如果用户 A 在两台设备上登录怎么办?那么这里应该使用哪个 client_id 呢?

stack overflow What should the client_id be when sending events to Google Analytics 4 using the Measurement Protocol?
原文答案

答案:

作者头像

Great question. Yes, your aim to use Measurement Protocol is a proper solution here.

  1. Do not hardcode the client id. It's gonna be a hellish mess in reports. The nature of user-based reporting (which GA is) demands client ids to uniquely identify users. To your best ability.
  2. GA stores the client id in a cookie. You should have convenient and immediate access to it on every client hit to BE. The cookie name is _ga. GA4 appends the measurement id to the cookie name. Here, google's docs on it: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage But you can easily find it if you inspect "collect" hits and look at their payloads. There's another cookie named _gid that contains a different value. That would be a unique client id. Set it too if you can, but don't use it for the normal client id. It has a different purpose. Here how the cookie looks here, on stack:

enter image description here

And here it is in Network. You will need it for proper debugging. Mostly to make sure your FE client ids are the same as BE client ids:

enter image description here

  1. Keep an eye on the cases when the cookie is not set. When a cookie is not set, that most frequently means the user is using an ad-blocker. Your analysts will still want to know that the transaction happened even if there's a lack of context about the user. You still can track them properly.

    3.1 The laziest solution would be giving them an "AnonymousUser" client id and then append a random number to that so that it would both indicate that a user is anonymous and still make it possible for GA to separate them.

    3.2 A better solution would be for you to make a fingerprint client id for such users, say, hashing a concatenated string of their useragent+ip+locale+screen resolution, this is up to your analysts to actually work on the definition of a unique user if the google analytics library is unable to do it.

    3.3 Finally, one of the best solutions for you would be generating a client id on your own, keeping GA's format and maybe adding an indicator there that it has been generated on your end just for easier debugging in the Future and setting it as a cookie, using it instead of _ga. Just use a different cookie name so that ad-blockers wouldn't know to block it.

  2. If you want to indicate that a hit was sent through the server, that's a good idea. Use custom dimension for that. Just sync it with your analysts first. Maybe they wouldn't want that, or maybe they would want it in a different dimension.

Now, this is very trivial. There are ways to go much deeper and to improve the quality of data from here. Like gluing the order id, the transaction id, the user id to that, using them to generate client id, do some custom client tracking for the future. But I must say that it's better than what more than 90% of, say, shopify clients have.

Also, GA4 is not good enough for deeper production usage. Many things there are still very rudimentary and lacking. I would suggest concentrating on Universal Analytics and having GA4 as a backup for when Google makes GA4 actually good enough to replace UA. That is, unless you're downloading your data elsewhere and not using GA's interface for analysis.

作者头像

It seems that this page (Relevant portion in the screenshot below), advices to either send the data along with the client_id or user_id. However fails to address the fact client_id is a mandatory field as stated here.

I believe it is probably safe to assume that randomly generating this field should work. At least it seems to on my end however be warned that I am unsure if this has any impact on attribution.

GA4 Help Page

* In the above image, Device ID refers to client_id

相关问题