[英]why the gtag event doesn't appear in Google Analytics?
my script like this:我的脚本是这样的:
<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>
The console.log of console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' })); </script>
console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' })); </script>
console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' })); </script>
is undefined
console.log(gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_action':'Click','event_label':'Live365BroadcastLaunch-ListenLive' })); </script>
undefined
after I clicked on the test button, I tried to check on google analytics.单击测试按钮后,我尝试检查谷歌分析。 the result like this:
结果是这样的:
there are seen total events = 0总事件数 = 0
how could this happen?怎么会这样? Is the code writing process wrong?
代码编写过程是否错误?
gtag.js event tracking syntax is the following: gtag.js 事件跟踪语法如下:
gtag('event', 'action', {'event_category': 'category', 'event_label': 'label', 'value': value});
ref:https://developers.google.com/analytics/devguides/collection/gtagjs/events参考:https://developers.google.com/analytics/devguides/collection/gtagjs/events
event_label and value are optional. event_label 和 value 是可选的。
as per @Michele Pisani comment event_action is not a valid parameter根据@Michele Pisani 评论 event_action 不是有效参数
The following should work for your link以下应该适用于您的链接
<a onclick="gtag('event', 'Click', { 'event_category': 'Outbound Link', 'event_label':'Live365BroadcastLaunch-ListenLive' });" class="test" href="...">
Sometimes when a link loads a new page in the same browser window, it happens before the event hit can be sent and tracked by GA.有时,当链接在同一浏览器 window 中加载新页面时,它发生在 GA 可以发送和跟踪事件命中之前。 In these instances adding target="_blank" to the link can help
在这些情况下,将 target="_blank" 添加到链接会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.