Skip to content
Snippets Groups Projects
matomoTracker.ts 2.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { History, Location, UnregisterCallback } from 'history'
    import { readCozyDataFromDOM } from 'cozy-ui/transpiled/react/helpers/appDataset'
    
    interface InitSettings {
    
      cozyUrl: string
    
      url: string
      siteId: number
      history: History
      phpFilename?: string
    }
    
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    declare global {
      interface Window {
        _paq: any
    
        Piwik: any
    
    export default class MatomoTracker {
    
      cozyUrl: string
    
      url: string
      siteId: number
      phpFilename: string
      history: History
      unlistenFromHistory: UnregisterCallback
    
      constructor({
    
        cozyUrl,
    
        url,
        siteId,
        history,
        phpFilename = 'matomo.php',
      }: InitSettings) {
        if (url === undefined || siteId === undefined) {
          throw new Error(
            'MatomoTracker cannot be initialized! SiteId and url are mandatory.'
          )
        }
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        this.cozyUrl = cozyUrl
    
        this.url = url
        this.siteId = siteId
        this.phpFilename = phpFilename
        this.history = history
        this.unlistenFromHistory = () => null
        this.init()
      }
    
      init() {
        if (typeof window !== 'undefined') {
          window._paq = window._paq || []
          MatomoTracker.push(['enableHeartBeatTimer', 30])
          MatomoTracker.push(['setSiteId', this.siteId])
    
          MatomoTracker.push(['setReferrerUrl', 'https://ecolyo.com'])
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          MatomoTracker.push(['setTrackerUrl', `${this.url + this.phpFilename}`])
    
          MatomoTracker.push(['enableLinkTracking'])
        }
        return {
          push: MatomoTracker.push,
          track: this.track,
          connectToHistory: this.connectToHistory,
          disconnectFromHistory: this.disconnectFromHistory,
        }
      }
    
      static push(args: (number[] | string[] | number | string)[]) {
        window._paq.push(args)
      }
    
      configure() {
        let cozyDomain
        let userId
        const root: any = document.querySelector('[role=application]')
        if (root && root.dataset) {
          cozyDomain = readCozyDataFromDOM('cozyDomain')
        }
        if (cozyDomain) {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          userId = cozyDomain
    
          const indexOfPort = cozyDomain.indexOf(':')
          if (indexOfPort >= 0) {
            userId = userId.substring(0, indexOfPort)
          }
        }
      }
    
      connectToHistory() {
        this.unlistenFromHistory = this.history.listen(loc => {
          this.track(loc)
        })
      }
    
      disconnectFromHistory() {
        if (this.unlistenFromHistory) {
          this.unlistenFromHistory()
          return true
        }
        return false
      }
    
      track(loc: Location) {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        console.log(loc)
    
        if (typeof window === 'undefined') {
          return
        }
        const currentPath = loc.hash.substring(1)
    
        MatomoTracker.push(['setDocumentTitle', currentPath.substring(1)])
    
        MatomoTracker.push(['setCustomUrl', 'https://ecolyo.com' + currentPath])
    
        MatomoTracker.push(['trackPageView'])
      }
    }