Adding Google Analytics to React Application

A quick way to add Google Analytics to React.

August 05, 2022 Code

The steps are pretty simple:

  1. Install
  2. react-ga
  3. package, which is a wrapper for Google Analytics script.
# Yarnyarn add react-ga
  1. Import and Add
  2. react-ga
  3. code in
  4. App.js
  5. .
import ReactGoogleAnalytics from 'react-ga'...ReactGoogleAnalytics.initialize('UA-XXXXXXXXX-X'); // Google Analytics IDReactGoogleAnalytics.pageview('/');

Invely's