Skip to content

Instantly share code, notes, and snippets.

@Ranatchai
Created March 10, 2019 08:01
Show Gist options
  • Save Ranatchai/9116e99109756defb3f71cfe51304d12 to your computer and use it in GitHub Desktop.
Save Ranatchai/9116e99109756defb3f71cfe51304d12 to your computer and use it in GitHub Desktop.
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