I want to add tracking code in Next.js but its not working.
<Script>
window.dataLayer = window.dataLayer || [];
function gtag(){
dataLayer.push(arguments)
}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxxxx-1');
</Script>
Try this code may work for you. I put the script in _app.js
function MyApp({ Component, pageProps }) {
return (
<>
<script dangerouslySetInnerHTML={{
__html: `Your script`}}></script>
<Component {...pageProps} />
</>
);
}
export default MyApp;