react-ga4

React Google Analytics 4

react-ga4 downloads react-ga4 version react-ga4 license

react-ga4Similar Packages:
Npm Package Weekly Downloads Trend
3 Years
🌟 Show real-time usage chart on react-ga4's README.md, just copy the code below.
## Usage Trend
[![Usage Trend of react-ga4](https://npm-compare.com/img/npm-trend/THREE_YEARS/react-ga4.png)](https://npm-compare.com/react-ga4#timeRange=THREE_YEARS)
Cumulative GitHub Star Trend
🌟 Show GitHub stars trend chart on react-ga4's README.md, just copy the code below.
## GitHub Stars Trend
[![GitHub Stars Trend of react-ga4](https://npm-compare.com/img/github-trend/react-ga4.png)](https://npm-compare.com/react-ga4)
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-ga4484,04031784.4 kB443 years agoMIT
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)

ParameterNotes
GA_MEASUREMENT_IDstring Required
options.noncestring Optional Used for Content Security Policy (CSP) more
options.testModeboolean Default false
options.gtagUrlstring Default https://www.googletagmanager.com/gtag/js
options.gaOptionsobject Optional Reference
options.gtagOptionsobject Optional

ReactGA.set(fieldsObject)

ParameterNotes
fieldsObjectobject Required

ReactGA.event(name, params)

This method signature are NOT for UA-XXX

ParameterNotes
namestring Required A recommended event or a custom event
paramsobject Optional

ReactGA.event(options)

ParameterNotes
optionsobject Required
options.actionstring Required
options.categorystring Required
options.labelstring Optional
options.valuenumber Optional
options.nonInteractionboolean Optional
options.transport'beacon'|'xhr'|'image' Optional

ReactGA.send(fieldsObject)

ParameterNotes
fieldsObjectobject 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