• Stars
    star
    265
  • Rank 154,577 (Top 4 %)
  • Language
    JavaScript
  • Created almost 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Google Analytics 4

✨ 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

More Repositories

1

Battery-Time-Remaining

Show battery time remaining in Mac OS X 10.8+ Mountain Lion and Mavericks
Objective-C
412
star
2

jQuery-Css3-Finalize

Tired of adding prefix for each browser when it comes to the new css3 attributes?
JavaScript
166
star
3

Bitcoin-phpMiner

phpMiner for bitcoin
PHP
121
star
4

Grid-Layout-Polyfill

JavaScript
105
star
5

jQuery-Ajax-Upload

Uses native XHR to upload files
PHP
95
star
6

Laravel-Facebook-SDK

Laravel Bundle - Facebook SDK
PHP
36
star
7

Smush.it-API

Smush.it API
PHP
10
star
8

Ractive-TypeScript-Definition

ractive.d.ts
TypeScript
9
star
9

sweden-coronavirus

Sverige Coronavirus (COVID-19) datakälla
TypeScript
7
star
10

Laravel-Extended

Extended Library of Laravel
PHP
6
star
11

Math

PHP
5
star
12

BombCarrier

A bomberman game using WebGL, Works best in Chrome
JavaScript
5
star
13

Media-Stream-Center

Stream any media from any OS to any unit
PHP
5
star
14

Rebellious-Tower-Defence

2d tower defence game in C++. A project in 2009
C++
5
star
15

snippets

All kind of snippets
PHP
5
star
16

Laravel-Query-Viewer

Laravel Bundle - Query Viewer
PHP
4
star
17

Text-To-Speech

4
star
18

Python-WebSocket

Python websocket - asynchronous socket - draft-ietf-hybi-thewebsocketprotocol-00
Python
4
star
19

avanza-api

Avanza API
TypeScript
4
star
20

Startup

Gathering all the good parts from all my projects and making it to a mini framework
PHP
4
star
21

Ractive-UI

JavaScript
3
star
22

Portfolio-in-Python

A project in course TDP003. Made in 2009.
Python
2
star
23

Image-Dock

Takes all images on your site and creates a navigation
JavaScript
2
star
24

jQuery-Parallel-Coordinates

JavaScript
2
star
25

bullhorn-api

TypeScript
2
star
26

CSS-Analyzer

Tool, utility, parser for CSS in JavaScript
JavaScript
2
star
27

Single-Sign-On

OpenId + Facebook
PHP
2
star
28

RuneCrafter

Multiplayer 3d game made in XNA 3.0. A project in 2009.
C#
1
star
29

bitmex-data-downloader

Download Bitmex price data
TypeScript
1
star
30

Polyfill

JavaScript
1
star
31

KaW-Guide

1
star
32

API

PHP
1
star
33

yap.nu

HTML
1
star
34

electron-boilerplate

TypeScript
1
star
35

pGChart

php class making charts via google chart url
PHP
1
star
36

Bitcoin-Wallet-RPC

Bitcoin-Wallet-RPC on Windows
C#
1
star
37

Build-your-site

A site where you can build a site without needing to code.
CSS
1
star
38

WINAML

WINAML Is Not A Markup Language, is a language created by Johan Alm and Han Lin Yap. A project in 2010.
Ruby
1
star
39

Ingrediens.se

Sök efter ingredienser
PHP
1
star
40

pQuery

Makes PHP more powerful, inspired from Jquery, Python and Ruby!
PHP
1
star
41

hardhat-faucet

Hardhat plugin for faucet
TypeScript
1
star