⨯ src/app/[...slug]/layout.js (50:11) @ dataLayer
⨯ ReferenceError: dataLayer is not defined
https://dev.sh20raj.com/sh20raj/add-google-analytics-to-nextjs-website-11gp
if your add like that
window.dataLayer = window.dataLayer || []; function gtag()
{dataLayer.push(arguments)}
gtag('js', new Date()); gtag('config', 'G-X1234');
Error will be shown like
⨯ src/app/[...slug]/layout.js (50:11) @ dataLayer
⨯ ReferenceError: dataLayer is not defined
at RootLayout (./src/app/[...slug]/layout.js:170:29)
48 |
49 | window.dataLayer = window.dataLayer || []; function gtag()
> 50 | {dataLayer.push(arguments)}
| ^
51 | gtag('js', new Date()); gtag('config', 'G-X1234');
52 |
53 |
for Fixing it add Like ( wrap it with a {} with backticks )
{`window.dataLayer = window.dataLayer || []; function gtag()
{dataLayer.push(arguments)}
gtag('js', new Date()); gtag('config', 'G-CWL59Y4TR7');`}
Additionally you can use Script
and https://nextjs.org/docs/messages/next-script-for-ga