/** * Core Web Vitals, measured by Google's `web-vitals` library. The SDK does * not depend on the library: the app passes it in (`webVitals` option), so * web vitals are opt-in and cost nothing for apps that do not use them. * Only the shape below is read, so a 5.x or 6.x build works, including * the IIFE build's `window.webVitals`. * * - One report per hard page load, sent when the page is first hidden (tab * switch, close, navigation away). * - The library is called with `reportAllChanges`, so it reports every new * CLS, INP and LCP value as it happens and the collector always holds the * latest one. Without it the library reports those only on * `visibilitychange`, and on a navigation or a close the browser fires * `pagehide` first (beforeunload, pagehide, visibilitychange, unload): a * report made at `pagehide` would lose them. * - On a tab switch, the library's own final reports come from its hidden * listener on `window` in the capture phase (5.x and 6.x). Ours is on * `window` in the capture phase too, added after the library's, so it * runs right after it, and before the page-leave flush (a `document` * listener): the report goes out in the same keepalive request as the * buffered events. The capture phase also works where `visibilitychange` * does not bubble. The SDK needs web-vitals 5.x or later. * - SPA route changes and back/forward cache restores are not reported on * their own; CLS and INP cover the whole life of the page, as CrUX does. * - A metric the browser does not support is left out, not reported as 0. */ /** The fields of a `web-vitals` Metric that the SDK reads. */ export type WebVitalsMetric = { name: string; value: number; navigationType?: string; navigationURL?: string; }; type OnMetric = (onReport: (metric: WebVitalsMetric) => void, opts?: Record) => unknown; /** The `web-vitals` module, or any object with its `on*` functions. */ export type WebVitalsLibrary = { onLCP?: OnMetric; onINP?: OnMetric; onCLS?: OnMetric; onFCP?: OnMetric; onTTFB?: OnMetric; }; export type WebVitalsMetrics = { lcp?: number; inp?: number; cls?: number; fcp?: number; ttfb?: number; }; /** Receives the report. Returning exactly `false` means it was not accepted (not sent). */ export type WebVitalsReporter = (report: WebVitalsReport) => unknown; export type WebVitalsReport = { metrics: WebVitalsMetrics; /** The library's navigationType: navigate, reload, back-forward, prerender, restore. */ navigationType: string; /** The URL the page was loaded with, before any SPA route change. */ url: string; /** Epoch ms of the navigation start. */ startTime: number; }; /** True when `value` has at least one of the library's `on*` functions. */ export declare const isWebVitalsLibrary: (value: unknown) => value is WebVitalsLibrary; export declare class WebVitalsCollector { private readonly key; private readonly metrics; private readonly disposers; private readonly startTime; private url; private navigationType; private reported; /** Cleared by stop(): the library keeps its callbacks, which must not keep the SDK alive. */ private onReport?; /** * Start measuring with the library the app passed in. Returns undefined * when there is nothing to measure with (no library, no browser). */ static start(library: unknown, onReport: WebVitalsReporter, /** One report per document for each key (the SDK passes its write key). */ key?: string): WebVitalsCollector | undefined; /** * The URL the document was loaded with. The navigation entry keeps it even * when an SPA changed the route before the SDK started; `location` is the * fallback for browsers without Navigation Timing 2. */ private static landingUrl; /** * Epoch ms of the navigation start: `performance.timeOrigin`, else the * legacy `performance.timing.navigationStart` (older Safari and WebViews), * and only then the current time. */ private static navigationStart; private constructor(); /** Stop without reporting. The library keeps its observers; its reports are ignored. */ stop(): void; /** The metrics as reported so far. */ collect(): WebVitalsMetrics; private onMetric; private send; } export {}; //# sourceMappingURL=index.d.ts.map