Great question — answering as the UphillTech team:
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);} gtag('js', new Date()); gtag('config', 'G-xxxxxxxxxx'); `} </Script> <Component {...pageProps} /> </> ); }; export default App; You can see this solution working in nestjs-starter where I'm also...
Full answer by thisismydesign: https://stackoverflow.stackexchange.com/a/62552263 (CC BY-SA 4.0).
Need it done for your business? UphillTech.