This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-browser.js
executable file
·60 lines (48 loc) · 2.35 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'whatwg-fetch'
import { analytics } from '@input-output-hk/front-end-core-libraries'
import App from './src/App'
import Root from './src/Root'
import config from './src/config'
// Setup analytics if GA tracking id is present in config
if (config.ga && config.ga.trackingID) analytics.initialize(config.ga.trackingID, { titleCase: false })
export const wrapPageElement = App
export const wrapRootElement = Root
let entryTime
const prefetchTimings = {}
const routeUpdateTimings = {}
// https://www.gatsbyjs.org/docs/browser-apis/#onClientEntry
export const onClientEntry = () => {
entryTime = Date.now()
}
// https://www.gatsbyjs.org/docs/browser-apis/#onInitialClientRender
export const onInitialClientRender = () => {
const renderTime = Date.now() - entryTime
if (entryTime) analytics.timing({ category: analytics.constants.RENDER_TIMING, label: 'Initial site render time', value: renderTime, variable: 'initial_render' })
}
const getTimingKey = location => `${location.pathname}`
// https://www.gatsbyjs.org/docs/browser-apis/#onRouteUpdate
export const onRouteUpdate = ({ location, prevLocation }) => {
const startTime = routeUpdateTimings[getTimingKey(location)]
if (startTime) {
const navigationTime = Date.now() - startTime
analytics.timing({ category: analytics.constants.NAVIGATE_TIMING, label: prevLocation ? 'navigate' : 'initial_load', value: navigationTime, variable: location.pathname })
}
analytics.pageView(location.pathname)
}
// https://www.gatsbyjs.org/docs/browser-apis/#onRouteUpdateDelayed
export const onRouteUpdateDelayed = ({ location }) => {
analytics.autoCapture({ category: analytics.constants.UX, label: location.pathname, action: 'route_update_delayed' })
}
// https://www.gatsbyjs.org/docs/browser-apis/#onPreRouteUpdate
export const onPreRouteUpdate = ({ location }) => {
routeUpdateTimings[getTimingKey(location)] = Date.now()
}
// https://www.gatsbyjs.org/docs/browser-apis/#onPrefetchPathname
export const onPrefetchPathname = ({ pathname }) => {
prefetchTimings[pathname] = Date.now()
}
// https://www.gatsbyjs.org/docs/browser-apis/#onPostPrefetchPathname
export const onPostPrefetchPathname = ({ pathname }) => {
const startTime = routeUpdateTimings[pathname]
if (startTime) analytics.timing({ category: analytics.constants.NAVIGATE_TIMING, label: 'prefetch', value: Date.now() - startTime, variable: pathname })
}