Events not being tracked in new Google Analytics (analytics.js) setup

If you are using Google Tag Manager and also want to trigger some events via code, ga('send'...) does not appear to be enough. You need to first fetch the appropriate analytics object:

if ("ga" in window) {
    tracker = ga.getAll()[0];
    if (tracker)
        tracker.send("event", "Test", "Test GA");
}

Note that this assumes you're only using a single Google Analytics Tracking code on your site. If you happen to be using multiple, you may need to fetch the appropriate one by name or index.


New version of analytics has a new syntax. Replace the line below;

ga('send', 'event', 'button', 'click', 'contact form');

with this;

gtag('event', 'click', {'event_category' : 'button',
  'event_label' : 'contact form'});

Reference; https://developers.google.com/analytics/devguides/collection/gtagjs/events