question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

uncaught (in promise) undefined using next/script, google analytics.

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

v16.5.0

What browser are you using?

Chrome, Firefox

What operating system are you using?

ubuntu

How are you deploying your application?

docker using next start using in my vm

Describe the Bug

Adding the following google analytic using the import Script from 'next/script' create an issue

<Script src={https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`} dangerouslySetInnerHTML={{ __html: window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', { page_path: window.location.pathname, }); , }} strategy=“lazyOnload” />`

image

I disable next-pwa to make sure its not the service worker , the error persists.

image

Expected Behavior

It should be simply work and import the js file without any errors.

To Reproduce

Add the the following script to reproduct the issue

  <Script
    src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
    dangerouslySetInnerHTML={{
      __html: `window.dataLayer = window.dataLayer || [];
      function gtag() {
        dataLayer.push(arguments);
      }
      gtag('js', new Date());
      gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', {
        page_path: window.location.pathname,
      });
      `,
    }}
    strategy="lazyOnload"
  />

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
HT-Mohcommented, Aug 2, 2021

@aksdevac thanks that was issue. @smakosh I don’t have the script on _document neither the _app , according to the doc it can be anywhere besides _document

1reaction
wwwdepotcommented, Jul 29, 2021

I think you need to use 2 Script tags instead of one.

    <Script
      src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
      strategy="lazyOnload"
    />
    <Script
      strategy="lazyOnload"
      dangerouslySetInnerHTML={{
        __html: `window.dataLayer = window.dataLayer || [];
      function gtag() {
        dataLayer.push(arguments);
      }
      gtag('js', new Date());
      gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', {
        page_path: window.location.pathname,
      });
      `,
      }}
    />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught ReferenceError: ga is not defined - Stack Overflow
I created a simple HTML with the same JS and click handler and ran it in my local apache server, it just works...
Read more >
Google Analytics does not work with Next.js Script tag #37957
If I use GA with next/script tag it won't work. But if I use normal script tag then it works. GA code snippet...
Read more >
next-script-for-ga - Next.js
An inline script was used for Google analytics which might impact your webpage's performance. Possible Ways to Fix It. Using gtag.js.
Read more >
@andrewmumblebee/consent-manager - npm
It works by taking control of the analytics.js load process to only load destinations that the user has consented to and not loading...
Read more >
cannot read properties of null (reading 'textcontent') - You.com
Additionally, you're using id on elements that are repeated from a list, ... Uncaught TypeError: Cannot read properties of undefined (reading 'textContent').
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found