Skip to content

Commit 65f307f

Browse files
authored
Avenmia/add google analytics (#206)
* Adding google analytics * Updating README for google analytics * Fixing errors
1 parent 92d47a4 commit 65f307f

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"@tailwindcss/forms": "^0.5.3",
44+
"@types/gtag.js": "^0.0.19",
4445
"@types/leaflet": "^1.9.0",
4546
"@types/node": "^18.11.18",
4647
"@types/prettier": "^2.7.2",

src/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ npx prisma db push
195195

196196
> Note: The demographic questions will not populate because they are hard-coded in the SQL migrations.
197197
198+
# Add Google Analytics Environment variable
199+
200+
- In the .env file add
201+
202+
```
203+
NEXT_PUBLIC_GA_ID = {MyGoogleAnalyticsID}
204+
```
205+
206+
- This will enable google analytics on the site
207+
198208
# Working with prisma
199209

200210
When the data model changes, run the following to update your local database with the latest migrations

src/pages/_document.tsx

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

0 commit comments

Comments
 (0)