react-ga4 vs react-ga
Google Analytics Libraries for React Comparison
1 Year
react-ga4react-ga
What's Google Analytics Libraries for React?

These libraries facilitate the integration of Google Analytics into React applications, allowing developers to track user interactions and gather analytics data effectively. They provide a streamlined way to send page views, events, and other metrics to Google Analytics, enhancing the ability to analyze user behavior and improve application performance. 'react-ga' is designed for Universal Analytics (UA), while 'react-ga4' is tailored for Google Analytics 4 (GA4), which introduces new features and a different data model focused on event-based tracking.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-ga4380,42229084.4 kB422 years agoMIT
react-ga274,8225,122214 kB144-Apache-2.0
Feature Comparison: react-ga4 vs react-ga

Analytics Model

  • react-ga4:

    Adopts an event-driven model that focuses on user interactions rather than sessions. GA4 allows for more granular tracking of user behavior, enabling developers to define custom events and parameters, making it more adaptable to modern web applications.

  • react-ga:

    Uses the traditional pageview and event tracking model of Universal Analytics, which relies on sessions and users. It is well-suited for applications that require a straightforward implementation of tracking user interactions based on these metrics.

Ease of Integration

  • react-ga4:

    Also offers a simple integration process, but with additional features that require understanding of the new GA4 event model. Developers may need to invest time in learning the new capabilities and how to effectively utilize them for tracking.

  • react-ga:

    Integrates easily with existing React applications by providing a simple API for tracking page views and events. It requires minimal setup and is straightforward for developers familiar with Universal Analytics.

Feature Set

  • react-ga4:

    Provides a richer feature set, including automatic tracking of certain user interactions, cross-platform tracking capabilities, and advanced reporting features, making it more suitable for comprehensive analytics.

  • react-ga:

    Includes basic tracking features such as page views and events, but lacks advanced features introduced in GA4, such as cross-platform tracking and enhanced measurement options.

Future-Proofing

  • react-ga4:

    Designed for the future of analytics, 'react-ga4' will continue to receive updates and support as Google evolves its analytics platform, ensuring that projects using it remain relevant.

  • react-ga:

    As Universal Analytics is being phased out, 'react-ga' may not receive future updates or support, making it less viable for long-term projects.

Community and Support

  • react-ga4:

    Benefits from growing community support and documentation as more developers transition to GA4, ensuring access to resources and best practices.

  • react-ga:

    Has a well-established community and documentation, but may see reduced support as the focus shifts to GA4.

How to Choose: react-ga4 vs react-ga
  • react-ga4:

    Choose 'react-ga4' if you are starting a new project or migrating to Google Analytics 4. This package supports the latest features of GA4, including enhanced measurement capabilities and a more flexible event model, making it ideal for modern analytics needs.

  • react-ga:

    Choose 'react-ga' if you are working with an existing project that uses Universal Analytics and you want to maintain compatibility with UA features and tracking methods. It's suitable for projects that have not yet transitioned to GA4.

README for react-ga4

React Google Analytics 4

Migrate from old react-ga

// Simply replace `react-ga` with `react-ga4` and remove `ReactGA.pageview()`
// import ReactGA from "react-ga";
import ReactGA from "react-ga4";

Install

npm i react-ga4

Usage

import ReactGA from "react-ga4";

ReactGA.initialize("your GA measurement id");

Example

More example can be found in test suite

// Multiple products (previously known as trackers)
ReactGA.initialize([
  {
    trackingId: "your GA measurement id",
    gaOptions: {...}, // optional
    gtagOptions: {...}, // optional
  },
  {
    trackingId: "your second GA measurement id",
  },
]);

// Send pageview with a custom path
ReactGA.send({ hitType: "pageview", page: "/my-path", title: "Custom Title" });

// Send a custom event
ReactGA.event({
  category: "your category",
  action: "your action",
  label: "your label", // optional
  value: 99, // optional, must be a number
  nonInteraction: true, // optional, true/false
  transport: "xhr", // optional, beacon/xhr/image
});

Reference

ReactGA.initialize(GA_MEASUREMENT_ID, options)

| Parameter | Notes | | ------------------- | ----------------------------------------------------------------------------------------------------------------------- | | GA_MEASUREMENT_ID | string Required | | options.nonce | string Optional Used for Content Security Policy (CSP) more | | options.testMode | boolean Default false | | options.gtagUrl | string Default https://www.googletagmanager.com/gtag/js | | options.gaOptions | object Optional Reference | | options.gtagOptions | object Optional |

ReactGA.set(fieldsObject)

| Parameter | Notes | | ------------ | ----------------- | | fieldsObject | object Required |

ReactGA.event(name, params)

This method signature are NOT for UA-XXX

| Parameter | Notes | | --------- | ----------------------------------------------------------------------------------------------------------------------------- | | name | string Required A recommended event or a custom event | | params | object Optional |

ReactGA.event(options)

| Parameter | Notes | | ---------------------- | ----------------------------------- | | options | object Required | | options.action | string Required | | options.category | string Required | | options.label | string Optional | | options.value | number Optional | | options.nonInteraction | boolean Optional | | options.transport | 'beacon'\|'xhr'\|'image' Optional |

ReactGA.send(fieldsObject)

| Parameter | Notes | | ------------ | ----------------- | | fieldsObject | object Required |

ReactGA.gtag(...args)

ReactGA.ga(...args)

Extending

import { ReactGAImplementation } from "react-ga4";

class MyCustomOverriddenClass extends ReactGAImplementation {}

export default new MyCustomOverriddenClass();

Debugging

Use Google Analytics Debugger Chrome Extension to see logs

Maintainer

Han Lin Yap

License

MIT