cover-img

Add google analytics to your Next.js app ✨

17 January, 2022

5

5

0

Wassup everyone, let's see how to add google analytics to your Next.js site. Google Analytics will help you to see the performance of each page, how well the site is doing in countries, different devices, etc.

Go to Google Analytics and click on admin in the sidebar.

img

After clicking on admin you will see a screen similar to this-

image.png

Click on "Create Property", now fill in the form with your details.

image.png

You also need to give basic business details.

After you click submit your property will be created! You will see this screen and as we are using Next.js, select web-

image.png

Fill in your website URL and title-

image.png

Click on add on Add new on-page inside Tagging instructions-

image.png

Copy the ID that you see and copy it as we are going to need it!

Go inside pages/_app.tsx and these two Next.js Scripts-

This is going to load google analytics via this script. As you can see we are using env variables for the google-analytics tag because it must remain a secret 🤫, so if you don't already have a .env.local file create one and add the following-

NEXT_PUBLIC_GOOGLE_ANALYTICS=YOUR_GOOGLE_ANALYTICS_ID

Paste in the ID that we copied in the last step! Since we are changing the env variables you need to restart the server too.

As we are using the Next.js script we also need to import it-

import Script from "next/script";

Open your localhost app in one tab and google analytics in another. If you click on Real-time inside of Reports you will see that you have 1 user!

image.png

Adding google analytics to your Next.js app was this easy! Hope you found it useful and insightful. See ya next time ✌️

Google Analytics

Next.js

Connect with me

google analytics

next

nextjs

analytics

5

5

0

google analytics

next

nextjs

analytics

Avneesh Agarwal
15 yo full stack web3 developer building cool shit and teaching others

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.