我的脚本如下:
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxxx-1');
console.log('test');
console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' }));
</script>
</head>
<body>
...
<a onclick="gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' });" class="test" href="...">
<div class="text">test</div>
</a>
...
</body>
</html>
console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' }));
</script>
的console.log是undefined
点击测试按钮后,我尝试检查Google Analytics(分析)。这样的结果:
总事件数= 0
这怎么可能发生?代码编写过程是否错误?
答案 0 :(得分:1)
答案 1 :(得分:1)
gtag.js事件跟踪语法如下:
gtag('event', 'action', {'event_category': 'category', 'event_label': 'label', 'value': value});
ref:https://developers.google.com/analytics/devguides/collection/gtagjs/events
event_label和value是可选的。
根据@Michele Pisani的评论event_action不是有效的参数
以下内容适用于您的链接
<a onclick="gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_label':'Live365BroadcastLaunch-ListenLive' });" class="test" href="...">
有时,当链接在同一浏览器窗口中加载新页面时,它发生在GA发送和跟踪事件点击之前。在这些情况下,将target =“ _ blank”添加到链接会有所帮助