gpt4 book ai didi

javascript - 谷歌分析事件跟踪 : button listener does not work

转载 作者:行者123 更新时间:2023-11-30 16:52:12 26 4
gpt4 key购买 nike

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

<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>

我在这里做错了什么?

最佳答案

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

<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>

关于javascript - 谷歌分析事件跟踪 : button listener does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30360969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com