Skip to content

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

@HT-Moh

Description

@HT-Moh

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"
  />

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions