MCPcopy Index your code
hub / github.com/codler/react-ga4

github.com/codler/react-ga4 @v3.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.1 ↗ · + Follow
14 symbols 23 edges 6 files 0 documented · 0% 18 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

✨ Looking for sponsors! ✨

Maintainer need help with sponsor!

Han Lin Yap Han Lin Yap 🚧

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

Extension points exported contracts — how you extend this code

GaOptions (Interface)
(no doc)
src/ga4.ts
Window (Interface)
(no doc)
src/gtag.ts
UaEventOptions (Interface)
(no doc)
src/ga4.ts
InitOptions (Interface)
(no doc)
src/ga4.ts

Core symbols most depended-on inside this repo

format
called by 3
src/format.ts
gtag
called by 1
src/ga4.ts
gtag
called by 1
src/gtag.ts
toTitleCase
called by 1
src/format.ts
mightBeEmail
called by 1
src/format.ts
redactEmail
called by 1
src/format.ts
constructor
called by 0
src/ga4.ts

Shape

Function 6
Interface 4
Class 2
Method 2

Languages

TypeScript100%

Modules by API surface

src/ga4.ts7 symbols
src/format.ts4 symbols
src/gtag.ts2 symbols
src/ga4.test.ts1 symbols

For agents

$ claude mcp add react-ga4 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact