首页 > 解决方案 > 如何在 Node.js 中使用谷歌标签管理器

问题描述

我将一个简单的电子商务对象推送到dataLayer客户端的 Google 跟踪代码管理器。它工作正常,但我需要将其移至服务器。

我找到了Google API Node.js 客户端库,但它还很年轻,我找不到任何工作示例。

有没有人设法做到这一点?我将如何使用 node.js 中的 google-api-nodejs-client 完成以下操作?

dataLayer.push({
  event: 'purchase',
  user: 'user_1234',
  transactionId: 'trans_123',
  transactionAffiliation: 'Acme Clothing',
  transactionTotal: 11.99,
  transactionProducts: [
    {
      id: '1234',
      sku: 'SKU47',
      name: 'Fluffy Pink Bunnies',
      price: 11.99,
      quantity: 1
    }
  ]
});

标签: node.jsgoogle-analyticsgoogle-apis-explorergoogle-api-nodejs-client

解决方案


我认为你不需要这个或那个库的谷歌 API。您可以只使用Google Measurement Protocol

这是他们如何构建呼叫的示例。

因此,对于您的数据层帖子,它将类似于:

对于交易

v=1               // Version.
&tid=UA-XXXXX-Y   // Tracking ID / Property ID.
&cid=1234          // user_1234.

&t=transaction    // Transaction hit type.
&ti=123          // transaction ID. Required.
&ta=acmeClothing   // Transaction affiliation.
&tr=11.99         // Transaction revenue.

然后项目命中

v=1               // Version.
&tid=UA-XXXXX-Y   // Tracking ID / Property ID.
&cid=1234          // Anonymous Client ID.

&t=item           // Item hit type.
&ti=123         // Transaction ID. Required.
&in=fluffy pink bunnies         // Item name. Required.
&ip=11.99           // Item price.
&iq=1             // Item quantity.
&ic=sku47      // Item code / SKU.

推荐阅读