使用 Measurement Protocol (GA4) 设置 debug_mode

Setting debug_mode with Measurement Protocol (GA4)

找不到使用 Measurement Protocol 4 设置 debug_mode 参数的方法。 试图把它放在任何地方(并命名我能想到的所有东西)但没有运气 :) 文档仍然很简单,没有提到 debug_mode。 使用 web/js 和 GA4 效果很好!

ga4 的测量协议有两个端点,就像旧的 Google anlaytics

的测量协议一样
  • 测量协议/mp/collect
  • 测量协议验证服务器/调试/mp/collect

所以如果你 send a event 它会被发送到 Google anlaytics ga4

POST /mp/collect HTTP/1.1
HOST: www.google-analytics.com

<payload_data>

因此,如果您向其发送事件,它将被发送至 debug endpoint 用于 Google anlaytics ga4

POST /debug/mp/collect HTTP/1.1
HOST: www.google-analytics.com

<payload_data>

奇怪。突然,调试模式开始使用代码,我 100% 确定以前没有用过。

将参数 "debug_mode": true 添加到测量协议请求将使其显示在 Analytics 的 DebugView 中。

示例 json 有效负载:

{
  "client_id": "XXXXXXXXXX.YYYYYYYYYY",
  "events": [
    {
      "name": "page_view",
      "params": {
        "page_location": "...",
        "page_path": "...",
        "page_title": "...",
        "debug_mode": true
      }
    }
  ]
}

添加@DalmTo 和@bang 的答案 - 我没有看到我通过测量协议发送的事件显示在我们的 GA4 调试视图中。在我的案例中,根本原因是 Measurement Protocol 需要一种时髦的 user_properties 格式,但以下步骤也应该有助于其他人调试其他问题。

我采取的解决步骤:

  1. debug_mode: true 字段添加到单个事件参数中
  2. 使用 /debug/mp 端点 - 这指出了我 user_properties 格式中的错误

关于 user_properties 字段,我发送的内容是这样的:

{
  "client_id": "XXX.XXX",
  "user_id": "YYY",
  "user_properties": {
    "property_a": "value_a",
    "property_b": "value_b"
  },
  "events": ...
}

事实证明 GA4/Measurement Protocol 需要这样的东西:

{
  "client_id": "XXX.XXX",
  "user_id": "YYY",
  "user_properties": {
    "property_a": { "value": "value_a" },
    "property_b": { "value": "value_b" }
  },
  "events": ...
}

在撰写本文时,解决这个问题的唯一方法是仔细查看示例 here