简体   繁体   English

跟踪代码管理器无法识别Google跟踪代码管理器dataLayer

[英]Google Tag Manager dataLayer unrecognized by Tag Manager

Please Help. 请帮忙。 I'm trying to use the GTM dataLayer to track purchases on my sales confirmation page. 我正在尝试使用GTM dataLayer在我的销售确认页上跟踪购买情况。 I've added the dataLayer and it is above the GTM container snippet, but the datalayer is completely unrecognized by Google Tag Manager. 我已经添加了dataLayer,它位于GTM容器代码段的上方,但是Google跟踪代码管理器无法完全识别该数据层。 It's like it doesn't exist on the page. 就像页面上不存在。 why? 为什么? You can see here in the source code that everything is properly formatted: view-source: http://www.maverickhelicopter.com/survey.aspx yet the GTM preview tool does not even recognize the existence of the datalayer. 您可以在源代码中看到所有格式都正确的格式:view-source: http : //www.maverickhelicopter.com/survey.aspx但是GTM预览工具甚至无法识别数据层的存在。 screenshot of GTM preview tool GTM预览工具的屏幕截图

The problem is you are calling dataLayer.push() function before GTM code snippet which defines dataLayer. 问题是您在定义dataLayer的GTM代码段之前调用dataLayer.push()函数。 As seen on your page: 如您在页面上看到的:

在此处输入图片说明

You have to move dataLayer.push either after you define dataLayer OR after GTM code snippet you have in the body. 您必须在定义dataLayer之后或在正文中的GTM代码段之后移动dataLayer.push。 It really depends when do you want to fire your "reservationPushed" event. 这实际上取决于您何时触发“ reservationPushed”事件。 If you include your push function in the head for example: 例如,如果您将推送功能包括在头部:

dataLayer = [{
    'page': {
        'type': 'confirmation',
        'environment': 'production'
    }
}];

dataLayer.push({
    'ecommerce': {
        'purchase': {
            'actionField': {
                'id': '0',
                'affiliation': '0',
                'revenue': '0'
            },
            'products': [{
                'name': '0',
                'id': '{{tourID}}',
                'price': '0',
                'category': 'None',
                'variant': 'None',
                'quantity': parseInt(document.getElementById("ctl00_ContentPlaceHolder1_hfGTMSEATS").value)
            }]
        }
    },
    'event': 'reservationPushed'
});

The "reservationPushed" event will be fired before Page View. 页面视图之前将触发“ reservationPushed”事件。

Alternatively you can move your whole push function below GTM code snippet to fire the event after Page View. 或者,您可以将整个推送功能移到GTM代码段的下方,以在“页面查看” 触发事件。 Just don't make a mistake of moving your dataLayer variable also, this is on the correct place as per Developer Guide: https://developers.google.com/tag-manager/devguide 只是不要犯一个错误,即也移动了dataLayer变量,根据开发人员指南,该变量位于正确的位置: https : //developers.google.com/tag-manager/devguide

So besically one of your options is to structure your code as such: 因此,通常来说,您的选择之一就是按以下方式构造代码:

<html>
...
<script>
dataLayer = [{
'page': {
  ...
}
}];
</script>
...

<!-- Google Tag Manager -->
...
<!-- End Google Tag Manger -->
<script>
dataLayer.push({
'ecomerce': {
...
}
</script>
...
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM