button - Google Analytics(分析)跟踪无效

标签 button google-analytics analytics tracking event-tracking

我想跟踪我的Google Analytics(分析)中某个按钮的点击情况。我尝试过四处搜寻,但似乎找不到任何有帮助的东西。

这是我复制的代码:

<script>
var downloadLink = document.getElementById('button');
addListener(downloadLink, 'click', function() {
  ga('send', 'event', 'button', 'click', 'contact-button');
});


/**
 * Utility to wrap the different behaviors between W3C-compliant browsers
 * and IE when adding event handlers.
 *
 * @param {Object} element Object on which to attach the event listener.
 * @param {string} type A string representing the event type to listen for
 *     (e.g. load, click, etc.).
 * @param {function()} callback The function that receives the notification.
 */
function addListener(element, type, callback) {
 if (element.addEventListener) element.addEventListener(type, callback);
 else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
</script>


我想跟踪的按钮:

<div id="button" class="small-12 medium-4 large-4 columns foot-btn">
   <a href="/contact" alt="Contact Us">Contact Us</a>
</div>


我尝试了<a id="button" href="/contact" alt="Contact Us">Contact Us</a>,但是没有显示任何结果。提前致谢。

最佳答案

在调用“ addListener”之后,就会发生函数定义。您需要先移动该定义,然后才能实际使用它。

function addListener(element, type, callback) {
    if (element.addEventListener) element.addEventListener(type, callback);
    else if (element.attachEvent) element.attachEvent('on' + type, callback);
}


然后

var downloadLink = document.getElementById('button');
addListener(downloadLink, 'click', function() {
    ga('send', 'event', 'button', 'click', 'contact-button');
});

关于button - Google Analytics(分析)跟踪无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594442/

相关文章:

android - 如何在 Google Analytics 中查看 segment.io 数据

php - 如何调用js而不显示代码

delphi - 通过 Delphi 在网页上按下按钮

javascript - 谷歌分析无法在确认页面上运行

javascript - 如何从 Java 代码触发 Google 跟踪代码管理器脚本

iphone - iPhone 应用程序分析

javascript - 自动完成搜索后跟踪 Google Analytics 中的目标页面

按钮上的 jQuery 事件冒泡在 Firefox 中无法按预期工作

html - (提交)按钮标签的样式部分

button - 在javafx中设置没有背景的imageview周围的边框