NextJS - Google Analytic code
-
File: _app.js
import Script from "next/script"; import Head from "next/head" const GA_MEASUREMENT_ID = 'G-XXXXXXXX' <Head> <Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`} strategy="afterInteractive" /> <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}'); `} </Script> </Head>
Ref: https://hackernoon.com/setting-up-google-analytics-4-in-a-nextjs-project