将 GA 添加到 reactJs 应用程序

Adding GA to reactJs application

我正在尝试添加 GA 来记录页面点击率。我已经尝试了所有的解决方案,但没有任何进展。

在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)
}); 

我的路线是

  <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>,

请指导我做错了什么。

根据您的评论,这取决于您使用的是 Google Analytics 4 属性 ID (G-XXXXXXXX),而有问题的 React Analytics 包适用于 Universal分析。我建议您创建一个 Universal Analytics 属性(如下图所示)并使用相对标识符 UA-XXXXXXX-X: