特定操作

时间:2017-10-17 14:30:41

标签: javascript google-tag-manager

正如标题所说,我需要在动作完成后将变量传递给GTM数据层。 更具体地说,我希望在datepicker中选择日期之后将巡视日期传递给GTM Layer。

我不知道做对照的方法是什么。现在我有以下数据层脚本:

<script >
    var dataLayer = [({
        "customerLoggedIn": 0,
        "customerId": 0,
        "customerGroupId": "1",
        "customerGroupCode": "GENERAL",
        "productId": "6",
        "productName": "Big Loop Boat Tour",
        "productSku": "boat-tour",
        "productPrice": "0.00",
        "productPriceExcludingTax": "0.00",
        "productTax": "0.00",
        "productTaxRate": 0,
        "productGender": false,
        "pageType": "catalog\/product\/view"
    })];
    dataLayer.push({
        'ecommerce': {
            "detail": [{
                "id": "6",
                "name": "Big Loop Boat Tour",
                "sku": "boat-tour",
                "price": 0,
                "priceexcludingtax": "0.00",
                "tax": "0.00",
                "taxrate": 0,
                "brand": "",
                "gender": false,
                "category": null,
                "children": []
            }]
        }
    });
    (function(w, d, s, l, i) {
        if (i == '') {
            return console.log('No GTM ID provided');
        }
        w[l] = w[l] || [];
        w[l].push({
            'gtm.start': new Date().getTime(),
            event: 'gtm.js'
        });
        var f = d.getElementsByTagName(s)[0],
            j = d.createElement(s),
            dl = l != 'dataLayer' ? '&l=' + l : '';
        j.async = true;
        j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl;
        f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'dataLayer', 'XXXXXXXX');
</script>

screenshot

我应该在dataLayer数组中添加其他变量吗?比如selectedDate,它将在页面加载时未定义。

    var dataLayer = [({
        "customerLoggedIn": 0,
        "customerId": 0,
        "customerGroupId": "1",
        "customerGroupCode": "GENERAL",
        "productId": "6",
        "productName": "Big Loop Boat Tour",
        "productSku": "boat-tour",
        "productPrice": "0.00",
        "productPriceExcludingTax": "0.00",
        "productTax": "0.00",
        "productTaxRate": 0,
        "productGender": false,
        "pageType": "catalog\/product\/view"
        "selectedDate" : ""
    })];

在页面中添加javascript函数以获取所选日期并将其写在变量$date.selected中。

使用此自定义脚本

在datepicker点击触发的GTM中创建自定义标记
<script>
    dataLayer.push({"selectedDate": $date.selected});    
</script>

这看起来是对的吗?

或者我可以像这样在主数据层脚本中传递变量吗?

<script >
    var dataLayer = [({
        "customerLoggedIn": 0,
        "customerId": 0,
        "customerGroupId": "1",
        "customerGroupCode": "GENERAL",
        "productId": "6",
        "productName": "Big Loop Boat Tour",
        "productSku": "boat-tour",
        "productPrice": "0.00",
        "productPriceExcludingTax": "0.00",
        "productTax": "0.00",
        "productTaxRate": 0,
        "productGender": false,
        "pageType": "catalog\/product\/view",
        "selectedDate": $date.selected
    })];
    dataLayer.push({
        'ecommerce': {
            "detail": [{
                "id": "6",
                "name": "Big Loop Boat Tour",
                "sku": "boat-tour",
                "price": 0,
                "priceexcludingtax": "0.00",
                "tax": "0.00",
                "taxrate": 0,
                "brand": "",
                "gender": false,
                "category": null,
                "children": []
            }]
        }
    });
    (function(w, d, s, l, i) {
        if (i == '') {
            return console.log('No GTM ID provided');
        }
        w[l] = w[l] || [];
        w[l].push({
            'gtm.start': new Date().getTime(),
            event: 'gtm.js'
        });
        var f = d.getElementsByTagName(s)[0],
            j = d.createElement(s),
            dl = l != 'dataLayer' ? '&l=' + l : '';
        j.async = true;
        j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl;
        f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'dataLayer', 'XXXXXXXX');
</script>

1 个答案:

答案 0 :(得分:1)

无需在开始时将变量定义为undefined(或空字符串)。只需在操作完成后将所需的值推送到dataLayer即可。对于您的情况,由于用户可能会改变他/她的想法并再次更改datepicker值,因此可能会在dataLayer中多次推送新日期。

每次向dataLayer添加内容时,都会推送新的“消息”。将dataLayer视为包含您推送的所有消息的数组。

在页面加载时初始化只会添加一个没有值的变量条目。