How to add Google Analytics to your React/Next.js app
If you have a React or Next.js app and want to track user behavior and gather insights into your app’s usage, integrating Google Analytics can provide valuable data. In this article, we’ll walk through the steps to add Google Analytics to your React or Next.js app.
Step 1: Create a Google Analytics account
If you don’t already have a Google Analytics account, you’ll need to create one. Go to the Google Analytics website and sign in or create a new account. Once you have an account, you can create a new property to track your React or Next.js app.
Step 2: Install a Google Analytics package
There are several packages available that can help you easily integrate Google Analytics into your React or Next.js app. One popular package is react-ga, which provides a simple way to initialize Google Analytics and send pageviews and events.
To install react-ga, you can use npm or yarn:
npm install react-ga
# or
yarn add react-ga
Step 3: Initialize Google Analytics in your app
Once you have the react-ga package installed, you’ll need to initialize Google Analytics in your app. You can do this by creating a file that initializes react-ga and sets up your Google Analytics tracking ID.
import ReactGA from 'react-ga';
const TRACKING_ID = 'YOUR_TRACKING_ID'; // Replace with your Google Analytics tracking ID
ReactGA.initialize(TRACKING_ID);
Step 4: Track pageviews and events
With Google Analytics initialized in your app, you can start tracking pageviews and events. For example, you can use the react-ga package to send a pageview when a user navigates to a new page in your app:
import ReactGA from 'react-ga';
function TrackPage() {
ReactGA.pageview(window.location.pathname + window.location.search);
return null;
}
Step 5: Verify your tracking
After completing the steps above, you should be able to see data in your Google Analytics account. You can verify that your tracking is working by navigating through your app and checking the Real-Time reports in Google Analytics to see if your pageviews and events are being captured.
By following these steps, you can easily add Google Analytics to your React or Next.js app and start gathering valuable insights into how users are interacting with your app.
Don't you think you should apply it to app page
Minute 8:55! That is what fixed my problem with calling an env variable in my code. Thank you!
This video not available on Next.js version 13.
This video is really good! God bless you!
please do a video about adsense setup for next js 13
Great Video. I've been working on adding Google Analytics to my site. How to do it in Nextjs 13 App router case? With Typescript. Thanks Chris!
Why do you not use .env.local instead of .env ???
does it work instantly or i have wait 48 hours to start seeing analytics coz i have done everything still dont see any data in analytics
Do we need to make the website go live or can we do it locally?
Is it updated for Google Analytics 4?
This is very basic. It won't register pageviews on route change
Thanks Chris!
How I'm I not seeing events. I can only see js and config in dataLayer
Is there a reason to include the Google Analytics tag as an environmental variable? It's in the <head> of the doc so you'll download it with the page regardless.
For latecomers, this video is not using the Script tag that's introduced after Next 11, so be careful using the same method. Nice video!