javascript - GA发送事件,返回未定义

标签 javascript events google-analytics

初始化GA

var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxxxx-6']);
    _gaq.push(['_trackPageview']);

    (function () {
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
    })();

跟踪事件

ga('send', {
              'hitType': 'event',
              'eventCategory': 'Customer',
              'eventAction': 'Registration',
              'eventLabel': 'Popup',
              'eventValue': 0.99
            });

但这总是返回未定义,我在谷歌分析网站的转化中添加了“注册”事件(带有该类别、标签和值),

知道为什么会发生这种情况吗?

当然不会被跟踪..

enter image description here

最佳答案

您正在混合旧式经典语法(使用 _gaq 数组)和 Universal Analytics,因此您的 ga 对象设置不正确(_gaq 中的跟踪器 ID 不会在 ga 对象中使用,因此不会创建跟踪器)。

您的代码应如下所示:

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXXXX-1', 'auto');
  ga('send', 'pageview');
ga('send', {
              'hitType': 'event',
              'eventCategory': 'Customer',
              'eventAction': 'Registration',
              'eventLabel': 'Popup',
              'eventValue': 1
            });

关于javascript - GA发送事件,返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32421691/

相关文章:

java - 关键事件只是有时注册

javascript - 将事件附加到文档,这取决于 React 中有条件呈现的元素

ios - iOS SWIFT 中的 Google Analytics 集成

php - Google Analytics 数据导出 API V3

php - 从Google Analytics(分析)获取数据的最简单方法?

javascript - 有人可以解释一下这个递归代码是如何工作的吗?

javascript - 有理由不在 JavaScript 中使用严格模式吗?

javascript - 在不使用 jquery 的情况下在 div/span 元素溢出上添加点/省略号

javascript - 响应式 jQuery 数据表无法获取一行的详细信息

javascript - 如何删除 javascript 中的事件监听器?