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

时间:2017-09-07 17:03:24

标签: google-tag-manager

我从简单的'创建GTM变量没有问题。数据层, 例如:

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

但是不能理解如何从数组中获取值。 基本上我需要从这些dataLayer表中获取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'],

1 个答案:

答案 0 :(得分: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>

并且您想要创建将返回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