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.