Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda