19

I am using google analytics in my react project and it doesn't show any active users even when I am online. I have tried different approaches that I found online but none seem to work. I have only tried it on localhost and not on a deployed website but I think it should still work.

Here is my code.

My app.js

import React, { Suspense, useEffect } from "react";
import "./App.css";
import IntroPage from "./containers/IntroPage/IntroPage";
import Layout from "./containers/Layout/Layout";
import { Switch, Route, Redirect, withRouter } from "react-router-dom";
import Spinner from "./Components/UI/Spinner/Spinner";
import ReactGA from "react-ga";

const Contact = React.lazy(() => import("./Components/Contact/Contact"));

const trackingId = "UA-171033255-1";
ReactGA.initialize(trackingId);


function App() {
  useEffect(() => {
    ReactGA.pageview(window.location.pathname + window.location.search);
  }, []);

  return (
    <div className="App">
      <Layout>
        <Switch>
          <Route
            path="/contact"
            render={() => (
              <Suspense
                fallback={
                  <div className="Fallback">
                    <Spinner />
                  </div>
                }
              >
                <Contact />
              </Suspense>
            )}
          />
          <Route path="/" component={IntroPage} />
          <Redirect to="/" />
        </Switch>
      </Layout>
    </div>
  );
}

export default withRouter(App);

What am I doing wrong here?

5 Answers 5

12

Updating from react-ga to react-ga4 worked for me.

Change the import to react-ga4 and instead of ReactGA.pageview(...) use ReactGA.send("pageview").

0
2

Since you are using empty dependencies array in useEffect your code gets executed only once, when <App/> rendered. So you are not sending pageviews on route change.

You need to put pageview-related code before your App component, right after ReactGA initialization. It's a usage example from docs.

1

Have you tried adding the tracking code inside <head> in index.html (Admin -> Property -> Tracking code)?

2
  • I don't think that is needed in react.
    – excurbia
    Commented Sep 3, 2020 at 10:51
  • Not sure what you mean here -- it seems like the easiest way to associate a GA property with your application. It might be required, too--the react entrypoint is in the <body>, and you need to get the tracking snippet into the <head>... Commented Sep 8, 2020 at 10:13
1

I had a similar issue try disabling your ad block if you have it active and put the ReactGA.initialize inside the useEffect

0

import ReactGA from "react-ga4";

ReactGA.initialize("your measurement id"); ReactGA.send("pageview");

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 14, 2022 at 21:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.