-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (58 loc) · 2.15 KB
/
index.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
export const transformer = options => ({
apply: 'post',
transform({ code, isBuild }) {
if (!isBuild) return code
return code.replace('</body>', `<script defer type="module">
const KEY = 'ga:user'
const UID = (localStorage[KEY] = localStorage[KEY] || Math.random() + '.' + Math.random())
const onError = err => console.error('[vite vitals] ', err)
const onDebug = (label, payload) => console.log('[vite vitals] ', label, payload)
function sendToAnalytics (fullPath, metric) {
const { name, delta, id, entries } = metric
const opts = {
ec: '${options.eventCategory || 'Vite Vitals'}',
ea: name,
dh: document.location.hostname,
el: id,
ev: parseInt(delta),
dp: fullPath,
ni: true
}
if (name === 'TTFB') opts.ev = parseInt(delta - entries[0].requestStart)
const args = { tid: '${options.trackingID}', cid: UID, ...opts }
const obj = { t: 'event', ...args, ...opts, z: Date.now() }
const blob = new Blob([new URLSearchParams(obj).toString()], {
type: 'application/x-www-form-urlencoded'
})
const url = 'https://www.google-analytics.com/collect?v=1'
const debug = ${options.debug}
if (debug) onDebug(name, JSON.stringify(obj, null, 2))
;(navigator.sendBeacon && navigator.sendBeacon(url, blob)) ||
fetch(url, {
body: blob,
method: 'POST',
credentials: 'omit',
keepalive: true
})
}
async function webVitals (fullPath) {
try {
const { getCLS, getFID, getLCP, getTTFB, getFCP } = await import('https://unpkg.com/[email protected]/dist/web-vitals.es5.min.js?module')
getFID(metric => sendToAnalytics(fullPath, metric))
getTTFB(metric => sendToAnalytics(fullPath, metric))
getLCP(metric => sendToAnalytics(fullPath, metric))
getCLS(metric => sendToAnalytics(fullPath, metric))
getFCP(metric => sendToAnalytics(fullPath, metric))
} catch (err) {
onError(err)
}
}
webVitals(document.location.pathname + document.location.search)
</script></body>`)
}
})
export default function (opts) {
return {
indexHtmlTransforms: [transformer(opts)]
}
}