Skip to content
Permalink
Branch: master
Find file Copy path
2 contributors

Users who have contributed to this file

@SimeonC @IllegalCactus
39 lines (31 sloc) 855 Bytes
/**
* From ReactGA Community Wiki Page https://github.com/react-ga/react-ga/wiki/React-Router-v4-withTracker
*/
import React, { Component } from 'react';
import ReactGA from '../../src';
export default function withTracker(WrappedComponent, options = {}) {
const trackPage = (page) => {
ReactGA.set({
page,
...options
});
ReactGA.pageview(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;
}
You can’t perform that action at this time.