作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的 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/
我是一名优秀的程序员,十分优秀!