2

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.

4
  • what's the mistake? Commented Nov 5, 2020 at 19:01
  • i debug it and its working fine but i am not receiving data on google analytics Commented Nov 5, 2020 at 21:26
  • The format of your tracking id is like this 'G-XXXXXX' or this 'UA-XXXX-XX'? Commented Nov 5, 2020 at 21:35
  • G-XXXXXX is the format Commented Nov 5, 2020 at 22:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

let me try this
Yes, let me know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.