本文介绍了dataLayer.push在GTM脚本之后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Google跟踪代码管理器实施增强型电子商务,我想推送标签Universal Analytics的一些数据。

I want to implement the Enhanced Ecommerce with Google Tag Manager and I want to push some data for the tag Universal Analytics.

我总是在GTM脚本之前创建dataLayer,但现在我需要用 dataLayer.push发送其他数据

I always created the dataLayer before the GTM script, but now I need to send other data with dataLayer.push

它不起作用,datalaLayer.push只有在GTM脚本启动之前才能工作。

And it doesn't work, datalaLayer.push only works if I do it just before the GTM script starts.

实施例。这有效:

   <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>

但如果我在GTM脚本不起作用后使用dataLayer.push,则不会发送任何数据报告错误。

But if I use dataLayer.push after the GTM script does not work, no data is sent and no errors are reported.

这对我不起作用:

<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>


推荐答案

您没有遵循最佳做法,因此,您迟早会遇到问题


  • 始终使用。 push 而不是初始化:你的第一个调用是一个数组初始化( dataLayer = [)。这意味着如果dataLayer已经存在,它将被完全覆盖,包括 .push 方法,该方法由GTM自定义以接收推送事件。现在它工作正常,因为你在初始化后调用GTM。但这是一个坏习惯。有一天你会将GTM移到初始化之上,或者在GTM之后添加类似的初始化调用,它会中断。

  • Always use .push instead of initialization: your first call is an array initialization (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. Right now it's working fine because you're calling GTM after the initialization. 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.

你的第一个电话应该是:

Your first call should be:

dataLayer = window.dataLayer || [];  
dataLayer.push({...});




  • 始终设置事件 property 事件属性是GTM用来定义触发器并知道数据何时可用的内容。您可以连续2次 .push 来电,第一次使用事件,第二次没有,以及来自的数据第1次将在第二次活动中提供(只要该活动不会覆盖它),但这又是一个坏习惯并且玩火。

    • Always set the event property: the event property is what is used by GTM to define triggers and know when data becomes available. 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.
    • 例如:

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

      在您的特定情况下,由于缺少事件键,只要GTM在推送后加载,它就会起作用,因为当GTM启动时数据已经存在。当在GTM之后移动 push 调用时,因为没有事件属性,GTM无法知道数据何时可用。所以你应该:

      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. So you should:


      • 添加事件键(始终!)

      • 配置触发器h匹配事件

      • Add the event key (always!)
      • Configure a trigger which matches the event

      以下是关于这些主题的更多内容:

      Here is some more reading on those topics:




      • https://www.simoahava.com/gtm-tips/datalayer-declaration-vs-push/
      • https://www.simoahava.com/gtm-tips/add-the-event-key-to-datalayer-pushes/

      这篇关于dataLayer.push在GTM脚本之后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:01