Skip to content

Commit 52ac470

Browse files
committed
add analytics
1 parent 8dc4249 commit 52ac470

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

.env.local.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Google Analytics
2+
# Get your measurement ID from https://analytics.google.com/
3+
# It should look like: G-XXXXXXXXXX
4+
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX

pages/_document.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Html, Head, Main, NextScript } from 'next/document';
2+
3+
export default function Document() {
4+
const GA_MEASUREMENT_ID = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID;
5+
6+
return (
7+
<Html>
8+
<Head>
9+
{/* Google Analytics */}
10+
{GA_MEASUREMENT_ID && (
11+
<>
12+
<script
13+
async
14+
src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`}
15+
/>
16+
<script
17+
dangerouslySetInnerHTML={{
18+
__html: `
19+
window.dataLayer = window.dataLayer || [];
20+
function gtag(){dataLayer.push(arguments);}
21+
gtag('js', new Date());
22+
gtag('config', '${GA_MEASUREMENT_ID}', {
23+
page_path: window.location.pathname,
24+
});
25+
`,
26+
}}
27+
/>
28+
</>
29+
)}
30+
</Head>
31+
<body>
32+
<Main />
33+
<NextScript />
34+
</body>
35+
</Html>
36+
);
37+
}
38+

0 commit comments

Comments
 (0)