-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
Comments
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,
});
`,
}}
/> |
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. |
@wwwdepot thanks, but that it was the first thing I tried, but it didn't work. |
Also make sure you're adding those 2 Script tags to your |
Hi, this has been updated in the latest version of Next.js |
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. |
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"
/>`
I disable
next-pwa
to make sure its not the service worker , the error persists.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
The text was updated successfully, but these errors were encountered: