March 23, 2024, 7:23 a.m. | Sh Raj

DEV Community dev.to


⨯ 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



        <script
async
src="https://www.googletagmanager.com/gtag/js?id=G-X1234"
></script>
<script>
window.dataLayer = window.dataLayer || []; function gtag()
{dataLayer.push(arguments)}
gtag('js', new Date()); gtag('config', 'G-X1234');
</script>


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 | <script>
49 | window.dataLayer = window.dataLayer || []; function gtag()
> 50 | {dataLayer.push(arguments)}
| ^
51 | gtag('js', new Date()); gtag('config', 'G-X1234'); …

analytics app async config error function google javascript next.js react website will

Data Architect

@ University of Texas at Austin | Austin, TX

Data ETL Engineer

@ University of Texas at Austin | Austin, TX

Lead GNSS Data Scientist

@ Lurra Systems | Melbourne

Senior Machine Learning Engineer (MLOps)

@ Promaton | Remote, Europe

Director, Clinical Data Science

@ Aura | Remote USA

Research Scientist, AI (PhD)

@ Meta | Menlo Park, CA | New York City