1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00
commons/client/src/withTracker.tsx

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-04-08 19:54:18 +02:00
import React, { PureComponent } from 'react'
2019-04-08 11:35:10 +02:00
import ReactGA from 'react-ga'
2019-04-08 19:54:18 +02:00
import { analyticsId } from './config/config'
2019-04-08 11:35:10 +02:00
interface TrackerProps {
2019-04-08 19:54:18 +02:00
location: Location
2019-04-08 11:35:10 +02:00
}
2019-04-08 19:54:18 +02:00
ReactGA.initialize(analyticsId, {
testMode: process.env.NODE_ENV === 'test'
})
2019-04-08 11:35:10 +02:00
export default function withTracker(WrappedComponent: any, options: any = {}) {
2019-04-08 19:54:18 +02:00
const trackPage = (page: string) => {
options.isWeb3 = window.web3 !== undefined
2019-04-08 11:35:10 +02:00
ReactGA.set({
page,
...options
})
ReactGA.pageview(page)
}
2019-04-08 19:54:18 +02:00
return class HOC extends PureComponent<TrackerProps, {}> {
public componentDidMount() {
const page =
this.props.location.pathname + this.props.location.search
2019-04-08 11:35:10 +02:00
trackPage(page)
}
2019-04-08 19:54:18 +02:00
public componentWillReceiveProps(nextProps: any) {
const currentPage = this.props.location.pathname
const nextPage = nextProps.location.pathname
2019-04-08 11:35:10 +02:00
if (currentPage !== nextPage) {
2019-04-08 19:54:18 +02:00
trackPage(
nextProps.location.pathname + nextProps.location.search
)
2019-04-08 11:35:10 +02:00
}
}
2019-04-08 19:54:18 +02:00
public render() {
2019-04-08 11:35:10 +02:00
return <WrappedComponent {...this.props} />
}
}
2019-04-08 19:54:18 +02:00
}