Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google analytics: dataLayer.push not working?

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?

like image 736
kenpeter Avatar asked Jan 30 '15 12:01

kenpeter


1 Answers

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');
like image 53
Simo Ahava Avatar answered Oct 11 '22 16:10

Simo Ahava