@sentry/browser
Version:
Official Sentry SDK for browsers
61 lines (60 loc) • 2.73 kB
JavaScript
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@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 = (0, _sentry_core.defineIntegration)((options = {}) => {
const { ignore = [], softNavigations = true, bfcacheNavigations = true } = options;
const ignored = new Set(ignore);
return {
name: WEB_VITALS_INTEGRATION_NAME,
setup(client) {
const spanStreamingEnabled = (0, _sentry_core.hasSpanStreamingEnabled)(client);
const reportSoftNavs = softNavigations && spanStreamingEnabled && (0, _sentry_browser_utils.supportsSoftNavigations)();
const reportBfcache = bfcacheNavigations && spanStreamingEnabled;
const perNavigation = reportSoftNavs || reportBfcache;
if (reportSoftNavs) {
(0, _sentry_browser_utils.enableSoftNavigationReporting)();
(0, _sentry_browser_utils.startSoftNavigationCorrelation)(client);
}
if (reportBfcache) (0, _sentry_browser_utils.enableBfcacheReporting)();
const trackClsOnPageloadSpan = !spanStreamingEnabled && !ignored.has("cls");
const trackLcpOnPageloadSpan = !spanStreamingEnabled && !ignored.has("lcp");
const finalizeWebVitals = (0, _sentry_browser_utils.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();
(0, _sentry_browser_utils.addWebVitalsToSpan)(span, {
recordClsOnPageloadSpan: trackClsOnPageloadSpan,
recordLcpOnPageloadSpan: trackLcpOnPageloadSpan,
spanStreamingEnabled
});
});
if (spanStreamingEnabled) {
if (!ignored.has("lcp")) (0, _sentry_browser_utils.trackLcpAsSpan)(client, perNavigation);
if (!ignored.has("cls")) (0, _sentry_browser_utils.trackClsAsSpan)(client, perNavigation);
}
if (!ignored.has("inp")) (0, _sentry_browser_utils.trackInpAsSpan)(client, perNavigation);
},
afterAllSetup() {
if (!ignored.has("inp")) (0, _sentry_browser_utils.registerInpInteractionListener)();
}
};
});
exports.WEB_VITALS_INTEGRATION_NAME = WEB_VITALS_INTEGRATION_NAME;
exports.webVitalsIntegration = webVitalsIntegration;
//# sourceMappingURL=webVitals.js.map