Google analytics with Reach router in 5 steps
Tags: react, googleanalytics, reachrouter, javascript
Published: 2020-08-16 09:20:03

Analytics is very important to track the performance of your web app. In a single page application, you would like to see the changing URL in Google Analytics Dashboard but as the page path changes, it doesn’t loads and sends the data automatically. Hence Just including the Google Analytics script tag won’t work. In this guide, I would take you through each step that you need in order to set up Google analytics with reach router that sends real-time data on each page change.
> [Reach router](https://reach.tech/router) is a small, simple router for React that borrows from React Router, Ember, and Preact Router. Reach Router has a small footprint, supports only simple route patterns by design, and has strong (but experimental) accessibility features.

In this guide, we will use React-ga that handles the Google Analytics tag and sends that data whenever there is a URL change by listening to the route changes.

### Step 1: Setup Google Analytics Property

We will first create a G[oogle Analytics](https://analytics.google.com/analytics/web/) property and obtain the tracking ID

![](https://cdn-images-1.medium.com/max/2866/1*MMMJfCF1_neXHY3xH98rkA.png)

### Step 2: Install React GA
> React GA is a JavaScript module that can be used to include Google Analytics tracking code in a website or app that uses [React](https://facebook.github.io/react/) for its front-end codebase.

**Installation using** [npm](https://www.npmjs.com/):

    npm install react-ga --save

### Step 3: Import required modules

Now we would need to import the following modules from the Reach Router and the react-ga library in our App.js

    import { Router,createHistory,LocationProvider }from "@reach/router";
    import ReactGA from "react-ga";

### Step 4: Setup tracking Code

Initialize Google Analytics with the obtained tracking ID in the first step in the global scope just after the export in App.js . Also, declare const history globally that enables you to record the browser history. We will use the constant later.

    ReactGA.*initialize*("UA-103xxxxx-xx");
    *const *history= *createHistory*(window);

### Step 5: Handle the Routes

![](https://cdn-images-1.medium.com/max/2000/1*0iqfOci7_7Q8s2-wW1t3PQ.png)

Now declare *<LocationProvider history={history}> *in return statement of your const App *ifi* function in App.js so that all components are inside it. This will help us listen to the URL’s as the route changes.

### Step 6: Send the Tracking Data

Now on every URL change using history.listen, we will trigger and send the data using ReactGA.pageview. For this, we will setup a function that listen to the history object and changes the value of ReactGA.pageview object inside the const APP ifi function in our app.js

    history.listen( window => {
      ReactGA.*pageview*(window.location.pathname+ window.location.search);
      console.log('page=>',window.location.pathname);
    });

Here window.location.pathname is the URL and window.location.search are the URL Params.

Your dinal setup in App.js should look something like this

![](https://cdn-images-1.medium.com/max/2000/1*4FMrklT3bxfXCNbG4opjKA.png)

Run your code and you should be able to see the changes live in google analytics dashboard as you change the path.

### Bonus Step: Sending Aditional Info

![](https://cdn-images-1.medium.com/max/2000/1*rStTIVfK4EEjNnT8fXpS8g.png)

You can also send additional information using React.GA such as user ID, demographic, preference and other additional information and even on certain triggers using the same approach.
[**react-ga/react-ga**
*React Google Analytics Module. Contribute to react-ga/react-ga development by creating an account on GitHub.*github.com](https://github.com/react-ga/react-ga)