在react-admin应用上使用GA的最佳方法是什么?
我知道npm模块react-ga,但它只跟踪主页:
import ReactGA from 'react-ga';
ReactGA.initialize('UA-12345');
ReactGA.pageview(window.location.pathname + window.location.search);
是否有一种通用的方法来查看每个资源页面和每个操作上的页面视图?
我能够通过使用自定义历史记录来使它正常工作。
import { createBrowserHistory } from 'history';
import ReactGA from 'react-ga';
// create the history
const history = createBrowserHistory();
// listen for a route change and record page view
history.listen(location => {
ReactGA.pageview(location.pathname);
});
// initialize GA
ReactGA.initialize('UA-000000000-1');
// record the first pageview because listen will not be called
ReactGA.pageview(window.location.pathname);
带有历史记录包的listen
的文档:https://github.com/ReactTraining/history/blob/master/docs/GettingStarted.md#listening
[不要忘记在Admin
组件中列出客户历史记录:
<Admin
history={history}
>
...
</Admin>
请参阅https://marmelab.com/admin-on-rest/Admin.html#history了解更多详细信息