how to set up a @vercel #react #next project to use a @google global site tag (@google analytics) #webdev #seo
how to set up a @vercel #react #next project to use a @google global site tag (@google analytics) #webdev #seo
simple usage
- set a vercel env var GTAG to your google tag id
- add /pages/_document.js to your project
/pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document {
render() {
var gtag = process.env.GTAG
return (
<Html>
<Head>
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${gtag}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtag}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
advanced google site tag pageviews and events usage
- Analytics (google.com)
- Add gtag.js to your site | Universal Analytics for Web (gtag.js) (google.com)
- Measure pageviews | Universal Analytics for Web (gtag.js) (google.com)
- Measure Google Analytics Events
- Using Google Analytics with Next.js | hoangtrinhj.com
- javascript - How to use google analytics with next.js app? - Stack Overflow
google site tag & google analytics statistics