+91-9784395621

Digital Locus
  • Home
  • Services
    • Website Development
      • Shopify & Shopify Plus
      • Ecommerce Website
      • Responsive Website
      • Travel Website
      • Hospitality Website
    • Website Design
      • B2B Web Design
    • Website Maintenance
      • Shopify Maintenance
    • SEO
      • Local SEO
      • Ecommerce SEO
      • Outsource SEO
    • Performance Marketing
      • Google Ads Management Services
      • Social Media Marketing Services
    • Enterprise CMS Solutions
      • WordPress
      • Shopify
    • Social Media Marketing
      • LinkedIn
      • Instagram
      • Facebook
      • Twitter
    • Website Redesign
    • Email Marketing
    • Google Analytics
    • Google Tag Management
  • Work
  • Insights
  • Guides
    • Google Analytics
    • Google Tag Manager
LET’S TALK

How to Integrate Google Analytics in React JS?

Written by: 

Sumi Rauf

Fact Checked By:  

Siddharth Jain

Published: 

10/09/2024

Last Updated: 

16/06/2025

Table of Contents

Toggle
  • Introduction: Why Use Google Analytics in React JS?
  • Prerequisites Before Integrating Google Analytics in React JS
  • Step 1: Install Google Analytics in Your React Project
    • Install the package:
    • Integrate in App Component
  • Step 2: Tracking Page Views in React
    • Set up React Router
    • Track Page Views
  • Step 3: Implementing Event Tracking in React with Google Analytics
    • Track Click Events
    • Form Submission Tracking
  • Step 4: Debugging Google Analytics Integration
    • Google Tag Assistant
    • React Developer Tools
  • Step 5: Best Practices for Google Analytics in React
  • Conclusion: Enhancing User Experience with Google Analytics

Introduction: Why Use Google Analytics in React JS?

Being a developer, I would say tracking user behavior is some of the most important knowledge you can gain when working with a React application. how to integrate Google Analytics in React JS is an essential skill for developers seeking to gain these insights. Google Analytics happens to be an industry leader in this area. By implementing how to integrate Google Analytics in React JS, you will be able to monitor traffic patterns, see a user engagement profile, and understand how to improve your application for better user experiences. This integration allows you to collect data on page views, custom events, and even real-time activity, which is crucial for both user retention and marketing efforts.

React applications are often built as Single Page Applications (SPAs), which bring with them a unique set of challenges for tracking page views and user interactions. Learning how to integrate Google Analytics in React JS ensures that routing with no full page reload is correctly captured, as the standard Google Analytics setup may not suffice. This tutorial guide helps you seamlessly configure Google Analytics in your React JS application to ensure that no tracking data is missed, providing complete information about users and their behaviors. Whether you’re building a performance-focused site or working with a LinkedIn marketing agency to enhance your B2B presence, accurate analytics are crucial for making informed, strategic decisions.

Prerequisites Before Integrating Google Analytics in React JS

Well before starting the implementation of how to incorporate Google Analytics in a React JS application, you are going to need some items ready:

  • Google Analytics Account: Set up a Google Analytics account if you have not done so already. To successfully implement how to integrate Google Analytics in React JS, you will need a tracking ID, which is a unique identifier for your application. This tracking ID is essential to connect your React app with Google Analytics and enables accurate data collection. If you’re new to Google Analytics, here’s a link to create a new account and generate your tracking ID. Following these steps ensures a seamless setup, crucial for understanding how to integrate Google Analytics in React JS effectively.
  • React Project Setup: Just ensure that you have the React project set up. If you have a starting point, create a new React app with create-react-app, or use some other project setup boilerplate.
  • Access to Your Website’s Code: You will have to get the ability to edit the codebase for your React application to complete the integration with Google Analytics.

Having these prerequisites in place will ensure that you can easily set up Google Analytics consultant or take advantage of pre-configured scripts for your application.

Also Read: What is Secondary Dimension in Google Analytics

Step 1: Install Google Analytics in Your React Project

