| export const roundMs = (n: number | undefined): number | undefined => |
| typeof n === 'number' && Number.isFinite(n) ? Math.round(n) : undefined; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const WEB_VITAL_SAMPLE_RATE = 0.2; |
|
|
| |
| |
| |
| |
| |
| |
| export function shouldSampleWebVital( |
| rate: number = WEB_VITAL_SAMPLE_RATE, |
| rng: () => number = Math.random, |
| ): boolean { |
| if (!(rate < 1)) return true; |
| if (rate <= 0) return false; |
| return rng() < rate; |
| } |
|
|
| export type WebVitalsFormFactor = 'mobile' | 'desktop'; |
|
|
| function mediaQueryMatches(query: string): boolean { |
| return typeof window.matchMedia === 'function' && window.matchMedia(query).matches; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getWebVitalsFormFactor(): WebVitalsFormFactor { |
| if (typeof window === 'undefined') return 'desktop'; |
| const navigatorWithUaData = window.navigator as Navigator & { |
| userAgentData?: { mobile?: boolean }; |
| }; |
| if (navigatorWithUaData.userAgentData?.mobile === true) return 'mobile'; |
| if ( |
| mediaQueryMatches('(pointer: coarse)') |
| || mediaQueryMatches('(hover: none)') |
| || mediaQueryMatches('(max-width: 1024px)') |
| ) { |
| return 'mobile'; |
| } |
| return window.innerWidth > 0 && window.innerWidth <= 1024 ? 'mobile' : 'desktop'; |
| } |
|
|
| export function sanitizeWebVitalUrl(raw: string | undefined): string { |
| if (!raw) return ''; |
| try { |
| const url = new URL(raw, typeof window !== 'undefined' ? window.location.href : 'https://worldmonitor.app/'); |
| const query = url.search ? '?[redacted]' : ''; |
| return `${url.origin}${url.pathname}${query}`; |
| } catch { |
| const [withoutQuery = raw] = raw.split('?'); |
| return withoutQuery.slice(0, 200); |
| } |
| } |
|
|