Adding GA to reactJs application

Viewed 271

I am trying to add GA to record page hits. I have tried every solution to make it work but no progress.

In index.js

import ReactGA from 'react-ga';

const hist = createBrowserHistory();

ReactGA.initialize('Tracking_Id');
hist.listen(location => {
  ReactGA.set({ page: location.pathname }); // Update the user's current page
  ReactGA.pageview(location.pathname +  location.search); // Record a pageview for the given page
console.log(location.pathname)
}); 

My Routes are

  <Provider store={store}>
    <Router history={hist}>
    <Route   path='/thank-you' component={ThankYou} />
    <Route   path='/auth' component={AuthLayout} />
    <Route exact path='/' component={HomeLayout} />
      <Switch>
        <PrivateRoute path='/admin' component={AdminLayout} />
      </Switch>
    </Router>
  </Provider>,

Kindly guide me what i am doing wrong.

1 Answers

Based on your comment it depends on the fact that you are using a Google Analytics 4 Property ID (G-XXXXXXXX), while the React Analytics package in question works for Universal Analytics. I suggest you create a Universal Analytics Property (as shown in following image) and use the relative identifier UA-XXXXXXX-X:

enter image description here

Related