1.0.0 • Published 2 years ago

google-analytics-ga4 v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

This is very first test Run Will be completing it within 1 month.

React Google Analytics 4

Migrate from old react-ga

import GA4 from "google-analytics-ga4";

Install

Usage

import GA4 from "google-analytics-ga4";

GA4.initialize("your GA measurement id");
GA4.send("pageview");

Example

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

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

// Send a custom event
GA4.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

GA4.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.gaOptionsobject Optional Reference
options.gtagOptionsobject Optional
options.legacyDimensionMetricboolean Default true

GA4.set(fieldsObject)

ParameterNotes
fieldsObjectobject Required

GA4.event(name, params)

This method signature are NOT for UA-XXX

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

GA4.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
options.dimension{1...200}any Optional
options.metric{1...200}any Optional

GA4.send(fieldsObject)

ParameterNotes
fieldsObjectobject Required

ReactGA.gtag(...args)

ReactGA.ga(...args)

GA4.pageview(path, _, title)

Deprecated Use .send("pageview") instead

GA4.outboundLink({ label }, hitCallback)

Deprecated Use enhanced measurement feature in Google Analytics.

Extending

import { ReactGAImplementation } from "google-analytics-ga4";

class MyCustomOverriddenClass extends ReactGAImplementation {}

export default new MyCustomOverriddenClass();

Debugging

Use Google Analytics Debugger Chrome Extension to see logs

Maintainer

pcprashant99

License

MIT