Skip to content

Commit

Permalink
fix(gatsby-plugin-google-analytics): Refactor gatsby-plugin-google-an…
Browse files Browse the repository at this point in the history
…alytics gatsby-browser.js (#14572)

The gatsby-browser.js file in gatsby-plugin-google-analytics and
gatsby-plugin-google-gtag have borderline identical functionality howeve
that is slightly obscured as their implementation is slightly different,
notably gatsby-plugin-google-analytics does not use the early return
pattern which means the nesting of everything is different if you try
and diff the two files.

This commit makes gatsby-plugin-google-analytics use the early return
pattern to make it applying the same changes to this an
gatsby-plugin-google-gtag easier.
  • Loading branch information
BPScott authored and freiksenet committed Jun 10, 2019
1 parent 48dbfd7 commit 9f2c0c9
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
exports.onRouteUpdate = function({ location }) {
// Don't track while developing.
if (process.env.NODE_ENV === `production` && typeof ga === `function`) {
if (
location &&
typeof window.excludeGAPaths !== `undefined` &&
window.excludeGAPaths.some(rx => rx.test(location.pathname))
) {
return
}
exports.onRouteUpdate = ({ location }) => {
if (process.env.NODE_ENV !== `production` || typeof ga !== `function`) {
return null
}

const pathIsExcluded =
location &&
typeof window.excludeGAPaths !== `undefined` &&
window.excludeGAPaths.some(rx => rx.test(location.pathname))

// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
// reactHelmet is using requestAnimationFrame so we should use it too: https://github.com/nfl/react-helmet/blob/5.2.0/src/HelmetUtils.js#L296-L299
const sendPageView = () => {
window.ga(
`set`,
`page`,
location
? location.pathname + location.search + location.hash
: undefined
)
window.ga(`send`, `pageview`)
}
if (pathIsExcluded) return null

if (`requestAnimationFrame` in window) {
requestAnimationFrame(() => {
requestAnimationFrame(sendPageView)
})
} else {
// simulate 2 rAF calls
setTimeout(sendPageView, 32)
}
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
// reactHelmet is using requestAnimationFrame so we should use it too: https://github.com/nfl/react-helmet/blob/5.2.0/src/HelmetUtils.js#L296-L299
const sendPageView = () => {
const pagePath = location
? location.pathname + location.search + location.hash
: undefined
window.ga(`set`, `page`, pagePath)
window.ga(`send`, `pageview`)
}

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

return null
}

0 comments on commit 9f2c0c9

Please sign in to comment.