Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track custom events when Google Analytics is managed by Google Tag Manager?

I ended up having to use Google Tag Manager for my site, so Google Analytics is now a part of that. It's all set up correctly and working fine. However, I used to be able to track custom events on my site very easily, with the ga() function:

ga('send', 'event', 'Videos', 'play', 'Fall Campaign');

However, now ga() is no longer defined; I get an error in the console, ReferenceError: ga is not defined. I then tried the gtag() method and it also doesn't work (same error message):

gtag('event', 'aaa', {
  'event_category' : 'bbb',
  'event_label' : 'ccc'
});

How can I track events with Javascript code?

To be clear, I do NOT want to fuss with the Google Tag Manager. It's a million clicks to get anything done in there. ;-) I just want to call the Javascript directly, like I always could before.

like image 588
Eric Avatar asked Apr 14 '19 13:04

Eric


People also ask

How do I track custom events with Google Tag Manager and Google Analytics?

You can track Google Analytics events via Google Tag Manager by creating a Google Analytics tag of type 'Event' and then creating a specific trigger to track specific time of users' interactions (like PDF Downloads, image link clicks, button clicks, form submissions, form fields, video watching, scrolling, external ...

How do I track custom events?

Tracking a Custom Event: Call to Action Clicks. To track clicks on a call-to-action button using GA, we will need three things: A website with some call-to-action (CTA) buttons. A functional Google Analytics (GA) setup sending proper analytical data.


1 Answers

Google Tag Manager (GTM) by default uses a random name for each tracker, generated for each Universal Analytics tag. AS GTM creates only named trackers, therefore no default (t0) tracker will be created. As no default tracker is created, you must provide a tracker name yourself. Therefore, you would need to now the tracker names, or somehow query and reference them, in order this to work, only by using ga() call directly from JavaScript.

There is a possibility to use fixed name for trackers, which is highly discouraged.

I have a recommended workaround for this. You still need to make a few GTM settings, but you can create a Google Analytics general event dispatcher tag, which you can later use to send any Analytics event calls from JavaScript.

Your tag setting will look something like this, where you can reference your Analytics settings, and all the event related variables from dataLayer:

enter image description here

Your event tracking code will look like this:

dataLayer.push({
  'event': 'GAEvent',
  'eventCategory': 'Videos',
  'eventAction': 'play',
  'eventLabel': 'Fall campaign', 
  'eventValue': undefined,
  'eventNI': true 
});

Please note, that it is recommended to always set all the relevant event values in dataLayer, even if no string or value should be tracked, so that no earlier dataLayer values would be inherited during the push merge. Also, you should update the non-interaction flag, as needed in your case. Your trigger should match the value of the event key.

like image 200
kgrg Avatar answered Oct 31 '22 01:10

kgrg