Google Analytics - 如何使用 DataLayer 设置自定义变量

Google Analytics - how to set custom variable with DataLayer

我不太了解 Google 分析 (GA)。 我现在的工作是找到从网上商店发送数据以在自定义变量(键 1)中显示在 GA 中的方法,并且它必须使用 dataLayer 发送。

要发送的数据是为了告诉访客是新访客还是已经是客户。

经过研究我发现只有 ga.js

发送的方式

_gaq.push(["_setCustomVar", 1, "ReturningVisitor", 'Yes', 1]);

但这对我不起作用,因为我们使用 DataLayer 将其他数据发送到 GA。

这是我们发送其他数据的示例代码:

window.dataLayer = window.dataLayer || [];

var dataToPush = {
  'event': 'transactionEvent',
  'transactionId': '112256',
  'transactionAffiliation': 'CompanyName',
  'transactionTotal': 250.00;?>,
  'transactionTax': 0,
  'transactionShipping': 0,
  'transactionProducts': []
}

dataLayer.push(dataToPush);

那么如何将自定义变量(key 1)添加到这个dataLayer中呢?

您可以根据需要将自定义变量发送到数据层。例如:

var dataToPush = {
  'event': 'transactionEvent',
  ...
  'anyNameForYourCustomVariable': 'ReturningVisitor'
}

dataLayer.push(dataToPush);

重要的一步是在 GTM 中进行设置。您已按照以下步骤操作:

  1. 在 GTM 中创建一个新的 DataLayer 变量并将 'Data Layer Variable Name' 设置为 'anyNameForYourCustomVariable'
  2. 在 GTM 中转到您要发送的 GA 标签并编辑其 Google 分析设置
  3. 点击 'More Settings' -> 'Custom Dimensions' 然后点击 'ADD CUSTOM DIMENSION'
  4. 将索引设置为“1”并为 'Dimension Value' select 您创建的数据层变量

更多信息请访问开发者指南:https://developers.google.com/tag-manager/devguide