How to use google analytics with next.js app?
Next.js since v11 recommends using their <Script> tag, and the right place to add it is the App component. pages/_app.jsx import React from ‘react’; import Script from ‘next/script’; const App = ({ Component, pageProps }) => { return ( <> <Script src=”https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxxxx” strategy=”afterInteractive” /> <Script id=”google-analytics” strategy=”afterInteractive”> {` window.dataLayer = window.dataLayer || []; function gtag(){window.dataLayer.push(arguments);} … Read more