This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgatsby-browser.js
68 lines (54 loc) · 1.7 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
61
62
63
64
65
66
67
68
let first = true
function getDuration() {
const start = window.start || new Date()
const now = new Date()
const difference = now.getTime() - start.getTime()
if (difference === 0) {
return null
}
return difference
}
export const onRouteUpdate = ({ location, prevLocation }, pluginOptions) => {
if (process.env.NODE_ENV === 'production' || window.dev === true) {
if (!window._paq) return
const { _paq, dev } = window
const url = location && location.pathname + location.search + location.hash
const prevUrl =
prevLocation &&
prevLocation.pathname + prevLocation.search + prevLocation.hash
const { trackLoad = true } = pluginOptions
// document.title workaround stolen from:
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
const sendPageView = () => {
const { title } = document
prevUrl && _paq.push(['setReferrerUrl', prevUrl])
_paq.push(['setCustomUrl', url])
_paq.push(['setDocumentTitle', title])
_paq.push(['trackPageView'])
_paq.push(['enableLinkTracking'])
_paq.push(['trackAllContentImpressions'])
if (dev) {
console.debug(`[Matomo] Page view for: ${url} - ${title}`)
}
}
// Minimum delay for reactHelmet's requestAnimationFrame
const delay = Math.max(32, 0)
setTimeout(sendPageView, delay)
if (first) {
first = false
if (trackLoad) {
_paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration()
])
}
if (dev) {
console.debug(`[Matomo] Tracking duration for: ${url}`)
}
}
}
return null
}