当我按下按钮时,我正试图举办活动但我在Google Analytics网页上看不到任何有关我活动的线索。 我按照此页面https://developers.google.com/analytics/devguides/collection/analyticsjs/events
中的Google Analytics教程进行了操作按照说明我创建了一个id为“button”的按钮:
<a id="button"><btn class="btn btn-default">Button</btn></a></p>
然后我在文档的主管中包含了将我的页面连接到正确数据库的主要Google Analytics调用(我已更改UA-XXXXXXXX-X','myname.com使用我当前的设置)
<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-XXXXXXXX-X', 'myname.com'); //create a tracker
ga('send', 'pageview'); //send a pageview
</script>
最后我将该代码粘贴到应该跟踪事件的页面底部:
<script>
var downloadLink = document.getElementById('button');
addListener(downloadLink, 'click', function() {
ga('send', 'test', 'button', 'click', 'nav-buttons');
console.log("clicked and sent (hopefully)");
});
/**
* 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>
它看起来对我来说:id是“按钮”,它应该触发对事件的调用。但它不起作用!我没有从Google的信息中心收到任何内容,因此我无法理解我的javascript出了什么问题。
答案 0 :(得分:2)
更改
ga('send', 'test', 'button', 'click', 'nav-buttons');
到
ga('send', 'event', 'button', 'click', 'nav-buttons');