Recently I have integrated GTM in my NextJs project. Before the integration was done I used to get 96 on lighthouse performance score. After integration, the score came down to 38. Is there any efficient way of integrating GTM without hampering the performance of the website?
My integration as follows - in _document.tsx file:
<Head>
<link rel="shortcut icon" href="/favicon.ico" />
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
defer
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', {
page_path: window.location.pathname,
});
`
}}
/>
</Head>