簡體   English   中英

使用新的 Google Analytics 4 (GA4) 服務器端進行跟蹤

[英]Tracking with the new Google Analytics 4 (GA4) server-side

Google Analytics 4 (GA4) 最近發布,並在 Google Analytics 控制面板上設置了新屬性的默認設置。 我正在檢查它,它看起來很棒。 我們通過我們的 Node.js 服務器向 Google Analytics 報告大量數據,而不是使用名為universal-analytics ( https://www.npmjs.com/package/universal-analytics ) 的庫來向客戶端報告,該庫非常有效出色地。

我們想盡快開始使用 GA4,但我們找不到任何關於如何將事件發送到 GA4 屬性服務器端的文檔。 只有客戶端示例,這些示例在服務器上似乎根本不起作用。

簡而言之,以下內容的服務器端等效項是什么?

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123ABC123"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-ABC123ABC123');
</script>

有人成功了嗎?

您可以嘗試使用測量協議 v2: https ://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#send_an_event

它運行良好

const measurementId = `G-XXXXXXXXXX`;
const apiSecret = `<secret_value>`;

fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, {
  method: "POST",
  body: JSON.stringify({
    "client_id": "client_id",
    "events": [{
      "name": "add_payment_info",
      "params": {
        "currency": "USD",
        "value": 7.77,
        "coupon": "SUMMER_FUN",
        "payment_type": "Credit Card",
        "items": [
          {
            "item_id": "SKU_12345",
            "item_name": "Stan and Friends Tee",
            "affiliation": "Google Merchandise Store",
            "coupon": "SUMMER_FUN",
            "currency": "USD",
            "discount": 2.22,
            "index": 0,
            "item_brand": "Google",
            "item_category": "Apparel",
            "item_list_id": "related_products",
            "item_list_name": "Related Products",
            "item_variant": "green",
            "location_id": "L_12345",
            "price": 9.99,
            "quantity": 1
          }
        ]
      }
    }]
  })
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM