Skip to content

Instantly share code, notes, and snippets.

@theinfosecguy
Last active October 18, 2021 14:23
Show Gist options
  • Save theinfosecguy/491238e0cec75c543f74aa0562201e65 to your computer and use it in GitHub Desktop.
Save theinfosecguy/491238e0cec75c543f74aa0562201e65 to your computer and use it in GitHub Desktop.
_document.js for NextJS and Google Analytics
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=G-Y0********`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-Y0********', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment