简体   繁体   English

dataLayer.push 在 GTM 脚本后不起作用

[英]dataLayer.push not working after GTM script

I want to implement the Enhanced Ecommerce with Google Tag Manager and I want to push some data for the tag Universal Analytics.我想使用 Google 标签管理器实施增强型电子商务,并且我想为标签 Universal Analytics 推送一些数据。

I have always created the dataLayer before the GTM script, but now I need to send more data with dataLayer.push我一直在 GTM 脚本之前创建 dataLayer,但现在我需要使用dataLayer.push发送更多数据

And it doesn't work, datalaLayer.push only works if it happens just before the GTM script starts.它不起作用, datalaLayer.push仅在 GTM 脚本启动之前发生时才起作用。

Example.例子。 This works:这有效:

   <script>
    <head>
     dataLayer = [{   
                 
            'google_tag_params': {
               'ecomm_pagetype': 'category',
               'ecomm_category': '{{ $resource->seo->h1 }}',
             }
         }];
    
    dataLayer.push({
              'ecommerce': {
                'currencyCode': 'EUR',    
                'impressions': [
                     {
                        'id':       '12312',
                        'price':    24,
                        'category': 'wfwefwerwerwer',
                        'position': 2,
                        'name':     'wfwefwerwerwer',  
                        'brand':   'My Brand',
                        'list':    'Product List',
    
                    }
                ]
              }
            });
    
    (function(w,d,s,l,i){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=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXXXX');
    
    </script>
    </head>

But if I use dataLayer.push after the GTM script does not work, no data is sent and no errors are reported.但是如果我在 GTM 脚本不起作用后使用 dataLayer.push ,则不会发送任何数据,也不会报告任何错误。

This isn't working for me:这对我不起作用:

<head>
    <script>
         dataLayer = [{   
                     
                'google_tag_params': {
                   'ecomm_pagetype': 'category',
                   'ecomm_category': '{{ $resource->seo->h1 }}',
                 }
             }];
        
        
        (function(w,d,s,l,i){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=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','GTM-XXXXXX');

    </script>
</head>

<body>

    //something content html here

    <footer></footer>

    <script>
        dataLayer.push({
          'ecommerce': {
            'currencyCode': 'EUR',    
            'impressions': [
                 {
                    'id':       '12312',
                    'price':    24,
                    'category': 'wfwefwerwerwer',
                    'position': 2,
                    'name':     'wfwefwerwerwer',  
                    'brand':   'My Brand',
                    'list':    'Product List',

                }
            ]
          }
        });
    </script>
</body>

You're not following best practices, so you'll run into issues , sooner or later.您没有遵循最佳实践,因此迟早会遇到问题

  • Always use .push instead of initialization : your first call is an array initialization ( dataLayer = [ ).始终使用.push而不是初始化:您的第一个调用是数组初始化( dataLayer = [ )。 This means that if the dataLayer already exists, it will be completely overwritten, including the .push method which is customized by GTM in order to receive pushed events.这意味着如果 dataLayer 已经存在,它将被完全覆盖,包括 GTM 自定义的.push方法以接收推送事件。 Right now it's working fine because you're calling GTM after the initialization.现在它工作正常,因为您在初始化后调用 GTM。 But it's a bad habit to take.但这是一个坏习惯。 One day you will move GTM above that initialization or add similar initialization calls after GTM, and it will break.有一天,您将 GTM 移到该初始化之上或在 GTM 之后添加类似的初始化调用,它就会中断。

Your calls should be:你的电话应该是:

window.dataLayer = window.dataLayer || [];  
dataLayer.push({...});
  • Always set the event property : the event property is what is used by GTM to define triggers and know when data becomes available.始终设置event属性event属性是 GTM 用来定义触发器并知道数据何时可用的属性。 You can have 2 successive .push calls, the 1st with an event , and the 2nd without, and the data from the 1st will be available in the 2nd event (as long as that event doesn't overwrite it), but once again that's bad habit and playing with fire.您可以有 2 个连续的.push调用,第一个带有event ,第二个没有,并且来自第一个的数据将在第二个事件中可用(只要该事件不覆盖它),但再次是坏习惯和玩火。

For instance:例如:

dataLayer.push({
  'event': 'ecommerce', // naming is up to you, should match your GTM triggers 
  'ecommerce': {
  ...

In your particular case , since the event key is missing, it works as long as GTM loads after the push, because the data is already there when GTM kicks in. When the push call is moved after GTM, because there is no event property, there is just no way for GTM to know when data becomes available.在您的特定情况下,由于缺少event键,只要在推送后加载 GTM,它就可以工作,因为当 GTM 启动时数据已经存在。当push调用在 GTM 之后移动时,因为没有event属性, GTM 无法知道数据何时可用。 So you should:所以你应该:

  • Add the event key (always!)添加event键(总是!)
  • Configure a trigger which matches the event配置匹配event的触发器

Here is some more reading on those topics:以下是有关这些主题的更多阅读资料:

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

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