moses toh
moses toh

Reputation: 13192

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

after I clicked on the test button, I tried to check on google analytics. the result like this :

enter image description here

there are seen total events = 0

how could this happen? Is the code writing process wrong?

Upvotes: 0

Views: 2035

Answers (2)

Bronwyn V
Bronwyn V

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

XTOTHEL
XTOTHEL

Reputation: 5208

You need to look at the real-time reports, for other views, the data could take up to 24-48 hours: enter image description here

The code/implementation provided works fine as-is, make sure you don't have any ad/tracking blockers: enter image description here enter image description here

Upvotes: 1

Related Questions