如何从 dataLayer 数组创建 GTM 变量?

How to create GTM variable from dataLayer array?

我从 'simple' dataLayer 创建 GTM 变量没有问题, 示例:

"transactionEntity": "ORDER",
"transactionId": "193552702",

但不明白如何从数组中取值。 基本上我需要从这些 dataLayer table:

中获取 ID 值
"transactionProducts":     [
      {
        "id": "5",
        "sku": "black-handbag",
        "price": 170,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      },
      {
        "id": "3",
        "sku": "red-handbag",
        "price": 120,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      }
    ],

并使用此格式将它们传递给 FB 像素:

content_ids: ['5', '3'],

如果你有这样的数据层:

<script>
dataLayer.push({
  'ecommerce': {
     "transactionProducts":     [
      {
        "id": "5",
        "sku": "black-handbag",
        "price": 170,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      },
      {
        "id": "3",
        "sku": "red-handbag",
        "price": 120,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      }
    ]
  }
});
</script>

并且您想创建变量,它将 return ID 数组 ['5', '3']

然后需要创建两个变量:

1) 姓名:transactionProducts

类型:Data Layer Variable

数据层变量名称:ecommerce.transactionProducts

2) 姓名:transactionProductsIds

类型:Custom JavaScript

自定义 JavaScript: function () { return {{transactionProducts}}.map(function(a) {return a.id;}); }

然后您可以使用第二个变量 transactionProductsIds 来接收 ID