Google Analytics Web+App event config: Error 'gtag is not defined'

Viewed 25010

I'm trying to setup ecommerce event configuration of Google Analytics App+Web for a website. Refer to the documentation here.

Because there is a lot of custom code for the ecommerce configuration, I cannot use the general GA App+Web webevent template, so instead I'm using a custom HTML-tag in this case.

As a first test, I tried to fire the following simple event:

<script>

  gtag('event', 'blabla', {
  'test_param1': 'test_value_1'
});

</script>

However, the event is not correctly sent to GA; instead, the following error in the console is returned:

Uncaught ReferenceError: gtag is not defined

To reproduce, see the following page: https://www.hema.nl/mooi-gezond/make-up/make-up-accessoires/toilettassen-1/toilettas-11830901.html

Additional information:

  • The GA App+Web base initialization is (successfully) done via GTM, via the GA App+Webconfiguration template tag.
  • I also configured a few other (simple non-ecommerce) GA App+Web events via GTM via the GA App+Web webevent template tag , and these events are sent to GA successfully.

Does anyone know what's going wrong, why the console error appears and why the event is not sent to GA?

6 Answers

Short answer

Additionally to the GTM GA App+Web tag, you need to add the following code to have gtag() defined:

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
</script>

Long answer

According to the official documentation (https://developers.google.com/analytics/devguides/collection/ga4) this is the code you should include in your site:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'MEASUREMENT_ID');
</script>

After inspecting a website with this tag I found GTM has injected that first line, which is enough to make analytics work. But it didn't add the other lines, some of which effectively defines the gtag global function as a "syntax sugar" for pushing things to the Analytics Data Layer.

Personally, I find this really strange, since using GTM is the most cannonical of adding Google Analytics to a website and it's by the same company.

The cause of the problem has been found. The problem seemed to be that gtag() does not work if the App+Web base setup is done with the GTM App+Web base template tag. Instead, an event should first be pushed to the GTM datalayer, and then the GTM App+Web event template should be used to send the event to Google Analytics.

Lots of thanks Gino (working at Merkle) for fixing the issue!

In my case Uncaught ReferenceError: gtag is not defined was caused by uBlock/adblock turned on.

I think it's worth testing if accepted answer doesn't work.

2022 Year answer:

For everybody struggling with using GA4 gtag and GTM (Google Tag Manager) simultaniously in your SPA/Angular/React app, you should do next:

  • don't use built-in GTM's "Google Analytics: GA4 Configuration" tag configuration
  • do use "Custom HTML" GTM tag with exactly that gtag JS code from your Google Analytics, i.e.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id= GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID');
</script>

After that you can use gtag in your code as you intended to do, all should work. No whole Firebase needed or something. Possibly you will need to have

gtag('config', 'GA_MEASUREMENT_ID', {
  send_page_view: false
});

to disable automatic page_view tracking.

If you're using Google Tag Manager, the gtag function doesn't exist. Follow this guide to create custom events

Basically the code you use is

dataLayer.push({event: 'event_name'});

Then you setup a tag in GTM for GA4 event. Use the event name that you want to see in Google analytics. For the trigger, use custom event. Here, in the event name, use the name that you used in your code event_name

I found out that it was due to an AD Blocking Extensions added to my chrome

Related