Last active
October 18, 2021 14:23
-
-
Save theinfosecguy/491238e0cec75c543f74aa0562201e65 to your computer and use it in GitHub Desktop.
_document.js for NextJS and Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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