From 6c0a840de21ce46f544b6ce9a7dd63e961e7ab6b Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 May 2018 12:45:47 +0200 Subject: [PATCH] do nothing on route updates when piwik isn't loaded and in development --- src/gatsby-browser.js | 31 +++++++++++++++++-------------- src/gatsby-ssr.js | 16 ++++++++-------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js index 8a009f8..c4c5417 100644 --- a/src/gatsby-browser.js +++ b/src/gatsby-browser.js @@ -13,20 +13,23 @@ function getDuration() { } exports.onRouteUpdate = ({ location }) => { - window._paq = window._paq || [] + if (process.env.NODE_ENV !== 'production' && typeof _paq !== 'undefined') { + window._paq = window._paq || [] - if (first) { - first = false - window._paq.push([ - 'trackEvent', - 'javascript', - 'load', - 'duration', - getDuration(), - ]) - } else { - window._paq.push(['setCustomUrl', location.pathname]) - window._paq.push(['setDocumentTitle', location.pathname]) - window._paq.push(['trackPageView']) + if (first) { + first = false + window._paq.push([ + 'trackEvent', + 'javascript', + 'load', + 'duration', + getDuration() + ]) + } else { + window._paq.push(['setCustomUrl', location.pathname]) + window._paq.push(['setDocumentTitle', location.pathname]) + window._paq.push(['trackPageView']) + } } + return null } diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js index 9a3ac46..d73bd69 100644 --- a/src/gatsby-ssr.js +++ b/src/gatsby-ssr.js @@ -40,14 +40,14 @@ function buildTrackingCodeNoJs(siteId, matomoUrl, siteUrl, pathname) { exports.onRenderBody = ({ setPostBodyComponents, pathname }, pluginOptions) => { if (process.env.NODE_ENV === 'production') { - const siteId = pluginOptions.siteId - const siteUrl = pluginOptions.siteUrl - const matomoUrl = pluginOptions.matomoUrl + const siteId = pluginOptions.siteId + const siteUrl = pluginOptions.siteUrl + const matomoUrl = pluginOptions.matomoUrl - return setPostBodyComponents([ - buildTrackingCode(siteId, matomoUrl), - buildTrackingCodeNoJs(siteId, matomoUrl, siteUrl, pathname) - ]) - } + return setPostBodyComponents([ + buildTrackingCode(siteId, matomoUrl), + buildTrackingCodeNoJs(siteId, matomoUrl, siteUrl, pathname) + ]) + } return null }