将GA添加到reactJs应用程序

我正在尝试添加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>,

请指导我做错了什么。

qwer879 回答:将GA添加到reactJs应用程序

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

enter image description here

本文链接:https://www.f2er.com/1266694.html

大家都在问