Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda