【发布时间】:2017-09-29 07:49:48
【问题描述】:
我正在尝试集成 Hotjar 库来跟踪我们 React SPA 中用户的热图。
我已按照一般教程进行操作,但无法正常工作。
我在文档的元素中有标准的 Hotjar 跟踪代码。
我正在使用高阶组件连接到 React 路由器并尝试调用 window.hj('stateChange', page) 和 window.hj('vpv', page) 但这些都没有登录Hotjar 热图。
这是 HOC 的代码:
import React, { Component } from 'react';
import GoogleAnalytics from 'react-ga';
GoogleAnalytics.initialize(process.env.REACT_APP_GA_TRACKING_ID);
const withPageViewTracker = (WrappedComponent, options = {}) => {
const trackPage = page => {
console.log('window.hj', window.hj);
console.log('page', page);
// window.hj('stateChange', page);
window.hj('vpv', page);
};
const HOC = class extends Component {
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);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
};
return HOC;
};
export default withPageViewTracker;
这是我使用 HOC 的方式:
<Route exact path="/" component={withPageViewTracker(Welcome)} />
【问题讨论】:
-
你试过什么?你能分享一些代码吗?
-
我已经编辑了我的帖子
-
你使用的是哪个版本的 React Router?
-
@davegeo 你找到这个了吗?
标签: reactjs react-router single-page-application heatmap hotjar