Skip to content

Commit

Permalink
Merge pull request #15 from kremalicious/fix/page-tracking
Browse files Browse the repository at this point in the history
Page tracking improvements
  • Loading branch information
kremalicious authored Mar 30, 2019
2 parents 24f57da + aedb806 commit b9cf5d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
52 changes: 34 additions & 18 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,51 @@ function getDuration() {
return difference
}

exports.onRouteUpdate = ({ location }) => {
if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined') {
window._paq = window._paq || []
window.dev = window.dev || null
exports.onRouteUpdate = ({ location, prevLocation }) => {
if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined' || window.dev === true) {
const _paq = window._paq || []
const dev = window.dev || null

const pathname = location.pathname
const url = location.pathname + location.search + location.hash
const prevUrl = prevLocation && prevLocation.pathname + prevLocation.search + prevLocation.hash

// document.title workaround stolen from:
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
const sendPageView = () => {
const { title } = document

prevUrl && _paq.push(['setReferrerUrl', prevUrl])
_paq.push(['setCustomUrl', url])
_paq.push(['setDocumentTitle', title])
_paq.push(['trackPageView'])
_paq.push(['enableLinkTracking'])

if (dev) {
console.log(`[Matomo] Page view for: ${url} - ${title}`)
}
}

if ('requestAnimationFrame' in window) {
requestAnimationFrame(() => {
requestAnimationFrame(sendPageView)
})
} else {
// simulate 2 rAF calls
setTimeout(sendPageView, 32)
}

if (first) {
first = false
window._paq.push([
_paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration()
])

if (window.dev) {
console.log(`[Matomo] Page view for: ${pathname}`)
}
} else {
window._paq.push(['setReferrerUrl', pathname])
window._paq.push(['setCustomUrl', pathname])
window._paq.push(['setDocumentTitle', pathname])
window._paq.push(['trackPageView'])
window._paq.push(['enableLinkTracking'])

if (window.dev) {
console.log(`[Matomo] Page view for: ${pathname}`)
if (dev) {
console.log(`[Matomo] Tracking duration for: ${url}`)
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ function buildTrackingCode(pluginOptions) {
${pluginOptions.disableCookies ? 'window._paq.push([\'disableCookies\']);' : ''}
window._paq.push(['setTrackerUrl', '${pluginOptions.matomoUrl}/piwik.php']);
window._paq.push(['setSiteId', '${pluginOptions.siteId}']);
window._paq.push(['trackPageView']);
window._paq.push(['enableLinkTracking']);
window._paq.push(['enableHeartBeatTimer']);
window.start = new Date();
Expand Down

0 comments on commit b9cf5d3

Please sign in to comment.