Google Analytics in React with Next + Extra Trick

Tiempo de lectura: < 1 minuto

Today we’re going to add the Google Analytics tag using React and Next.

To add the tag, we need to have a Google Analytics account created.

We’ll go to our <Head> tag and add:

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

Where IDGoogleAnalytics will be our ID: G-XXXXXX

Extra: Improve bounce rate performance.

With this script, we can improve the bounce rate our website returns.

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

Leave a Comment