Firstly, to start the process, you need to install a Google Analytics library that will handle tracking integration in your React JS app. The most common one is react-ga, which is a specific library for interaction with Google Analytics in a React context.

Install the package:

Use npm or yarn to install the react-ga package:

Copy code

npm install react-ga

or

csharp

Copy code

yarn add react-ga

Set Up Google Analytics:
In your src folder, create a new file named analytics.js and set up the Google Analytics configuration. You’ll need to insert your Google Analytics Tracking ID (it usually looks like UA-XXXXXX-X):
javascript
Copy code
import ReactGA from ‘react-ga’;

const initializeAnalytics = () => {

  ReactGA.initialize(‘UA-XXXXXX-X’); // Replace with your Google Analytics Tracking ID

};

export default initializeAnalytics;

Integrate in App Component

Next, you’ll import and call this initialization function in your main App.js or index.js file to set up Google Analytics when the app starts.
javascript


Copy code
import React from ‘react’;

import ReactDOM from ‘react-dom’;

import ‘./index.css’;

import App from ‘./App’;

import initializeAnalytics from ‘./analytics’;

// Initialize Google Analytics

initializeAnalytics();

ReactDOM.render(

  <React.StrictMode>

    <App />

  </React.StrictMode>,

  document.getElementById(‘root’)

);

By doing this, every time your app loads, it will automatically connect to Google Analytics and begin tracking user behavior.

Also Read: What Does Direct Mean in Google Analytics

Step 2: Tracking Page Views in React

The big challenge of React applications is tracking page views, which is necessary as users go through various routes. This is because React will hardly ever reload the page if changing the route due to the fact that React Router has to deal with it.

Set up React Router


If you are using react-router-dom for navigation, it’s essential to integrate it with Google Analytics so that page views are logged whenever the route changes.
First, make sure you have installed react-router-dom:
Copy code
npm install react-router-dom

Track Page Views

Next, add the tracking logic within your React Router’s useEffect hook to monitor page changes:
javascript


Copy code
import React, { useEffect } from ‘react’;

import { useLocation } from ‘react-router-dom’;

import ReactGA from ‘react-ga’;

const usePageTracking = () => {

  const location = useLocation();

  useEffect(() => {

    // Sends pageview hit to Google Analytics

    ReactGA.pageview(location.pathname + location.search);

  }, [location]);

};

export default usePageTracking;

Now, you can call this usePageTracking hook inside your main component:
javascript
Copy code
import usePageTracking from ‘./usePageTracking’;

const App = () => {

  usePageTracking();

  return (

    <div>

      {/* Your React App Components */}

    </div>

  );

};

This will trigger page view tracking every time the URL changes in your React app.

Step 3: Implementing Event Tracking in React with Google Analytics

Event tracking is another powerful feature of Google Analytics that allows you to track user interactions, such as clicks, form submissions, and other custom events.

Track Click Events


Suppose you want to track button clicks across your app. Here’s how to implement it:
javascript
Copy code
const handleButtonClick = () => {

  ReactGA.event({

    category: ‘User’,

    action: ‘Clicked Button’,

    label: ‘Home Page Button’

  });

};

return <button onClick={handleButtonClick}>Click Me</button>;

This example sends a custom event to Google Analytics when the button is clicked, providing insights into user behavior.

Form Submission Tracking


You can also track form submissions. Here’s an example:
javascript
Copy code
const handleFormSubmit = () => {

  ReactGA.event({

    category: ‘Form’,

    action: ‘Submitted Contact Form’,

    label: ‘Contact Us’

  });

};

Use such event tracking across your app to gain deeper insights into user actions.

Also Read: What Are The Five Sections of The Google Analytics Dashboard

Step 4: Debugging Google Analytics Integration

Testing and debugging ensures that Google Analytics is reporting data correctly. There are several tools and strategies you can use to test everything is working correctly.

Google Tag Assistant

To check if Google Analytics fires correctly on your pages, you can use the Google Tag Assistant Chrome extension.

