Меню

Gtag is not defined ошибка

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?

asked Apr 5, 2020 at 10:26

Timo Rietveld's user avatar

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.

answered Feb 21, 2021 at 13:27

Cristiano Dalbem's user avatar

3

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!

answered Apr 7, 2020 at 12:48

Timo Rietveld's user avatar

Timo RietveldTimo Rietveld

3831 gold badge5 silver badges19 bronze badges

1

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.

answered Oct 4, 2021 at 8:58

dpatryas's user avatar

dpatryasdpatryas

2903 silver badges11 bronze badges

2022 Year answer:

For anyone 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.

answered Apr 12, 2022 at 14:06

Vladimir Tolstikov's user avatar

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

answered Oct 31, 2021 at 15:14

Whip's user avatar

WhipWhip

1,60719 silver badges41 bronze badges

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

answered Feb 19, 2022 at 13:41

植植心's user avatar

植植心植植心

3672 silver badges4 bronze badges

JavaScriptJavaScript

Sep 19, 2019
·
Updated: Jul 12, 2021 · by Tim Kamanin

Are you staring at a pesky «Uncaught ReferenceError: ga is not defined» console error message right now?

Yeah, I feel your pain, I’ve just lost two hours of my life figuring it out. Let me save this time for you.

This message means ga is not available for the code that tries to use it. And ga stands for «Google Analytics». ga is a global object used by the legacy analytics.js snippet.

Let’s troubleshoot

  1. Open a debug console and type: window.ga. Did you get undefined? If yes, that go to the next step. If not, then your problem is in code order. Make sure https://www.google-analytics.com/analytics.js gets included before you call ga() function. This one is easy, and that wasn’t my case. Let’s move on.

  2. Okay, so window.ga says «undefined» to you. If you’re sure that Google analytics is enabled on your website (via wordpress/drupal or whatever else plugin, just like in my case), the only reason why it’s still not working is because your website includes Google analytics in a modern, «Global Site Tag» way. View a source of your page and try to locate a https://www.googletagmanager.com/gtag/js string. Got it? Yeah, right. It means you can’t use ga() in your code and you need either:

    a. Rollback to the legacy analytics.js code, or

    b. convert existing code, so it’s compatible with gtag.js. Read here for more info on migrating from analytics.js to gtag.js.

My case

My issue was related to Google Optimize. Every doc and tutorial was telling me to use ga('require', 'GTM-XXXXXX'); to enable Google Optimize container. But since I had Google Analytics included as gtag.js I needed to use:

gtag("config", "UA-GA_TRACKING-CODE", { optimize_id: "GTM-XXXXXX" });

Did it help? You’re welcome! Follow me on Twitter @timonweb and let me know if this post was helpful.

Hey, if you’ve found this useful, please share the post to help other folks find it:

Ecommerce Marketing

SEO, AdWords, affiliates, advertising, and promotions


Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.


Showing results for 


Show  only 

|


Search instead for 

Did you mean: 

To continue receiving payouts, you need to secure your account by turning on two-step authentication. If two-step authentication is not turned on your payouts will be paused. Learn more

  • Shopify Community

  • Shopify Discussion

  • Ecommerce Marketing

  • gtag is not defined on Thank you page

Options

  • Subscribe to RSS Feed
  • Mark Topic as New
  • Mark Topic as Read
  • Float this Topic for Current User
  • Bookmark
  • Subscribe
  • Mute
  • Printer Friendly Page


  • All forum topics


  • Previous Topic

  • Next Topic
    Next topic

Replies 3 (3)

Tony_Bui

  • Mark as New
  • Bookmark
  • Subscribe
  • Mute
  • Subscribe to RSS Feed
  • Permalink
  • Print
  • Report Inappropriate Content

‎09-15-2019

10:55 PM

Just keep in mind you just need to insert UA-…. code into the Google Analytics account field.

Don’t forget to enable «Enhanced Ecommerce» to track Conversion Report of GA.

