Using google analytics with Next.JS

1 year ago23k times

With the following code, you can use google analytics with Next.js App in the header section

import Script from 'next/script'
import Head from 'next/head'

export default function Index() {
  return (
      <Script id="google-analytics" strategy="afterInteractive">
          window.dataLayer = window.dataLayer || [];
          function gtag(){window.dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'GA_MEASUREMENT_ID');

Add Answer

Add a codeAdd Code
Remove adsremove

Latest codes

view all

Latest Snippets

view all