Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Custom Events not firing when using Google Tag Manager

I'm trying to track custom events and pageviews in Google Analytics. When including the GA tracking code (Universal Analytics) directly into my pages, events fire normally. However when including the code using Google Tag Manager, nothing is fired except for the initial pageview.

This is the code for firing custom events:

ga('send', 'event', 'test', 'test');

I tested using the console and in both cases ga is defined, and the above code doesn't throw any errors.

I also tried to find some configuration option in GTM that's blocking my events, but couldn't find anything useful.

Any ideas what's preventing the custom events from being fired?

like image 536
Tzach Avatar asked Feb 11 '23 11:02

Tzach


2 Answers

You can't use the same code to track events using Universal Analytics and GTM. When you switch to GTM, you have to push events to the dataLayer and then fire tags based on rules. You can not fire tags directly using analytics.js.

like image 148
kevintechie Avatar answered Feb 14 '23 00:02

kevintechie


you should migrate your code to use the dataLayer instead. Something like this:

javascript">
        window.onload = function() {
            if (window.dataLayer) {
                dataLayer.push({'virtualPageView': {
                    category: 'Virtual Page View',
                    event: '/buy.html',
                    label: '10 Koowong Avenue',
                }});
            }
        }
    </script

>

like image 38
Elise Chant Avatar answered Feb 13 '23 23:02

Elise Chant