我想跟踪我的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/