当我按下按钮时,我试图获取一个事件,但我在我的 Google Analytics(分析)页面上看不到我的活动的任何线索。我遵循了此页面中的 Google Analytics 教程https://developers.google.com/analytics/devguides/collection/analyticsjs/events
按照该说明,我创建了一个 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 的仪表板收到任何信息,所以我无法理解我的 jJavascript 出了什么问题。