How to add Google Analytics to Next.js
As more and more people visited my blog, I reached the limit of data ingestion of at the end of August.
In order to continue supervising the web traffic of my blog, I decided to replace
Vercel Analytics with
Firstly, I need a new account of google analytics. I had one, so I just need create an new
id or use the
id I created before.
For those who do not have google analytics account yet, you can read these toturials,
Google Analytics for Beginners
For decoupling the Google Analytics ID with JSX page in Next.js, I set
NEXT_PUBLIC_ANALYTICS_ID as environment variable in
MEASUREMENT ID into it.
Vercel Analytics with a code snippets from
View tag instructions as below:
Viewing page source and see:
Setting the environment variable,
MEASUREMENT ID, in production,
which will automatically inject
MEASUREMENT ID in the rigit position of the page
after pushing source code to GitHub repository, https://github.com/shenlu.me.