DEV Community

Cover image for Google Analytics on React.js Components

Posted on • Updated on

Google Analytics on React.js Components


Struggling to use Google Analytics on a React-app?
Do you only get data on the landing page of your app, instead of every page?

This article will be about how to fix this problem!

The issue:

Google Analytics (GA) sends a pageview per page load, this means that if you would go on a certain page on your website, it requests that page from the server and loads it for the user. That is what GA records and sends as data.

(This is in the case of a simple page view, page-events will not be discussed here!)

And this is also what is the issue when trying to use GA on a React-app (or any single-page applications). React will load the whole app at once and switch components directly on the browser side.

GA doesn't see this as a new page-load as it doesn't actually load a new page, but as a reload of the same page.

A solution:

There have been different kinds of solutions to this problem, but when I went looking for one I found it confusing to find a clear and up-to-date solution. So here I am, sharing my own recommendation!

The idea would be that, as GA recognizes the page reload of the root URL ( but not the URI (That is the part after the root:, the trick would be to make it check that URI everytime it reloads. And send that as the whole location of the page.

This solution requires you to use those dependencies:

  • react-router-dom and react-ga
npm -i react-router-dom react-ga --save
yarn add react-router-dom react-ga --save
Enter fullscreen mode Exit fullscreen mode

Create a file in your source folder and give it a name like useGaTracker.js or something, you can name it however you want though.



  • Go get your UA tracking code on GA.

If you already have created your GA property, you can find that code like this:

  • Go to Admin image
  • Select the property you are using - and click on Property Settings image
  • Copy your Tracking ID (should start with UA-xxxxxxx-x) image


  • Import { useEffect, useState } from react; { useLocation } from react-router-dom and ReactGA from react-ga inside the useGaTracker.js file.
import { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import ReactGA from "react-ga";
Enter fullscreen mode Exit fullscreen mode


  • Let's wright the function:
const useGaTracker = () => {
    const location = useLocation();
    const [initialized, setInitialized] = useState(false);

    useEffect(() => {
        if (!window.location.href.includes("localhost")) {
    }, []);

    useEffect(() => {
        if (initialized) {
        ReactGA.pageview(location.pathname +;
    }, [initialized, location]);

export default useGaTracker;
Enter fullscreen mode Exit fullscreen mode

useLocation() is what we will use to get our routing as location.

The first useEffect() will check if your project is running on a localhost, if not, it will initialize your Tracking ID in the ReactGA and set the state initialized to TRUE.

The Second useEffect() will send the pageview to GA with it's location.

This one will also know to send that information each time initialized or location has changed.


  • Import this function into our routing file.

Go to the file that you use to route your components, and add your function in the router-component (in my case this would be my app.js file:

import { Route, Switch, Redirect } from 'react-router-dom'
import useGaTracker from './useGaTracker'

const App = () => {


  return (
    <div className="App">
          <Route path="/" exact component={ Landing } /> 

          <Route path="/about" exact component={ About } />
          <Route path="/discord" exact component={ Discord } />

          <Route path="/404" component={ Error } />
          <Redirect to="/404" />

export default App;
Enter fullscreen mode Exit fullscreen mode


  • That's about it!

You should have a working connection that will save and send all the different routes you use in your routing file!


Discussion (5)

redboy18x profile image


This statement should be in if block.

useEffect(() => {
if (!window.location.href.includes("localhost")) {
}, []);

javierpineros profile image
Javier Piñeros

Hello Basile, can you indicate the versions of react, react-router-dom and others module used?

What you indicate does not work for me and the following error is generated

26 | );
27 | }
28 |

29 | return useContext (Context) .location;
30 | }
31 |
32 | export function useParams () {


ilariacallegari profile image

not sure it is still useful, but try implementing the hook in every page, rather than only in the routing component. I had the same error, and it worked after doing this.

vighnesh profile image
Vighnesh Kulkarni

Thanks Basile!

ilariacallegari profile image
Ilaria • Edited

In my case, using the useGaTracker hook in the routing component was not working, as the location returned undefined. I had to implement the hook in every page in order for it to work properly.