Integrating Google Analytics 4 with a React Application

Sudhir Kumar
2 min readJun 2, 2024

--

Google Analytics 4 (GA4) offers robust analytics and insights for modern web applications. Integrating GA4 with a React application can help track user interactions and improve user experience. Here’s a comprehensive guide on how to set up GA4 in a React app.

Step 1: Set Up Google Analytics 4 Property

1. Create a GA4 Property

  • Go to Google Analytics.
  • Click on the Admin gear icon in the bottom-left corner.
  • Under the Property column, select Create Property.
  • Follow the prompts to set up a new GA4 property.

2.Obtain the Measurement ID

  • In the Admin area, navigate to Data Streams under the Property column.
  • Select your web data stream.
  • Your Measurement ID (starts with G-) will be displayed at the top right of the data stream details.

Step 2: Install react-ga4

To integrate GA4 with your React application, you’ll need the react-ga4 library.

npm install react-ga4

Step 3: Initialize GA4 in Your React Application

Initialize GA4 in your main entry file (index.js or App.js).

// index.js or App.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import ReactGA from 'react-ga4';

// Initialize GA4 with your Measurement ID
ReactGA.initialize('G-XXXXXXXXXX'); // Replace with your Measurement ID

// Send a pageview for the initial load
ReactGA.send("pageview");

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

Step 4: Track Page Views

Track page views by listening to route changes. This example uses React Router.

// App.js
import React, { useEffect } from 'react';
import { BrowserRouter as Router, Route, Switch, useLocation } from 'react-router-dom';
import ReactGA from 'react-ga4';

const usePageTracking = () => {
const location = useLocation();

useEffect(() => {
ReactGA.send("pageview");
}, [location]);
};

const HomePage = () => <div>Home Page</div>;
const AboutPage = () => <div>About Page</div>;

const App = () => {
usePageTracking();

return (
<Router>
<Switch>
<Route path="/about" component={AboutPage} />
<Route path="/" component={HomePage} />
</Switch>
</Router>
);
};

export default App;

Step 5: Track Custom Events

Track custom events by calling ReactGA.event within your components.

// ExampleComponent.js
import React from 'react';
import ReactGA from 'react-ga4';

const ExampleComponent = () => {
const handleClick = () => {
ReactGA.event({
category: 'User',
action: 'Clicked example button'
});
};

return <button onClick={handleClick}>Click me</button>;
};

export default ExampleComponent;

Conclusion

By following these steps, you’ll successfully integrate Google Analytics 4 into your React application, enabling you to track user interactions and gather valuable insights. For advanced tracking and customization, refer to the react-ga4 documentation and the Google Analytics 4 API documentation.

Happy tracking!

--

--

Sudhir Kumar
Sudhir Kumar

Written by Sudhir Kumar

Email me for frontend and design work. I have delivered lots of projects. Website: https://codeindica.netlify.app/

No responses yet