gatsby-plugin-matomo/src/gatsby-browser.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
2018-05-07 18:29:27 +02:00
let first = true
function getDuration() {
const start = window.start || new Date()
const now = new Date()
const difference = now.getTime() - start.getTime()
if (difference === 0) {
return null
}
return difference
}
exports.onRouteUpdate = ({ location }) => {
2018-05-14 22:15:53 +02:00
if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined') {
window._paq = window._paq || []
window.dev = window.dev || null
const pathname = location.pathname
2018-05-07 18:29:27 +02:00
if (first) {
first = false
window._paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration()
])
if (window.dev) {
console.log(`[Matomo] Page view for: ${pathname}`)
}
} else {
2018-06-28 19:53:03 +02:00
window._paq.push(['setReferrerUrl', pathname])
window._paq.push(['setCustomUrl', pathname])
window._paq.push(['setDocumentTitle', pathname])
window._paq.push(['trackPageView'])
2018-06-28 19:53:03 +02:00
window._paq.push(['enableLinkTracking'])
if (window.dev) {
console.log(`[Matomo] Page view for: ${pathname}`)
}
}
2018-05-07 18:29:27 +02:00
}
return null
2018-05-07 18:29:27 +02:00
}