5
votes

Based on this thread: Tracking events using Google Tag Manager

I created my own version, which is located at e.g. http://test.site.com

<!DOCTYPE html>
<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

  <script>
    window.dataLayer = window.dataLayer || [];  

    dataLayer.push({
      'event':'GAevent',
      'eventCategory': 'App4', 
      'eventAction': 'Click',
      'eventLabel': 'iOS4'
    });


  </script>
</head>

<body>
  <!-- Start google tag manager -->
  <script>
    (function(w, d, s, l, i) {
        w[l] = w[l] || [];
        w[l].push({
            'gtm.start': new Date().getTime(),
            event: 'gtm.js'
        });
        var f = d.getElementsByTagName(s)[0],
            j = d.createElement(s),
            dl = l != 'dataLayer' ? '&l=' + l : '';
        j.async = true;
        j.src =
            '//www.googletagmanager.com/gtm.js?id=' + i + dl;
        f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'primecont', 'GTM-1234');
  </script>
  <!-- End google tag manager -->

</body>
</html>

I turned on google tag manager debug mode and watched it on google analytics real-time.

I have 2 fire rules for a tag:

  1. {{url}} contains test.site.com
  2. {{event}} equals GAevent

what I got is "event category: undefined" & "event action: undefined" in real time google analytics.

If I remove "{{url}} contains test.site.com", nothing is appearing in real-time.

Update I used a separated google tag manager account and create a test page, so everything it is minimum. It seems working in real time. The non-working google tag manager are shared by schools and faculties. I suspect that is the reason?

1
Did you remember to insert the respective macros in you Analytics event tracking tag ? Plus, don't be confused by the fact that both GTM and GA have something called "event", these are completely separate things. - Eike Pierstorff
I'm certain that the problem is what @EikePierstorff mentioned. You didn't create and insert to your Google Analytics tag the corresponding eventCategory,eventAction,eventLabel macros. - georgez
I have created dataLayer marcos for eventCategory,eventAction,eventLabel. Do I need to create a custom event for GAevent? Another question: I set '{{event}} equals GAevent' as a fire rule. Does dataLayer.push({'event':'GAevent', ...}) actually fire the GAevent? - kenpeter
It looks like you fire the tag on this specific page only, based on the placement of the dataLayer push. Is that your intention? There is nothing else that triggers the push other than this page load. - nyuen
My final goal is, if I click a button, then google analytics will record the click action in 'event category'. I figure if I am able to make it working on page load, then I am 1 step closer to my goal. I assume dataLayer.push({'event':'GAevent', ...}) actually fire the GAevent, when the page load? - kenpeter

1 Answers

5
votes

Your Tag is firing with wrong variables for a number of reasons.

If the dataLayer.push() were correct, you Tag would only need the {{url}} matches RegEx .* as its rule, since all pushes that occur before the container snippet are available to the All Pages rule.

However, you can also push the 'event' : 'GAEvent' pre-container-snippet, if you wish. But then you must remove the {{url}} rule, as it would make your tag fire twice: First with {{event}} equals GAEvent and then with the {{url}} rule.

The reason why your code doesn't work even if you fix the above issues is because you have renamed the dataLayer object in your container snippet:

})(window, document, 'script', 'primecont', 'GTM-1234');

the 'primecont' String is the new name for Google Tag Manager's Data Layer that you have given, for some reason. This is why your dataLayer.push() will not work, since Google Tag Manager is listening for a primecont.push() instead.

So, either change all your dataLayer interactions to primecont, or edit the container snippet's invocation line to look like this:

})(window, document, 'script', 'dataLayer', 'GTM-1234');