1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-08 23:25:02 +00:00
mon-entreprise/source/Tracker.ts
Maxime Quandalle ca5b7cc2df
Ajout des types null et undefined
Ajout des paramètres strictNullChecks et strictPropertyInitialization
dans la configuration TypeScript et correction des environ 70 erreurs
de typage résultantes.
2019-12-18 18:52:32 +01:00

53 lines
1.1 KiB
TypeScript

import { History, Location } from 'history'
import { debounce } from './utils'
declare global {
interface Window {
_paq: any
}
}
type PushArgs = ['trackPageView'] | ['trackEvent', ...Array<string | number>]
type PushType = (args: PushArgs) => void
export default class Tracker {
push: PushType
unlistenFromHistory: (() => void) | undefined
previousPath: string | undefined
constructor(pushFunction: PushType = args => window._paq.push(args)) {
if (typeof window !== 'undefined') window._paq = window._paq || []
this.push = debounce(200, pushFunction) as PushType
}
connectToHistory(history: History) {
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?.debug?.bind(console) ?? (() => {}) // eslint-disable-line no-console
)