@sentry/browser
Version:
Official Sentry SDK for browsers
60 lines (59 loc) • 2.61 kB
JavaScript
import { defineIntegration, hasSpanStreamingEnabled } from "@sentry/core";
import { addWebVitalsToSpan, enableBfcacheReporting, enableSoftNavigationReporting, registerInpInteractionListener, startSoftNavigationCorrelation, startTrackingWebVitals, supportsSoftNavigations, trackClsAsSpan, trackInpAsSpan, trackLcpAsSpan } from "@sentry/browser-utils";
// src/integrations/webVitals.ts
const WEB_VITALS_INTEGRATION_NAME = "WebVitals";
/**
* Captures Core Web Vitals (LCP, CLS, INP) and related pageload vitals.
*
* `browserTracingIntegration` auto-registers this integration if no
* `webVitalsIntegration` is already present, so explicit registration is only
* needed to customize options or to use it without `browserTracingIntegration`.
*/
const webVitalsIntegration = defineIntegration((options = {}) => {
const { ignore = [], softNavigations = true, bfcacheNavigations = true } = options;
const ignored = new Set(ignore);
return {
name: WEB_VITALS_INTEGRATION_NAME,
setup(client) {
const spanStreamingEnabled = hasSpanStreamingEnabled(client);
const reportSoftNavs = softNavigations && spanStreamingEnabled && supportsSoftNavigations();
const reportBfcache = bfcacheNavigations && spanStreamingEnabled;
const perNavigation = reportSoftNavs || reportBfcache;
if (reportSoftNavs) {
enableSoftNavigationReporting();
startSoftNavigationCorrelation(client);
}
if (reportBfcache) enableBfcacheReporting();
const trackClsOnPageloadSpan = !spanStreamingEnabled && !ignored.has("cls");
const trackLcpOnPageloadSpan = !spanStreamingEnabled && !ignored.has("lcp");
const finalizeWebVitals = startTrackingWebVitals({
trackCls: trackClsOnPageloadSpan,
trackLcp: trackLcpOnPageloadSpan,
client
});
const pageloadSpans = /* @__PURE__ */ new WeakSet();
client.on("afterStartPageLoadSpan", (span) => {
pageloadSpans.add(span);
});
client.on("spanEnd", (span) => {
if (!pageloadSpans.delete(span)) return;
finalizeWebVitals();
addWebVitalsToSpan(span, {
recordClsOnPageloadSpan: trackClsOnPageloadSpan,
recordLcpOnPageloadSpan: trackLcpOnPageloadSpan,
spanStreamingEnabled
});
});
if (spanStreamingEnabled) {
if (!ignored.has("lcp")) trackLcpAsSpan(client, perNavigation);
if (!ignored.has("cls")) trackClsAsSpan(client, perNavigation);
}
if (!ignored.has("inp")) trackInpAsSpan(client, perNavigation);
},
afterAllSetup() {
if (!ignored.has("inp")) registerInpInteractionListener();
}
};
});
export { WEB_VITALS_INTEGRATION_NAME, webVitalsIntegration };
//# sourceMappingURL=webVitals.js.map