This nextjs app is an exmaple of implementing google analytics
Installing the dependencies
- Create an app on firebase https://firebase.google.com/
- Make sure you're logged in with the same google account and then open https://analytics.google.com/analytics/web to see the google analytics dashboard
- Select app on the Top-left second button
- Setup stream if not already by going to the stream setup page entering the website name and url
- Create a
pages/_document.tsx
file - refer code
- Create
.env.local
file and append the following key to it
NEXT_PUBLIC_GOOGLE_ANALYTICS=<Your google tracking id>
- Add lib/ga folder to add custom events
- Make changes similar to
_app.tsx
to listen to route change events
- Refer
pages/index.tsx
to log custom events with params