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.
npm install react-gaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `react-ga` with a Universal Analytics tracking ID and send a pageview and a custom event. It includes a warning for users attempting to use GA4 IDs.
Migrate your analytics implementation to Google Analytics 4 (GA4). `react-ga` does not support GA4's `G-` measurement IDs. Consider using `react-ga4` (a separate library) or integrating the native `gtag.js` directly for GA4 tracking.
When initializing, include `siteSpeedSampleRate: 100` in the `gaOptions` object to send 100% of page timing hits. Example: `ReactGA.initialize('UA-XXXXXXXXX-X', { gaOptions: { siteSpeedSampleRate: 100 } });`Ensure `debug: true` is only enabled in non-production environments. A common pattern is `ReactGA.initialize('UA-XXXXXXXXX-X', { debug: process.env.NODE_ENV === 'development' });`Call `ReactGA.initialize` early in your application's lifecycle, typically within a `useEffect` hook in your root component or in your application's entry file, ensuring it runs only once.
Provide 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.
1. 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.