gpt4 book ai didi

google-analytics - 如何为 React-Router 设置 Google Analytics?

转载 作者:行者123 更新时间:2023-12-03 12:54:32 24 4
gpt4 key购买 nike

我正在尝试在我的 React 网站上设置 Google Analytics,并且遇到了一些软件包,但没有一个软件包具有我在示例中所提供的那种设置。希望有人能对此有所启发。

我正在查看的包是 react-ga .

我的 index.js 上的渲染方法如下所示。

React.render((
<Router history={createBrowserHistory()}>
<Route path="/" component={App}>
<IndexRoute component={Home} onLeave={closeHeader}/>
<Route path="/about" component={About} onLeave={closeHeader}/>
<Route path="/gallery" component={Gallery} onLeave={closeHeader}/>
<Route path="/contact-us" component={Contact} onLeave={closeHeader}>
<Route path="/contact-us/:service" component={Contact} onLeave={closeHeader}/>
</Route>
<Route path="/privacy-policy" component={PrivacyPolicy} onLeave={closeHeader} />
<Route path="/feedback" component={Feedback} onLeave={closeHeader} />
</Route>
<Route path="*" component={NoMatch} onLeave={closeHeader}/>
</Router>), document.getElementById('root'));

最佳答案

保留对历史对象的引用。即

import { createBrowserHistory } from 'history';

var history = createBrowserHistory();

ReactDOM.render((
<Router history={history}>
[...]

然后添加一个监听器来记录每个页面浏览量。 (这假设您已经以通常的方式设置了 window.ga 对象。)

history.listen((location) => {
window.ga('set', 'page', location.pathname + location.search);
window.ga('send', 'pageview');
});

关于google-analytics - 如何为 React-Router 设置 Google Analytics?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34836500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com