Google Analytics事件跟踪:按钮侦听器不起作用 [英] Google Analytics Event Tracking: button listener does not work

查看:103
本文介绍了Google Analytics事件跟踪:按钮侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为GA事件追踪添加一个事件处理程序到一个按钮。我仔细阅读了文档并花费了一个小时的谷歌搜索,但我没有成功。
会话跟踪工作(我在实时分析中看到自己),但事件跟踪不起作用。
这是我的代码:

I need to add an event handler for GA event tracking to a button. I read the documentation thoroughly and spent an hour googling, but I didn't make it work. The session tracking works (I see myself in realtime analytics), but the event tracking doesn't work. This is my code:

<html>
<head>
<script>
  (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-12345678-1', 'auto');
  ga('send', 'pageview');
</script>
</head>

<body>
<div>
<script>
var downloadLink = document.getElementById('submit_btn');
addListener(downloadLink, 'click', function() {
  ga('send', 'event', 'button', 'click', 'nav-buttons');
});
function addListener(element, type, callback) {
 if (element.addEventListener) element.addEventListener(type, callback);
 else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
</script>
<button type="submit" id="submit_btn" name="submit_btn">submit</button></div>
</body>
</html>

我在这里做错了什么?

What am I doing wrong here?

推荐答案

我不确定,但它可能是JavaScript代码在页面上调用的方式。可以尝试以下代码:

I am not sure, but it may be the way the JavaScript code is invoke on the page.Can you try the following code:

<body>
  <div>
    <button type="submit" id="submit_btn" name="submit_btn">submit</button>
  </div>  
</body>
<script>
  var downloadLink = document.getElementById('submit_btn');

  addListener(downloadLink, 'click', function() {
    ga('send', 'event', 'button', 'click', 'nav-buttons');
  });

  function addListener(element, type, callback) {
    if (element.addEventListener) element.addEventListener(type, callback);
    else if (element.attachEvent) element.attachEvent('on' + type, callback);
  }
</script>
</html>

这篇关于Google Analytics事件跟踪:按钮侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