Created
September 6, 2017 20:27
-
-
Save knowbody/a1385b4daee108d1645b898314cf63a8 to your computer and use it in GitHub Desktop.
React Router with Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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