Adding Google Analytics to React Application

A simple way to add google analytics code to React App.

Code |

September 29, 2019

The steps are pretty simple:

  1. Install react-ga package, which is a wrapper for Google Analytics script.
# Yarn 
yarn add react-ga
  1. Import and Add react-ga code in App.js.
import ReactGoogleAnalytics from 'react-ga'

...

ReactGoogleAnalytics.initialize('UA-XXXXXXXXX-X'); // Google Analytics ID
ReactGoogleAnalytics.pageview('/');