352 react ga pageviews #353

Merged
bones7242 merged 5 commits from 352-react-ga-pageviews into master 2018-02-16 16:58:59 +01:00
Showing only changes of commit ec4669d058 - Show all commits

View file

@ -1,39 +0,0 @@
import React from 'react';
import GoogleAnalytics from 'react-ga';
const config = require('../../config/speechConfig.js');
const googleApiKey = config.analytics.googleId;
GoogleAnalytics.initialize(googleApiKey);
const withAnalytics = (WrappedComponent, options = {}) => {
const trackPage = page => {
GoogleAnalytics.set({
page,
...options,
});
GoogleAnalytics.pageview(page);
};
const HOC = class extends React.Component {
componentDidMount () {
const page = this.props.location.pathname;
// track initial page
trackPage(page);
};
componentWillReceiveProps (nextProps) {
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
// track each new page
if (currentPage !== nextPage) {
trackPage(nextPage);
}
};
render () {
return <WrappedComponent {...this.props} />;
};
};
return HOC;
};
export default withAnalytics;