I added Google Analytics to a Next.js website that I’m building last week, and just added it again to a second site this AM. The process is very smooth. There are only 3 files involved. Two of these, we have to create and one of them should already be in your Next.js project and needs to be edited.
You should also have a free Google Analytics account. I made mine on the fly during my first run-through of this process. It only took a few minutes at most.
Get Ready
- Create or log into your Google Analytics account
- Open your Next.js project in your editor (I use VS Code)
First file – gtag.js
- Create a folder at the root level called “lib”
- Create a new file in “lib” called “gtag.js”
- Head to nextjs.org and click on the GitHub icon on the top-right of the screen
- From the GitHub page, navigate to the “examples” folder, go to the “with-google-analytics” subfolder and then the “lib” subfolder
- In the “lib” folder, open the “gtag’js” file and copy its contents
- Paste the contents into the local “gtag.js” file that’s open in your editor and save it

Get your Measurement ID from Google Analytics and paste it into gtag.js
- Go to Google Analytics (create an account if you don’t have one) and click on “Admin” at the bottom-left of the screen
- Click on “Create Property” – a property is a project
- Give the property a name (I use the website name), time zone and currency
- Give the property an industry category, business size and pick usage rationales
- Click on the “create” button
- Pick a data stream – you can choose more than one from Android, iOS and web
- Enter the website URL and a stream name (I use the website name here, too)
- Click “Create stream” and copy the ‘Measurement ID’
- Paste the “Measurement ID” into the first line in your “gtag.js” file, in “<YOUR_GA_TRACKING_ID>” – overwrite the <> and everything in-between and fill the quotes, and save it

Second file – modify your _app.js file
- Go back to the Next.js GitHub page and navigate to the “pages” directory in “with-google-analytics”
- In the “pages” directory, open “_app.js” and copy its contents
- Go to “_app.js” in your “pages” folder in your editor and paste the contents of “_app.js” from GitHub over it and save it – I keep the global styles import from the old file

Third file – create your _document.js file
- In the “pages” directory, create “_document.js”
- Go back to GitHub and copy the contents of the “_document.js” file and paste it into your local “_document.js” file using your code editor and save it

That’s it! Now, just publish your website and go to Google Analytics.
- Close your “Web stream details” if its still open
- Click on the Home icon at the top of the left toolbar – you can select from different properties (projects) using the drop-down at the top of the screen
- Click on the Realtime icon, right below Home – if you see no activity (0 users), open your website from your browser or a phone and check the Realtime data again, you should now see activity
- If the “Users in the last 30 minutes” card doesn’t update immediately, give it about 2 minutes, and it should come alive

This is the video that I followed with instructions for how to complete this process: