Skip to content

Commit 9f2c0c9

Browse files
BPScottfreiksenet
authored andcommitted
fix(gatsby-plugin-google-analytics): Refactor gatsby-plugin-google-analytics 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.
1 parent 48dbfd7 commit 9f2c0c9

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
exports.onRouteUpdate = function({ location }) {
2-
// Don't track while developing.
3-
if (process.env.NODE_ENV === `production` && typeof ga === `function`) {
4-
if (
5-
location &&
6-
typeof window.excludeGAPaths !== `undefined` &&
7-
window.excludeGAPaths.some(rx => rx.test(location.pathname))
8-
) {
9-
return
10-
}
1+
exports.onRouteUpdate = ({ location }) => {
2+
if (process.env.NODE_ENV !== `production` || typeof ga !== `function`) {
3+
return null
4+
}
5+
6+
const pathIsExcluded =
7+
location &&
8+
typeof window.excludeGAPaths !== `undefined` &&
9+
window.excludeGAPaths.some(rx => rx.test(location.pathname))
1110

12-
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
13-
// 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
14-
const sendPageView = () => {
15-
window.ga(
16-
`set`,
17-
`page`,
18-
location
19-
? location.pathname + location.search + location.hash
20-
: undefined
21-
)
22-
window.ga(`send`, `pageview`)
23-
}
11+
if (pathIsExcluded) return null
2412

25-
if (`requestAnimationFrame` in window) {
26-
requestAnimationFrame(() => {
27-
requestAnimationFrame(sendPageView)
28-
})
29-
} else {
30-
// simulate 2 rAF calls
31-
setTimeout(sendPageView, 32)
32-
}
13+
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
14+
// 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
15+
const sendPageView = () => {
16+
const pagePath = location
17+
? location.pathname + location.search + location.hash
18+
: undefined
19+
window.ga(`set`, `page`, pagePath)
20+
window.ga(`send`, `pageview`)
3321
}
22+
23+
if (`requestAnimationFrame` in window) {
24+
requestAnimationFrame(() => {
25+
requestAnimationFrame(sendPageView)
26+
})
27+
} else {
28+
// simulate 2 rAF calls
29+
setTimeout(sendPageView, 32)
30+
}
31+
32+
return null
3433
}

0 commit comments

Comments
 (0)