javascript 谷歌分析:dataLayer.push 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28236289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:38:25  来源:igfitidea点击:

Google analytics: dataLayer.push not working?

javascriptgoogle-analyticsgoogle-tag-manager

提问by kenpeter

Based on this thread: Tracking events using Google Tag Manager

基于此线程:使用 Google 标签管理器跟踪事件

I created my own version, which is located at e.g. http://test.site.com

我创建了自己的版本,位于例如http://test.site.com

<!DOCTYPE html>
<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

  <script>
    window.dataLayer = window.dataLayer || [];  

    dataLayer.push({
      'event':'GAevent',
      'eventCategory': 'App4', 
      'eventAction': 'Click',
      'eventLabel': 'iOS4'
    });


  </script>
</head>

<body>
  <!-- Start google tag manager -->
  <script>
    (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 =
            '//www.googletagmanager.com/gtm.js?id=' + i + dl;
        f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'primecont', 'GTM-1234');
  </script>
  <!-- End google tag manager -->

</body>
</html>

I turned on google tag manager debug mode and watched it on google analytics real-time.

我打开了谷歌标签管理器调试模式并在谷歌分析上实时观看。

I have 2 fire rules for a tag:

我有 2 个标签的防火规则:

  1. {{url}} contains test.site.com
  2. {{event}} equals GAevent
  1. {{url}} 包含 test.site.com
  2. {{event}} 等于 GAevent

what I got is "event category: undefined" & "event action: undefined" in real time google analytics.

我得到的是实时谷歌分析中的“ event category: undefined”&“ event action: undefined”。

If I remove "{{url}} contains test.site.com", nothing is appearing in real-time.

如果我删除"{{url}} contains test.site.com“,则不会实时显示任何内容。

UpdateI used a separated google tag manager account and create a test page, so everything it is minimum. It seems working in real time. The non-working google tag manager are shared by schools and faculties. I suspect that is the reason?

更新我使用了一个单独的谷歌标签管理器帐户并创建了一个测试页面,所以一切都是最少的。它似乎是实时工作的。不工作的谷歌标签管理器由学校和教职员工共享。我怀疑是这个原因?

采纳答案by Simo Ahava

Your Tag is firing with wrong variables for a number of reasons.

由于多种原因,您的标签使用错误的变量触发。

If the dataLayer.push()were correct, you Tag would only need the {{url}} matches RegEx .*as its rule, since all pushes that occur before the container snippet are available to the All Pages rule.

如果dataLayer.push()正确,您的 Tag 只需要{{url}} 匹配 RegEx .*作为其规则,因为在容器代码段之前发生的所有推送都可用于所有页面规则。

However, you can also push the 'event' : 'GAEvent'pre-container-snippet, if you wish. But then you must remove the {{url}}rule, as it would make your tag fire twice: First with {{event}} equals GAEventand then with the {{url}}rule.

但是,'event' : 'GAEvent'如果您愿意,您也可以推送预容器代码段。但是随后您必须删除{{url}}规则,因为它会使您的标签触发两次:首先使用{{event}} 等于 GAEvent,然后使用{{url}}规则。

The reason why your code doesn't work even if you fix the above issues is because you have renamed the dataLayer object in your container snippet:

即使您修复了上述问题,您的代码也无法运行的原因是您已重命名容器代码段中的 dataLayer 对象:

})(window, document, 'script', 'primecont', 'GTM-1234');

the 'primecont'String is the new name for Google Tag Manager's Data Layer that you have given, for some reason. This is why your dataLayer.push()will not work, since Google Tag Manager is listening for a primecont.push()instead.

'primecont'出于某种原因,字符串是您为 Google 跟踪代码管理器的数据层提供的新名称。这就是为什么您dataLayer.push()将无法工作的原因,因为 Google 标签管理器正在监听 a primecont.push()

So, either change all your dataLayerinteractions to primecont, or edit the container snippet's invocation line to look like this:

因此,要么将所有dataLayer交互更改为primecont,要么将容器代码段的调用行编辑为如下所示:

})(window, document, 'script', 'dataLayer', 'GTM-1234');