4

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?

Tzach
  • 12,889
  • 11
  • 68
  • 115
  • 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 Dec 01 '14 at 09:43
  • @EikePierstorff thanks, actually I'm setting the event action. Just updated the question. – Tzach Dec 01 '14 at 09: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 Dec 01 '14 at 09: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 Dec 01 '14 at 09: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 Dec 01 '14 at 10:01
  • @EikePierstorff Correct me if I'm wrong but it seems that your suggestion will listen to every link click. I just want to listen to my custom events that many of them are not triggered by link clicks. – Tzach Dec 01 '14 at 10:10
  • Yes, that's the way GTM works - you listen to every link but fire only when certain conditions are fullfilled (i.e. you would amend the rule with a condition "element url eq mygoshaneventhappend.html"). The event listener is there to populate the event, the rule is what actually activates the tag. Without a rule nothing happens (as you are currently witnessing). – Eike Pierstorff Dec 01 '14 at 10:19

3 Answers3

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.

kevintechie
  • 1,441
  • 1
  • 13
  • 15
2

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

>

Elise Chant
  • 5,048
  • 3
  • 29
  • 36
1

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

Pinaki
  • 792
  • 8
  • 17