Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created September 6, 2017 20:27
Show Gist options
  • Save knowbody/a1385b4daee108d1645b898314cf63a8 to your computer and use it in GitHub Desktop.
Save knowbody/a1385b4daee108d1645b898314cf63a8 to your computer and use it in GitHub Desktop.
React Router with Google Analytics
import React from 'react';
import GoogleAnalytics from 'react-ga';
if (process.env.REACT_APP_ENVIRONMENT === 'production') {
GoogleAnalytics.initialize('UA-91111111-1');
}
const withGA = WrappedComponent => {
const trackPage = page => {
GoogleAnalytics.set({ page });
GoogleAnalytics.pageview(page);
};
const HOC = props => {
const page = props.location.pathname;
trackPage(page);
return <WrappedComponent {...props} />;
};
return HOC;
};
export default withGA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment