|
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)) |
11 | 10 |
|
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 |
24 | 12 |
|
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`) |
33 | 21 | }
|
| 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 |
34 | 33 | }
|
0 commit comments