2017-10-16 17:08:08 +00:00
|
|
|
export default class Piwik {
|
2018-01-03 15:54:19 +00:00
|
|
|
constructor(opts) {
|
|
|
|
const options = opts
|
|
|
|
|
|
|
|
options.enableLinkTracking =
|
|
|
|
options.enableLinkTracking !== undefined
|
|
|
|
? options.enableLinkTracking
|
|
|
|
: true
|
|
|
|
options.trackDocumentTitle =
|
|
|
|
options.trackDocumentTitle !== undefined
|
|
|
|
? options.trackDocumentTitle
|
|
|
|
: true
|
|
|
|
|
|
|
|
this.options = options
|
|
|
|
|
2018-08-23 14:42:02 +00:00
|
|
|
if (this.options.url === undefined || this.options.siteId === undefined) {
|
2018-01-03 15:54:19 +00:00
|
|
|
throw new Error(
|
|
|
|
'PiwikTracker cannot be initialized! SiteId and url are mandatory.'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initPiwik()
|
|
|
|
}
|
|
|
|
|
|
|
|
initPiwik() {
|
|
|
|
let url = this.options.url
|
|
|
|
|
|
|
|
if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1) {
|
|
|
|
url = `${url}/`
|
|
|
|
} else {
|
|
|
|
url =
|
|
|
|
document.location.protocol === 'https:'
|
|
|
|
? `https://${url}/`
|
|
|
|
: `http://${url}/`
|
|
|
|
}
|
|
|
|
|
|
|
|
window._paq = window._paq || [] // eslint-disable-line no-underscore-dangle
|
|
|
|
|
2018-10-11 13:53:15 +00:00
|
|
|
// DPRG Compliant Piwik
|
|
|
|
// See https://www.cnil.fr/sites/default/files/typo/document/Configuration_piwik.pdf
|
|
|
|
Piwik.push([
|
|
|
|
function() {
|
|
|
|
var self = this
|
|
|
|
function getOriginalVisitorCookieTimeout() {
|
|
|
|
var now = new Date(),
|
|
|
|
nowTs = Math.round(now.getTime() / 1000),
|
|
|
|
visitorInfo = self.getVisitorInfo()
|
|
|
|
var createTs = parseInt(visitorInfo[2])
|
|
|
|
var cookieTimeout = 33696000 // 13 mois en secondes
|
|
|
|
var originalTimeout = createTs + cookieTimeout - nowTs
|
|
|
|
return originalTimeout
|
|
|
|
}
|
|
|
|
this.setVisitorCookieTimeout(getOriginalVisitorCookieTimeout())
|
|
|
|
}
|
|
|
|
])
|
2018-01-03 15:54:19 +00:00
|
|
|
Piwik.push(['setSiteId', this.options.siteId])
|
|
|
|
Piwik.push(['setTrackerUrl', `${url}piwik.php`])
|
|
|
|
|
|
|
|
if (this.options.enableLinkTracking) {
|
|
|
|
Piwik.push(['enableLinkTracking'])
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptElement = document.createElement('script')
|
|
|
|
const refElement = document.getElementsByTagName('script')[0]
|
|
|
|
|
|
|
|
scriptElement.type = 'text/javascript'
|
|
|
|
scriptElement.defer = true
|
|
|
|
scriptElement.async = true
|
|
|
|
scriptElement.src = `${url}piwik.js`
|
|
|
|
refElement.parentNode.insertBefore(scriptElement, refElement)
|
|
|
|
|
|
|
|
return {
|
|
|
|
push: this.push,
|
|
|
|
track: this.track,
|
|
|
|
connectToHistory: this.connectToHistory,
|
|
|
|
disconnectFromHistory: this.disconnectFromHistory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static push(args) {
|
|
|
|
window._paq.push(args) // eslint-disable-line no-underscore-dangle
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:27:01 +00:00
|
|
|
push(args) {
|
|
|
|
Piwik.push(args)
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
connectToHistory(history) {
|
|
|
|
this.unlistenFromHistory = history.listen(loc => {
|
|
|
|
this.track(loc)
|
|
|
|
})
|
|
|
|
|
|
|
|
return history
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectFromHistory() {
|
|
|
|
if (this.unlistenFromHistory) {
|
|
|
|
this.unlistenFromHistory()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
track(loc) {
|
|
|
|
const currentPath =
|
|
|
|
loc.path || loc.pathname + loc.search /*.replace(/^\//, '')*/
|
|
|
|
|
|
|
|
if (this.previousPath === currentPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.trackDocumentTitle) {
|
|
|
|
Piwik.push(['setDocumentTitle', document.title])
|
|
|
|
}
|
|
|
|
|
|
|
|
Piwik.push(['setCustomUrl', currentPath])
|
|
|
|
Piwik.push(['trackPageView'])
|
|
|
|
|
|
|
|
this.previousPath = currentPath
|
|
|
|
}
|
2017-10-16 17:08:08 +00:00
|
|
|
}
|