Reactjs 如何在HashRouter中使用react ga?
我一直在尝试将Google Analytics与我的React应用程序集成,该应用程序使用React router dom v4中的HashRouter。但抱怨根本不起作用: 警告:Reactjs 如何在HashRouter中使用react ga?,reactjs,google-analytics,react-router,Reactjs,Google Analytics,React Router,我一直在尝试将Google Analytics与我的React应用程序集成,该应用程序使用React router dom v4中的HashRouter。但抱怨根本不起作用: 警告:忽略历史记录属性。使用自定义 历史记录,使用import{Router}代替import{HashRouter作为 路由器} Github上也发布了同样的问题:但还没有提供解决方案 我必须在不使用任何HOC的情况下使用HashRouter进行项目。这可能吗 我目前的执行情况: ReactGA.initialize('
import{Router}
代替import{HashRouter作为
路由器}
Github上也发布了同样的问题:但还没有提供解决方案
我必须在不使用任何HOC的情况下使用HashRouter进行项目。这可能吗
我目前的执行情况:
ReactGA.initialize('UA-XXXXXXXX-X');
const history = createHistory()
history.listen((location, action) => {
ReactGA.set({ page: location.pathname });
ReactGA.pageview(location.pathname);
});
ReactDOM.render((
<HashRouter history={history}>
<Switch>
<Route path="/" name="App" component={App} />
</Switch>
</HashRouter>
),document.getElementById('root'));
ReactGA.initialize('UA-XXXXXXXX-X');
const history=createHistory()
历史。聆听((位置、动作)=>{
set({page:location.pathname});
ReactGA.pageview(location.pathname);
});
ReactDOM.render((
),document.getElementById('root');
演示项目中提供的示例代码也使用HashRouter。
如图所示;
不一定要使用HOC,withTracker。只需在每页上轻松插入跟踪触发器即可。如果在自己的页面组件中实现相同的代码,则可以获得相同的结果
然后,您需要将下面的代码添加到您想要手动跟踪的每个组件中
const trackPage = (page) => {
ReactGA.set({
page
});
ReactGA.pageview(page);
};
componentDidMount() {
const page = this.props.location.pathname;
trackPage(page);
}
componentWillReceiveProps(nextProps) {
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
if (currentPage !== nextPage) {
trackPage(nextPage);
}
}
你找到解决办法了吗?没有,那时候我找不到解决办法。刚刚从静态页面直接初始化了google analytics,用于跟踪访问,并使用了一个web应用程序内消息平台来跟踪页面视图数据。只是一个黑客,但我解决了它。我使用了与ReactGA演示应用程序相同的实现。但是您必须使用那里提供的withTracker HOC。如果您愿意,可以添加这些代码行作为此问题的答案,以便我将其标记为已接受吗?好主意,现在添加:)