In the Real-Time section of Google Analytics, you can keep up with live activity on your website. You can even see how many users are browsing live, what pages they view, and even which geographic region they come from. That helps in campaigns or launches as a way to measure instant performance.

React Developer Tools

React Developer Tools can ensure that your components render as you expect them to, and page view tracking hooks work.

Step 5: Best Practices for Google Analytics in React

  • Avoid Duplicate Page Views: React automatically updates components without triggering a full page reload. To avoid triggering duplicate pageviews in Google Analytics, ensure that you only send page view events when necessary.
  • Use React Helmet for Metadata: Use react-helmet for managing metadata like titles and descriptions, especially if you’re creating a dynamic SPA. This approach is essential in ensuring accurate metadata for pages when implementing how to Integrate google analytics in React JS. Proper metadata allows Google Analytics to receive the correct information while tracking each page effectively.
  • Custom reports set-up: With Google Analytics, you set up custom reports for every metric that is significant in your app. Setting such up will make it better have a glimpse into how good your app performs.

Conclusion: Enhancing User Experience with Google Analytics

By integrating Google Analytics with React JS, you can gain valuable insights into how users interact with your app. Learning how to integrate Google Analytics in React JS ensures you effectively track page views, user events, and real-time activity, which empowers data-driven decisions and app optimization. Whether you’re offering shopify maintenance services, running a b2b web design agency, or providing website maintenance services and wordpress development services, understanding your users through analytics data and factoring in the average cost of website design for small business can significantly enhance decision-making and improve user engagement.

With Google Tag Management consulting services, website redesign services, responsive website development services and Google Analytics consulting services, the integration process could be streamlined to give one maximum utilization of analytics data. From a complex application up to a simple website, continuous improvement and user satisfaction highly depend on proper analytics integration. Partnering with a travel website development company ensures that these integrations are tailored to the specific needs of the travel industry, helping you track key metrics, improve user journeys, and drive meaningful growth.

Sumi Rauf

Sumi Rauf is a seasoned digital marketing expert and the creative mind behind Digitalocus. With years of experience in SEO, analytics, and content strategy, Sumi specializes in helping businesses grow through innovative and data-driven solutions. Passionate about staying ahead of industry trends, Sumi is dedicated to delivering results that matter. When not optimizing digital campaigns, Sumi enjoys sharing insights on the latest developments in digital marketing.

Google Analytics Guide

Comments

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

←Previous: What Is Unassigned Traffic in Google Analytics?
Next: The Complete Guide to Website Redesign: Strategies, Processes, and Best Practices→

latest posts

  • What is the primary purpose of the traffic reporting collection
    What is the primary purpose of the traffic reporting collection​?
  • why content marketing is important for b2b​?
    why content marketing is important for b2b​?
  • How Web Design Impacts Content Marketing​?
    How Web Design Impacts Content Marketing​?
  • How to Add Google Analytics to Google Tag Manager
    How to Add Google Analytics to Google Tag Manager?
  • How Often Should You Audit Your Site for SEO
    How Often Should You Audit Your Site for SEO?

services

  • SEO
  • Website Development
  • Ecommerce Websites
  • Website Maintenance
  • Website Redesign
  • Web Design
  • Google Tag Manager
  • Google Analytics

Industries

  • Travel
  • Hospitality

Technologies

  • Shopify
  • WordPress

guides

  • Google Analytics
  • Google Tag Manager

case studies

  • Maulik Hospitality
  • Kamay Hotels & Resorts
  • BB Club Paid Ads
  • Maulik Hospitality Paid Ads
  • Tags Bnb Paid Ads
  • Turban Hotels Paid Ads
Digital Locus

info@digitalocus.com

+91-9784395621

  • Instagram
  • LinkedIn
  • Facebook
  • Threads
  • YouTube
  • X
  • Behance

USEFUL LINKS

  • Privacy Policy
  • contact
  • Blog

Find us

Digital Locus © 2025. All Rights Reserved