admin管理员组

文章数量:1287104

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?

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?

Share Improve this question edited Dec 1, 2014 at 9:44 Tzach asked Dec 1, 2014 at 9:06 TzachTzach 13.4k15 gold badges76 silver badges117 bronze badges 7
  • 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 Commented Dec 1, 2014 at 9:43
  • @EikePierstorff thanks, actually I'm setting the event action. Just updated the question. – Tzach Commented Dec 1, 2014 at 9:45
  • 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 Commented Dec 1, 2014 at 9:49
  • @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 Commented Dec 1, 2014 at 9:55
  • 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 Commented Dec 1, 2014 at 10:01
 |  Show 2 more ments

3 Answers 3

Reset to default 8

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.

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

>

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

本文标签: javascriptGoogle Analytics Custom Events not firing when using Google Tag ManagerStack Overflow