Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Google Analytics 4 does not work with Nextjs Script tag

I have built a site with Nextjs. I tried integrating GA to the site. I tried to implement with next/script tag but GA does not show reports, users, pageviews, etc. on the dash board. But if I use normal script tag then it works.

Why Google Analytics is not working with next/script tag.

GA Code Snippet with next/script [not working]

<Script
  src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_MEASUREMENT_ID}`}
  strategy="afterInteractive"
  />
  
<Script id="google-analytics-script" strategy="afterInteractive">
        {`
              window.dataLayer = window.dataLayer || [];
              function gtag(){dataLayer.push(arguments);}
              gtag('js', new Date());
              gtag('config', '${process.env.GA_MEASUREMENT_ID}');
        `}
 </Script> 

Note: I have also tried setting ga code inside dangerouslySetInnerHTML with next/script but it still does not work.

GA code snippet with regular script tag [working]

<script
  async
  src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_MEASUREMENT_ID}`}
 />

 <script
   dangerouslySetInnerHTML={{
   __html: `
   window.dataLayer = window.dataLayer || [];
   function gtag(){dataLayer.push(arguments);}
   gtag('js', new Date());
   gtag('config', '${process.env.GA_MEASUREMENT_ID}', { 'send_page_view': true });
            `,
   }}
 />

Now my simple question is that why google analytics is not working with next/script?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!