我正在使用 react-ga 从使用谷歌分析跟踪我的反应应用程序。我通过 API 调用获取跟踪 ID,大约 4 秒后返回。我的功能是这样的:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentDidMount() {
ReactGA.initialize(this.props.partnerTrackingCode);
const page = this.props.location.pathname;
trackPage(page);
}
componentWillReceiveProps(nextProps) {
console.log(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 withTracker;
问题是必须首先调用初始化函数并且使用 undefined 调用,因为起初 trackid 是未定义的(请记住,它是异步获取的)。我使用了这个编辑:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentWillReceiveProps(nextProps) {
if(nextProps.trackId) {
ReactGA.initiliaze(nextProps.trackId);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
我正在使用 react-ga 从使用谷歌分析跟踪我的反应应用程序。我通过 API 调用获取跟踪 ID,大约 4 秒后返回。我的功能是这样的:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentDidMount() {
ReactGA.initialize(this.props.partnerTrackingCode);
const page = this.props.location.pathname;
trackPage(page);
}
componentWillReceiveProps(nextProps) {
console.log(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 withTracker;
问题是必须首先调用初始化函数并且使用 undefined 调用,因为起初 trackid 是未定义的(请记住,它是异步获取的)。我使用了这个编辑:
const withTracker = (WrappedComponent, options = {}) => {
const trackPage = (page) => {
ReactGA.set({
page,
options
});
ReactGA.pageview(page);
};
class HOC extends Component {
componentWillReceiveProps(nextProps) {
if(nextProps.trackId) {
ReactGA.initiliaze(nextProps.trackId);
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
trackPage(nextPage);
}
}
render() {
return <WrappedComponent {...this.props} />;
}
}
return HOC;
};
但我收到一个错误,无法读取未定义的属性 parentNode你有什么想法吗?