我正在使用react-ga通过Google Analytics(分析)跟踪我的React应用。我正在通过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;
问题在于,必须首先调用initiliaze函数,并使用undefined对其进行调用,因为起初,trackid是undefined(请记住以异步方式获取)。我使用了此编辑:
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通过Google Analytics(分析)跟踪我的React应用。我正在通过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;
问题在于,必须首先调用initiliaze函数,并使用undefined对其进行调用,因为起初,trackid是undefined(请记住以异步方式获取)。我使用了此编辑:
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 。您有什么想法吗?
答案 0 :(得分:0)
您是否在拨打GoogleAnalytics.set({ username });
之前设置了这样的用户名GoogleAnalytics.initialize
?如果没有尝试这样做,看看是否有所作为。我刚才遇到了同样的问题,这对我有用。