From 04b8ba19038f49571a0f19099f9af4575739a8fb Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 30 Mar 2019 14:30:50 +0100 Subject: [PATCH 1/3] better page url & referrer url tracking --- src/gatsby-browser.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js index c396b8a..7bb03ff 100644 --- a/src/gatsby-browser.js +++ b/src/gatsby-browser.js @@ -14,12 +14,13 @@ function getDuration() { return difference } -exports.onRouteUpdate = ({ location }) => { +exports.onRouteUpdate = ({ location, prevLocation }) => { if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined') { window._paq = window._paq || [] window.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 if (first) { first = false @@ -32,17 +33,17 @@ exports.onRouteUpdate = ({ location }) => { ]) if (window.dev) { - console.log(`[Matomo] Page view for: ${pathname}`) + console.log(`[Matomo] Page view for: ${url}`) } } else { - window._paq.push(['setReferrerUrl', pathname]) - window._paq.push(['setCustomUrl', pathname]) - window._paq.push(['setDocumentTitle', pathname]) + window._paq.push(['setReferrerUrl', prevUrl]) + window._paq.push(['setCustomUrl', url]) + window._paq.push(['setDocumentTitle', url]) window._paq.push(['trackPageView']) window._paq.push(['enableLinkTracking']) if (window.dev) { - console.log(`[Matomo] Page view for: ${pathname}`) + console.log(`[Matomo] Page view for: ${url}`) } } } From 7b7f4783a6d77c2f3c30d78dc20f699b791c2756 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 30 Mar 2019 14:38:22 +0100 Subject: [PATCH 2/3] always fire route updates in dev mode --- src/gatsby-browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js index 7bb03ff..2d129c7 100644 --- a/src/gatsby-browser.js +++ b/src/gatsby-browser.js @@ -15,7 +15,7 @@ function getDuration() { } exports.onRouteUpdate = ({ location, prevLocation }) => { - if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined') { + if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined' || window.dev === true) { window._paq = window._paq || [] window.dev = window.dev || null From aedb80684fd485534e90d0f453ecd9efd688fa64 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 30 Mar 2019 17:09:23 +0100 Subject: [PATCH 3/3] capture and send custom page title * workaround to delay page tracking until react-helmet is finished updating document.title * closes #11 --- src/gatsby-browser.js | 45 ++++++++++++++++++++++++++++--------------- src/gatsby-ssr.js | 2 -- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js index 2d129c7..ef767da 100644 --- a/src/gatsby-browser.js +++ b/src/gatsby-browser.js @@ -16,15 +16,40 @@ function getDuration() { exports.onRouteUpdate = ({ location, prevLocation }) => { if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined' || window.dev === true) { - window._paq = window._paq || [] - window.dev = window.dev || null + const _paq = window._paq || [] + const dev = window.dev || null 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', @@ -32,18 +57,8 @@ exports.onRouteUpdate = ({ location, prevLocation }) => { 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']) - window._paq.push(['enableLinkTracking']) - - if (window.dev) { - console.log(`[Matomo] Page view for: ${url}`) + if (dev) { + console.log(`[Matomo] Tracking duration for: ${url}`) } } } diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js index 3b73980..7d4f559 100644 --- a/src/gatsby-ssr.js +++ b/src/gatsby-ssr.js @@ -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();