01.jpg

:red_heart: Found my answer helpful? Please LIKE or Accept Solutions.
:red_heart:Top 30 Best Shopify Themes Free + Premium: A Complete Review Read More

Astha

  • Mark as New
  • Bookmark
  • Subscribe
  • Mute
  • Subscribe to RSS Feed
  • Permalink
  • Print
  • Report Inappropriate Content

‎09-16-2019

02:55 AM

Hii,

I think you are talking about GTM code setup first check gtag code is it properly inserted in the backend of the website . It should be properly inserted in the head and body section of a website. After this, you can check in google tag manager in the preview section.
Once you will verify it you can track analytics and other tracking data for other queries you can contact me.

MageNative | Mobile App for your Shopify Store | Native Android and iOS Apps

Digital Marketing Analyst
Skype: live:asthaupadhyay_1
E-mail: asthaupadhyay@magenative.com

  • All Unanswered Topics
  • Community Blog
  • Community Guidelines
  • Code of Conduct
  • Terms of Service
  • Privacy Policy

  • Shopify Community

    • Click to collapse Click to expand Shopify Discussion category Click to collapse Shopify Discussion category

      Shopify Discussion


      • Shopify Discussions


      • Store Feedback


      • Shopify Design


      • Announcements


      • Shopify Apps


      • Shopify Translate & Adapt


      • Ecommerce Marketing


      • Technical Q&A


      • Site Speed


      • Retail and Point of Sale


      • International Commerce


      • Payments, Shipping, and Fulfillment


      • Accounting and Taxes


      • Wholesale and Dropshipping


      • Shopify Flow App


      • Shopify Scripts


      • Jobs and Careers

    • Click to expand Click to expand Partners and Developers category Click to collapse Partners and Developers category

      Partners and Developers


      • Shopify APIs and SDKs


      • Sales Channels, Payments Platform & Wallet API


      • Storefront API and SDKs


      • Subscription APIs


      • Online Store 2.0


      • Fulfillment API Deprecation


      • Shopify Functions

    • Click to expand Click to expand Groups category Click to collapse Groups category

      Groups

      • Click to expand Click to expand Regional category Click to collapse Regional category

        Regional


      • Education


      • Voices

      • Click to expand Click to expand Products & Services category Click to collapse Products & Services category

        Products & Services


    • English video guides and tutorials


    • Blog

Community Blog Articles

  • Shopify Community

    Shopify Design
    Shopify Discussions
    Shopify APIs and SDKs
    Technical Q&A
    Payments, Shipping, and Fulfillment

  • Support

    24/7 Support
    Shopify Help Center
    API documentation
    Free Tools

  • Shopify

    Contact
    Partner Program
    Affiliate Program
    App Developers
    Investors

  • Quick Links

    Register
    Log in

Adobe Analytics Coffee Break: On Monday, January 30 at 10am PT, the NOAM User Group led by Jennifer Dungan and Jeff Bloomer will be joining us to answer your Analytics related questions. To register and for more information, follow the link to the right.

Dear All,

We need to check that Google floodlight pixel working or not so we need to check Gtag(Global site tag) and then we typed gtag in console and its showing gtag is not defined. 

It means gtag is missing/removed right? 

Also I would like to know that in how many ways we can check this «how to identify gtag is present or not.»

Thank you

kamleshmaddheshiya_0-1655463593132.png

1 Accepted Solution

How have you implemented gtag?

Normally, you can query for gtag in the browser console, like what you’ve done. You could also install the Google Tag Assistant extension in your browser and see if it detects the Google tags appropriately.


  • All forum topics


  • Previous Topic

  • Next Topic

1 Reply

How have you implemented gtag?

Normally, you can query for gtag in the browser console, like what you’ve done. You could also install the Google Tag Assistant extension in your browser and see if it detects the Google tags appropriately.

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Gta vice city ошибка 640 480
  • Gta vice city definitive edition ошибка при запуске