【问题标题】:How to create GTM variable from dataLayer array?如何从 dataLayer 数组创建 GTM 变量?
【发布时间】:2017-09-07 17:03:23
【问题描述】:

从“简单”数据层创建 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'],

【问题讨论】:

    标签: google-tag-manager


    【解决方案1】:

    如果你有这样的数据层:

    <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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多