4
votes

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?

3
You are missing the event action, that's a required parameter (first parameter (after send and hit type): category, second: action, third (optional): Label, fourth (optinonal, integer): event value). - Eike Pierstorff
@EikePierstorff thanks, actually I'm setting the event action. Just updated the question. - Tzach
On what condition (which rule) the event is supposed to be fired ? Do you use the tag template for ga code or a custom html tag ? - Eike Pierstorff
@EikePierstorff I don't use GTM rules to fire the events. They are fired directly from the js or HTML (for example in onclick handlers). I'm using a tag template. - Tzach
You would still need a rule/trigger (depending on the version of GTM, in the current version you'd need to set up a link click listener and add a rule "event eq gtm.linkClick, the new version has event listing baked in). - Eike Pierstorff

3 Answers

6
votes

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.

1
votes

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

>

1
votes

Problem - onclick ga() function wasn't working for me also. In my index page, there was 'Google Tag Manager script' and 'analytics.js' added.

Solution - Instead of ga(), gtag() function was working fine for me with the 'gtag.js'.

onclick="gtag('event', 'Click', {'event_category':'Dashboard', 'event_label':'demoClick', 'value':'30'});"

Click here for details