1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-08 21:05:01 +00:00
mon-entreprise/source/Tracker.js
Johan Girod a6a4c2dc01
📈 charge le script de tracking en même temps que la page
plutôt que de le charger dans le bundle principale.

- Permet d'avoir une estimation du temps de chargement
- Peut-être que le problème du nombre d'entrance plus faible que le nombre de visite sera ainsi reglé
2019-07-04 12:00:23 +02:00

52 lines
1.1 KiB
JavaScript

/* @flow */
import { debounce } from './utils'
import type { BrowserHistory } from 'history/createBrowserHistory'
type PushType = (
| ['trackPageView']
| ['trackEvent', string, string]
| ['trackEvent', string, string, string]
| ['trackEvent', string, string, string, number]
) => void
export default class Tracker {
push: PushType
unlistenFromHistory: () => void
previousPath: string
constructor(pushFunction: PushType = args => window._paq.push(args)) {
window._paq = window._paq || []
this.push = debounce(200, pushFunction)
}
connectToHistory(history: BrowserHistory) {
this.unlistenFromHistory = history.listen(loc => {
this.track(loc)
})
return history
}
disconnectFromHistory() {
if (this.unlistenFromHistory) {
this.unlistenFromHistory()
return true
}
return false
}
track(loc: Location) {
const currentPath = loc.pathname + loc.search
if (this.previousPath === currentPath) {
return
}
this.push(['trackPageView'])
this.previousPath = currentPath
}
}
export const devTracker = new Tracker(
(console && console.log && console.log.bind(console)) || (() => {}) // eslint-disable-line no-console
)