Skip to content

Instantly share code, notes, and snippets.

@harshmaur
Created October 29, 2022 06:41
Show Gist options
  • Save harshmaur/47b635516d4fc348d2c742282302662d to your computer and use it in GitHub Desktop.
Save harshmaur/47b635516d4fc348d2c742282302662d to your computer and use it in GitHub Desktop.
GA Tracking
import Document, { Head, Html, Main, NextScript } from 'next/document'
// You can import from other folder based on your project.
import { GA_TRACKING_ID } from '../utils/constants'
export default class MyDocument extends Document {
render() {
return (
<Html>
<Head>
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} />
<script dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}', { page_path: window.location.pathname, });
`,
}} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment