0

I am trying track events using Google Tag Manager, here is my code:

<!-- Google Tag Manager -->
(function (w, d, s, l, i) {
    w[l] = w[l] || [];
    w[l].push({
        'gtm.start':
            new Date().getTime(), event: 'gtm.js'
    });
    var f = d.getElementsByTagName(s)[0],
        j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
    j.async = true;
    j.src =
        'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
    f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-MYTAG');

window.dataLayer = window.dataLayer || [];

function gtag() {
    dataLayer.push(arguments);
}

gtag('js', new Date());

gtag('config', 'GTM-MYTAG');

function trackEvent(eventName, eventValue) {
    gtag('event', 'click', {
        'event_category': eventName,
        'event_label': eventValue,
        'value': 1
    });
}

And here is the HTML that fires the event when something is clicked:

<button onclick="trackEvent('Add Button Clicked', 'Header')"
        class="chrome-button chrome-link btn btn-sm btn-sample">
    <div>Add to Chrome - Free</div>
</button>

What am I doing wrong? I have tried going to Google Analytics -> Real Time -> Events and nothing shows up while I click the button. I have also made sure I removed all filters with my IP address. Please let me know if I am missing something.

1
  • I wish I'd never chosen GTM :-( Commented Jun 8, 2021 at 21:03

1 Answer 1

2

You've confused GTM and gtag.js. They're two separate libraries for tracking.

Here are the methods you should use to do GA event tracking in GTM: https://support.google.com/tagmanager/answer/6106716?hl=en

If you just want it to work, then replace GTM with:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

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

Then have this function trigger as you have:

function trackEvent(eventName, eventValue) {
    gtag('event', 'click', {
        'event_category': eventName,
        'event_label': eventValue,
        'value': 1
    });
}

You'll need nothing else.

7
  • Which one am I currently implementing? GTM or gtag.js? Which one should I be using if I want to fire events from Javascript?
    – superdee
    Commented Jan 31, 2019 at 2:18
  • updated my answer, though I'd recommend you look at what Event Tracking is in GA, understand what Event Category, Event Action and Event Label is used for and how the metrics around it all work. Right now you have "eventName' tracked as the category, "eventValue" as the label and 1 as the value. I feel it might not be a good way to organize it.
    – XTOTHEL
    Commented Jan 31, 2019 at 2:22
  • Thanks a bunch, so can I delete the <!-- Google Tag Manager --> section from my code completely? What's the difference between GTM and gtag.js, I don't quite understand. When should I use which one?
    – superdee
    Commented Jan 31, 2019 at 2:30
  • Yes, you only need what I have in my answer for your event tracking to work. I short. gtag.js is the google analytics tracking library. It is meant just for google analytics. GTM is a tag management system, when you create a google analytics tag in GTM, you are essentially including the GA library in your code without modifying the source code for your page. GTM will allow you to add other code on the page as well (facebook, crazyegg, hotjar, custom html, etc).
    – XTOTHEL
    Commented Jan 31, 2019 at 2:34
  • Ah I see, so I was implementing gtag.js, instead of the whole GTM tag system I set up (which included Hotjar, Facebook, etc)?
    – superdee
    Commented Jan 31, 2019 at 2:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.