React Google Analytics Module (Universal Analytics)
react-ga is a JavaScript module designed for integrating Google Universal Analytics (UA-xxxxxx-x properties) tracking into React applications. It provides a React-friendly wrapper around the core Google Analytics library, offering an opinionated API to standardize instrumentation. The current stable version is 3.3.1, released in June 2022. However, this library is now considered deprecated because Google Universal Analytics ceased processing new hits on July 1, 2023, in favor of Google Analytics 4 (GA4). react-ga does not support GA4's `G-` measurement IDs, making it unsuitable for new analytics implementations or existing ones that have migrated to GA4. For GA4 integration, users should consider `react-ga4` or direct `gtag.js` implementations.
Common errors
-
Error: `gaTrackingID` is required to initialize Google Analytics
cause The `ReactGA.initialize` method was called without a Universal Analytics (UA-XXXXXX-X) tracking ID, or with an invalid format like a GA4 (G-XXXXXX) measurement ID.fixProvide a valid Universal Analytics tracking ID (e.g., 'UA-12345-1') to `ReactGA.initialize`. If you intend to use GA4, you must switch to a GA4-compatible library like `react-ga4` as `react-ga` does not support GA4 measurement IDs. -
No data appearing in Google Analytics Realtime/Standard Reports
cause This usually indicates incorrect initialization, an invalid tracking ID, an active ad-blocker, or that you are using a GA4 `G-` measurement ID instead of a Universal Analytics `UA-` tracking ID. Additionally, the `siteSpeedSampleRate` default of 1% can make it seem like no data is being collected.fix1. Verify `ReactGA.initialize` is called once with a correct `UA-` tracking ID. 2. Disable ad-blockers for testing. 3. Add `debug: true` to `initialize` options to see console logs. 4. If using GA4, migrate to `react-ga4`. 5. Set `siteSpeedSampleRate: 100` in `gaOptions` for more comprehensive data collection.
Warnings
- breaking Google Universal Analytics (UA) has been officially deprecated and stopped processing new hits on July 1, 2023. This means `react-ga`, which is built exclusively for UA properties, no longer collects data for standard UA properties.
- gotcha The default `siteSpeedSampleRate` for Universal Analytics is 1%, meaning only a small fraction of page timing hits are sent. This can lead to insufficient data for performance analysis.
- gotcha Using the `debug: true` option in `ReactGA.initialize` will log analytics calls to the console, which is useful during development but should be avoided in production environments to prevent unnecessary console output and potential performance overhead.
- gotcha The `ReactGA.initialize` function must be called exactly once before any other tracking functions are invoked. Calling it multiple times or after other tracking calls can lead to inconsistent or lost data.
Install
-
npm install react-ga -
yarn add react-ga -
pnpm add react-ga
Imports
- ReactGA
const ReactGA = require('react-ga');import ReactGA from 'react-ga';
- ReactGA
import ReactGA from 'react-ga/core';
- OutboundLink
import OutboundLink from 'react-ga';
import { OutboundLink } from 'react-ga';
Quickstart
import ReactGA from 'react-ga';
import React, { useEffect } from 'react';
const GA_TRACKING_ID = process.env.REACT_APP_GA_UA_ID ?? 'UA-000000-01'; // Use a Universal Analytics ID
function App() {
useEffect(() => {
if (GA_TRACKING_ID.startsWith('UA-')) {
ReactGA.initialize(GA_TRACKING_ID, { debug: process.env.NODE_ENV === 'development' });
ReactGA.pageview(window.location.pathname + window.location.search);
console.log('ReactGA initialized and pageview sent for UA ID:', GA_TRACKING_ID);
} else {
console.warn('GA_TRACKING_ID is not a Universal Analytics ID (UA-). react-ga only supports Universal Analytics. Please use react-ga4 for GA4 (G-) IDs.');
}
}, []);
const handleButtonClick = () => {
if (GA_TRACKING_ID.startsWith('UA-')) {
ReactGA.event({
category: 'User',
action: 'Clicked Button',
label: 'Demo Button'
});
console.log('GA Event sent: Clicked Demo Button');
}
};
return (
<div>
<h1>Welcome to React-GA Demo</h1>
<p>This demo uses react-ga for Universal Analytics tracking.</p>
<button onClick={handleButtonClick}>Track Me</button>
<p>Check your Universal Analytics dashboard for data.</p>
<p>Note: Universal Analytics is deprecated. For GA4, use react-ga4.</p>
</div>
);
}
export default App;