Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
HT-Moh opened this issue Jul 29, 2021 · 8 comments
Closed

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

HT-Moh opened this issue Jul 29, 2021 · 8 comments
Labels
bug Issue was opened via the bug report template.

Comments

@HT-Moh
Copy link

HT-Moh commented Jul 29, 2021

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"
  />
@HT-Moh HT-Moh added the bug Issue was opened via the bug report template. label Jul 29, 2021
@wwwdepot
Copy link

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,
      });
      `,
      }}
    />

@imaksp
Copy link
Contributor

imaksp commented Jul 30, 2021

I think you should use two tags as mentioned by @wwwdepot & for analytics, tag manager you should use default script loading strategy (afterInteractive) as mentioned in official docs.

@HT-Moh
Copy link
Author

HT-Moh commented Jul 30, 2021

@wwwdepot thanks, but that it was the first thing I tried, but it didn't work.
image

@imaksp
Copy link
Contributor

imaksp commented Jul 31, 2021

@wwwdepot thanks, but that it was the first thing I tried, but it didn't work.
image

@HT-Moh Hi it seems you have some kind of Ad blocker installed its not next js error, try in incognito mode.

@smakosh
Copy link
Contributor

smakosh commented Aug 2, 2021

Also make sure you're adding those 2 Script tags to your _app component and not _document

@HT-Moh
Copy link
Author

HT-Moh commented 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

@ijjk
Copy link
Member

ijjk commented Sep 21, 2021

Hi, this has been updated in the latest version of Next.js v11.1.2 specifically #27903 please update and give it a try!

@ijjk ijjk closed this as completed Sep 21, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

6 participants