React Google Analytics 4 - forked
Migrate from old react-ga
import ReactGA from "react-ga4-forked";
Install
npm i react-ga4-forked
Usage
import ReactGA from "react-ga4-forked";
ReactGA.initialize("your GA measurement id");
Example
More example can be found in test suite
ReactGA.initialize([
{
trackingId: "your GA measurement id",
gaOptions: {...},
gtagOptions: {...},
},
{
trackingId: "your second GA measurement id",
},
]);
ReactGA.send({ hitType: "pageview", page: "/my-path", title: "Custom Title" });
ReactGA.event({
category: "your category",
action: "your action",
label: "your label",
value: 99,
nonInteraction: true,
transport: "xhr",
});
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.gaOptions | object Optional Reference |
options.gtagOptions | object Optional |
options.scriptSrc | string Default https://www.googletagmanager.com/gtag/js |
options.scriptLoadingMode | 'async'|'defer'\ Default async |
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-forked";
class MyCustomOverriddenClass extends ReactGAImplementation {}
export default new MyCustomOverriddenClass();
Debugging
Use Google Analytics Debugger Chrome Extension to see logs
Maintainer
Han Lin Yap
License
MIT