Greg Overmonds
Greg Overmonds

Reputation: 1

simple javascript google analytics event error, not sending to ga

I've got a button on my site that I want to track as an GA event. The javascript function is properly redirecting the user but the GA call is not showing anything in the real-time view of google analytics and have no idea why not. It is showing my visit in Google analytics (the only 1..) so it seems me, not trigging the event.

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-138599174-1');
</script>

<script>  
  function handleOutboundLinkClicks() {
    ga('send', 'event', {
      eventCategory: 'GVoteRSVP',
      eventAction: 'click',
      transport: 'beacon'
    });

    window.location.href = "URL";  
  }
</script>

BUTTON:

<a href="#" onClick="handleOutboundLinkClicks()" class="sqs-block-button-element--medium sqs-block-button-element" >
    REGISTER TODAY
</a>

Any help would be greatly appreciated!!

Upvotes: 0

Views: 335

Answers (1)

kgrg
kgrg

Reputation: 1633

If you use gtag to initialize Analytics, you need to use the same method to send events. In your code you are currently referring to ga() instead. Your function should look like this:

function handleOutboundLinkClicks() {
  gtag('event', 'click', {
    'event_category': 'GVoteRSVP'
  });

  window.location.href = "URL";

}

Generally, the event parameters can be passed like this:

gtag('event', <action>, {
  'event_category': <category>,
  'event_label': <label>,
  'value': <value>
});

You can find further details in this guide. If you need to set the transport type to beacon, as it is used in your current code, you might want to read this documentation as well.

Upvotes: 1

Related Questions