-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcrux-metrics.js
More file actions
27 lines (19 loc) · 919 Bytes
/
crux-metrics.js
File metadata and controls
27 lines (19 loc) · 919 Bytes
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
import { getDeviceInfo, collectLoad, collectFcp, collectLcp, collectFid, collectCls, onVisibilityChange } from '..'
// init `metrics` and get device information
const { url, connection } = getDeviceInfo()
const metrics = { url, effectiveConnectionType: connection.effectiveType }
// collect loading metrics
collectLoad(({ value: load, detail: { domContentLoaded, timeToFirstByte } }) => {
metrics.timeToFirstByte = timeToFirstByte
metrics.domContentLoaded = domContentLoaded
metrics.load = load
})
// collect user-centric metrics
collectFcp(({ value }) => (metrics.firstContentfulPaint = value))
collectLcp(({ value }) => (metrics.largestContentfulPaint = value))
collectFid(({ value }) => (metrics.firstInputDelay = value))
collectCls(({ value }) => (metrics.cumulativeLayoutShift = value))
// all metrics are collected on "visibilitychange" event
onVisibilityChange(() => {
console.log(metrics)
}, 1)