-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-browser.js
35 lines (28 loc) · 1.09 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
exports.onRouteUpdate = ({ location }, pluginOptions = {}) => {
if (process.env.NODE_ENV !== `production` || typeof gtag !== `function`) {
return null
}
const pluginConfig = pluginOptions.pluginConfig || {}
const pathIsExcluded =
location &&
typeof window.excludeGtagPaths !== `undefined` &&
window.excludeGtagPaths.some(rx => rx.test(location.pathname))
if (pathIsExcluded) return null
// wrap inside a timeout to make sure react-helmet is done with its changes (https://github.com/gatsbyjs/gatsby/issues/11592)
const sendPageView = () => {
const pagePath = location
? location.pathname + location.search + location.hash
: undefined
window.gtag(`event`, `page_view`, { page_path: pagePath })
}
const { delayOnRouteUpdate = 0 } = pluginConfig
if (`requestAnimationFrame` in window) {
requestAnimationFrame(() => {
requestAnimationFrame(() => setTimeout(sendPageView, delayOnRouteUpdate))
})
} else {
// Delay by 32ms to simulate 2 requestOnAnimationFrame calls
setTimeout(sendPageView, 32 + delayOnRouteUpdate)
}
return null
}