驗證命中 - Measurement Protocol

本文說明如何驗證 Google Analytics (分析) Measurement Protocol 命中。

總覽

即使 Measurement Protocol 命中的格式有誤或缺少必要參數,Google Analytics (分析) Measurement Protocol 也不會傳回 HTTP 錯誤代碼。如要確保命中格式正確且包含所有必要的參數,您可以先在驗證伺服器中測試這些參數,然後再部署至實際工作環境。

本指南說明將命中傳送至 Measurement Protocol 驗證伺服器的程序,並說明如何解讀回應。

傳送命中以進行驗證

命中可透過 Measurement Protocol 目前支援的任何方法傳送至 Measurement Protocol 驗證伺服器。唯一的差別在於網址。如要將命中傳送至 Measurement Protocol 驗證伺服器,請使用端點 /debug/collect,而非 /collect。以下範例顯示傳送至 Measurement Protocol 驗證伺服器的無效命中:

https://www.google-analytics.com/debug/collect?tid=fake&v=1

如要進一步瞭解如何建構 Measurement Protocol 命中資料,請參閱通訊協定參考資料

解讀回應

Measurement Protocol Validation 伺服器的回應會採用 JSON 格式。以下範例顯示上述命中的回應:

{
  "hitParsingResult": [
    {
      "valid": false,
      "hit": "GET /debug/collect?tid=fake\u0026v=1 HTTP/1.1",
      "parserMessage": [
        {
          "messageType": "ERROR",
          "description": "The value provided for parameter 'tid' is invalid. Please see http://goo.gl/a8d4RP#tid for details.",
          "parameter": "tid"
        },
        {
          "messageType": "ERROR",
          "description": "Tracking Id is a required field for this hit. Please see http://goo.gl/a8d4RP#tid for details.",
          "parameter": "tid"
        }
      ]
    }
  ]
}

回應根層級 hitParsingResult 是一個陣列,長度會對應至原始要求中傳送的命中次數。陣列中的每個物件都會包含 validhitparserMessage 鍵。如果命中無效,parserMessage 會包含說明驗證問題的物件陣列。如果命中有效,parserMessage 就會是空白陣列。

下表說明 hitParsingResult 陣列中每個項目的屬性:

類型 說明
valid Boolean true 代表有效命中,false 代表無效命中。
hit string 包含要求路徑、其方法和通訊協定的字串。
parserMessage Array 剖析器訊息清單。如果命中有效,這個陣列就會是空白的。
parserMessage.messageType string 可能的值包括「INFO」、「WARN」和「ERROR」。
parserMessage.description string 解決驗證問題的其他詳細資料或步驟。
parserMessage.parameter string|undefined 導致驗證問題的參數 (如適用)。