Search code examples
google-analyticsanalyticsgtag.js

gtag events not registering in analytics


I've looked at countless answers but been unable to find the right solution. I'm trying to get Google analytics to register a goal once a contact number has been clicked, using an event registration (onclick).

I've got the following code, which is not registering any results. What's the thing I'm missing?

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-XX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXX-XX');
</script>

onclick="gtag('event', 'click', { 'send_to': 'UA-XXXXXX-XX', eventCategory: 'Telefoon ', eventAction: 'click', eventLabel: 'Telefoonnummer aangeklikt', eventValue: 1});"

enter image description here


Solution

  • The syntax for the onclick code is incorrect. send_to is not necessary if only a single tracking code is being used on the site. If more than one tracking code is in use then you can configure it to send (or route) data to individual GA Properties or groups of accounts or products.
    Ref: https://developers.google.com/gtagjs/devguide/routing

    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.

    The following should work for your link

    onclick="gtag('event', 'click', { 'event_category': 'Telefoon', 'event_label':'Telefoonnummer aangeklikt', value: 1 });"