我正在尝试集成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)} />发布于 2020-01-17 13:51:28
您可以使用react hotjar
https://www.npmjs.com/package/react-hotjar
您可以在组件中的任意位置注入hotjar
请务必在组件挂载后使用
componentDidMount() {
hotjar.initialize('xxxxxxx', x);
}如果您使用的是钩子,那么您可以在没有任何依赖数组的useEffect中执行此操作
const SampleComponent = () => {
useEffect(() => {
hotjar.initialize('xxxxxxx', x);
}, [])确保在所有异步填充完成后加载hotjar,这样它就会记录您想要的所有活动
async componentDidMount() {
await load1();
await load2();
hotjar.initialize('xxxxxxx', x);
}发布于 2018-07-22 15:53:01
默认情况下,Hotjar在SPA上工作。默认情况下,如果是url的变化,而不是hashchange,它们会跟踪url中的变化。
如果您希望Hotjar跟踪hashchange,那么您需要在设置中进行配置。
在站点和组织中,您可以访问站点设置。在这里,您可以指定如果默认设置不起作用,Hotjar应该如何跟踪URL更改。下面是对所有选项的解释。

https://stackoverflow.com/questions/46484184
复制相似问题