Created
March 10, 2019 08:01
-
-
Save Ranatchai/9116e99109756defb3f71cfe51304d12 to your computer and use it in GitHub Desktop.
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 { withRouter } from 'react-router-dom' | |
import { track } from './tracker' | |
import { getQueryParams } from './utils/params' | |
import { EVENT_PAGE_VIEW } from './utils/event' | |
import ReactGA from 'react-ga' | |
export const TrackPageView = withRouter( | |
class TrackPageView extends React.Component { | |
componentDidMount() { | |
ReactGA.initialize(process.env.GA_TRACKING_ID) | |
this.trackPageview() | |
} | |
componentDidUpdate(nextProps) { | |
if (nextProps.location !== this.props.location) { | |
this.trackPageview() | |
} | |
} | |
async trackPageview() { | |
ReactGA.pageview( | |
this.props.location.pathname + this.props.location.search | |
) | |
track(EVENT_PAGE_VIEW + ':' + this.props.location.pathname, { | |
...getQueryParams() | |
}) | |
} | |
render() { | |
return null | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment