()
| 4 | import { useLogContext } from '../contexts/LogContext'; |
| 5 | |
| 6 | export function useWebVitals(): void { |
| 7 | const { logEvent } = useLogContext(); |
| 8 | |
| 9 | const logMetric = useCallback( |
| 10 | (metric: Metric) => { |
| 11 | const { name, entries, id, ...rest } = metric; |
| 12 | logEvent({ event_name: name, extra: JSON.stringify(rest) }); |
| 13 | }, |
| 14 | [logEvent], |
| 15 | ); |
| 16 | |
| 17 | // Have to wait for document to load and ensure it's not run on SSR |
| 18 | useEffect(() => { |
| 19 | // We apply sampling rate of 10% |
| 20 | if (Math.random() < 0.1) { |
| 21 | onCLS(logMetric); |
| 22 | onFID(logMetric); |
| 23 | onLCP(logMetric); |
| 24 | onTTFB(logMetric); |
| 25 | onFCP(logMetric); |
| 26 | } |
| 27 | // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM |
| 28 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 29 | }, []); |
| 30 | } |
no test coverage detected