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

52 lines
1.3 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, prevLocation }) => {
2019-03-30 14:38:22 +01:00
if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined' || window.dev === true) {
window._paq = window._paq || []
window.dev = window.dev || null
const url = location.pathname + location.search + location.hash
const prevUrl = prevLocation && prevLocation.pathname + prevLocation.search + prevLocation.hash
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: ${url}`)
}
} else {
window._paq.push(['setReferrerUrl', prevUrl])
window._paq.push(['setCustomUrl', url])
window._paq.push(['setDocumentTitle', url])
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: ${url}`)
}
}
2018-05-07 18:29:27 +02:00
}
return null
2018-05-07 18:29:27 +02:00
}