How To Add Google Analytics in React?
Last Updated :
15 Apr, 2025
Integrating Google Analytics into a React application enables you to monitor and analyze your website’s traffic and user behavior. This integration offers valuable insights into user interactions on your site, aiding in making informed decisions to enhance user experience and achieve business objectives. This guide walks you through the steps of adding Google Analytics to a React application.
Prerequisites:
Steps to Setup React Application
Step 1: Create a React application by using this command
npx create-react-app <project-name>
Step 2: Navigate to the project directory
cd <project-name>
Project Structure:
Project structureStep 3: Create a repository in github, initialize git in project and push the code in that repository. From vercel account, deploy this react application, so next we can implement Google analytics in this React application.
Step 4: Now Open your Google Analytics console and create a account for adding analyics to account. You should have an account. If not, create one.
analytics accound and propertyDuring the account creation process, you will also need to create a property. In Google Analytics, a property represents a set of data from a website, app, or device that you want to track separately from other sites or apps. Properties allow you to gain insights into user engagement, enhance website performance, and increase traffic.
Step 5: Now, we choose web as next react web application.
collect dataStep 6: Next, we have to provide our react application URL, and a stream name.
web stream
Step 7: After property creation, below interface appear with some necessary details. Keep the tab open of Google analytics and come back to our project in Code editor.
stream detailsStep 8: Open index.html in public folder of your React application, add google tags script and update "app.js" in "src" folder according to our react application code.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-9FXXSKK3VD">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-9FXXSKK3VD');
</script>
</html>
JavaScript
// src/app.js
import './App.css';
function App() {
return (
<div className="App">
Hello, this is my react application With Google analytics
</div>
);
}
export default App;
Step 9: Now save and push the code again for deployment, vercel deploy your react application again. Now, when we visit the website through URL, we can clear see the real time update of active user.
How To Add Google Analytics in React
Similar Reads
How to add Google Analytics in New Google Sites?
Adding Google Analytics to your New Google Sites is essential for understanding and improving your websiteâs performance. This tool gives you detailed information about how visitors use your site, where they come from, and how engaged they are. With over 50 million websites using it, Google Analytic
4 min read
How to add Analog Clock in ReactJS ?
In this article, we are going to learn how we can add Analog Clock in ReactJs. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to create Analog Cl
2 min read
How to Add Your Website to Google Analytics ?
Google provides some wonderful services to webmasters which definitely helps in managing a web application or a static website. It provides Google AdWords Keyword Planner, Google Search Console, PageSpeedTest by Google, Google AdSense, and also Google Analytics is one of those. After acquiring the U
3 min read
How To Add Google Analytics To GitHub Pages
If you're hosting a website on GitHub Pages and want to track how many people visit your site and what they do there, Google Analytics is the perfect tool. It provides valuable insights that can help you understand your audience and improve your site. In this easy-to-follow guide, we'll show you how
4 min read
How to Use Google Fonts in React?
Adding the Google fonts in React app enhances the typography. It provides a wide range of font styles that are easy to integrate and use in the react components. Google Fonts is a widely used popular library of open-source web fonts that provides a vast collection of typefaces to enhance the visual
5 min read
How to Add Google Analytics to a Next.js Application?
Adding Google Analytics to a Next.js application allows you to track and analyze your website's traffic and user actions. This can provide valuable insights into how users interact with your site, helping you make informed decisions to improve user experience and drive business goals. This article h
3 min read
How to Install Google Analytics in WordPress ?
Google Analytics is a powerful tool that helps you understand your website's traffic and user behaviour. By installing Google Analytics on your WordPress site, you can gain valuable insights to improve your content, enhance user experience, and boost your site's performance. This guide will walk you
3 min read
How to add code input in React JS?
In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our
2 min read
How to create an event in React ?
In the development of contemporary webpages, user interactions play a pivotal role. These interactions include mouse clicks, keypresses, or even uncommon events like connecting a battery to a charger. As developers, our task is to actively "listen" to these events and subsequently ensure that our ap
2 min read
How to Add Auto-Complete Search Box in ReactJS?
An autocomplete search box is a user interface feature that offers suggestions as users type their queries. It enhances the user experience by providing real-time suggestions and help users find what they're looking for more quickly. In this article, we will explore how to implement an autocomplete
5 min read