Reputation: 13192
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>
is 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
how could this happen? Is the code writing process wrong?
Upvotes: 0
Views: 2035
Reputation: 774
gtag.js event tracking syntax is the following:
gtag('event', 'action', {'event_category': 'category', 'event_label': 'label', 'value': value});
ref: https://developers.google.com/analytics/devguides/collection/gtagjs/events
event_label and value are optional.
as per @Michele Pisani comment event_action is not a valid parameter
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. In these instances adding target="_blank" to the link can help
Upvotes: 2
Reputation: 5208
You need to look at the real-time reports, for other views, the data could take up to 24-48 hours:
The code/implementation provided works fine as-is, make sure you don't have any ad/tracking blockers:
Upvotes: 1