2019-04-15 15:44:02 +02:00
|
|
|
import React, { useEffect } from 'react'
|
|
|
|
import ReactGA, { FieldsObject } from 'react-ga'
|
|
|
|
import { RouteComponentProps } from 'react-router-dom'
|
2019-04-15 21:08:49 +02:00
|
|
|
import { analyticsId } from '../config'
|
2019-04-15 15:44:02 +02:00
|
|
|
|
2019-05-16 11:39:25 +02:00
|
|
|
ReactGA.initialize(analyticsId, {
|
|
|
|
testMode: process.env.NODE_ENV === 'test',
|
|
|
|
debug: false
|
|
|
|
})
|
|
|
|
|
2019-04-15 15:44:02 +02:00
|
|
|
const withTracker = <P extends RouteComponentProps>(
|
|
|
|
WrappedComponent: any,
|
|
|
|
options: FieldsObject = {}
|
|
|
|
) => {
|
|
|
|
const trackPage = (page: string) => {
|
|
|
|
ReactGA.set({ page, ...options })
|
|
|
|
ReactGA.pageview(page)
|
|
|
|
}
|
|
|
|
|
|
|
|
const HOC = (props: P) => {
|
2019-07-11 23:01:58 +02:00
|
|
|
useEffect(() => {
|
|
|
|
trackPage(props.location.pathname)
|
|
|
|
}, [props.location.pathname])
|
2019-04-15 15:44:02 +02:00
|
|
|
|
|
|
|
return <WrappedComponent {...props} />
|
|
|
|
}
|
|
|
|
|
|
|
|
return HOC
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTracker
|