Adding Google Analytics to Your React/Next.js App: A Step-by-Step Guide

Posted by






How to add Google Analytics to your React/Next.js app

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.


0 0 votes
Article Rating
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Instagram Account
7 months ago

Don't you think you should apply it to app page

Ziyad Salem
7 months ago

Minute 8:55! That is what fixed my problem with calling an env variable in my code. Thank you!

Peter Lee
7 months ago

This video not available on Next.js version 13.

Jenna Rou
7 months ago

This video is really good! God bless you!

Raghunath Thilagar
7 months ago

please do a video about adsense setup for next js 13

Siddharth Ponnapalli
7 months ago

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!

Donut Duck
7 months ago

Why do you not use .env.local instead of .env ???

Ayush Gogna
7 months ago

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

Francis
7 months ago

Do we need to make the website go live or can we do it locally?

CNK Vintage
7 months ago

Is it updated for Google Analytics 4?

Ankit Chaurasia
7 months ago

This is very basic. It won't register pageviews on route change

John Paul Mariano
7 months ago

Thanks Chris!

Eldin Dezmic
7 months ago

How I'm I not seeing events. I can only see js and config in dataLayer

Coding Curricula
7 months ago

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.

Lin Chen
7 months ago

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!