@embrace-io/web-sdk
Version:
65 lines (64 loc) • 2.98 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_constants_attributes = require("../../../constants/attributes.cjs");
const require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase = require("../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.cjs");
const require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants = require("./constants.cjs");
let _opentelemetry_api_logs = require("@opentelemetry/api-logs");
//#region src/instrumentations/max-scroll-depth/MaxScrollDepthInstrumentation/MaxScrollDepthInstrumentation.ts
var MaxScrollDepthInstrumentation = class extends require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase.EmbraceInstrumentationBase {
_onScrollHandler;
_hasScrolled = false;
_maxScrollY = 0;
constructor({ diag, perf } = {}) {
super({
instrumentationName: "MaxScrollDepthInstrumentation",
instrumentationVersion: "1.0.0",
diag,
perf,
config: {}
});
this._onScrollHandler = () => {
try {
const scrollY = window.scrollY;
if (scrollY > this._maxScrollY) this._maxScrollY = scrollY;
this._hasScrolled = true;
} catch (e) {
this._diag.error("failed to process scroll", e);
}
};
if (this._config.enabled) this.enable();
}
onEnable() {
window.addEventListener("scroll", this._onScrollHandler, { passive: true });
this.setSessionPartListeners({ end: () => {
try {
this._emit();
} catch (e) {
this._diag.error("failed to emit max-scroll-depth log", e);
}
} });
}
onDisable() {
window.removeEventListener("scroll", this._onScrollHandler);
}
_emit() {
const documentHeight = document.documentElement.scrollHeight;
const scrollBottom = documentHeight - window.innerHeight;
const scrollPercent = scrollBottom > 0 ? Math.min(100, Math.max(0, Math.round(this._maxScrollY / scrollBottom * 100))) : 0;
this.logger.emit({
eventName: require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants.MAX_SCROLL_DEPTH_EVENT_NAME,
severityNumber: _opentelemetry_api_logs.SeverityNumber.INFO,
attributes: {
[require_constants_attributes.KEY_EMB_TYPE]: "emb.otel_log",
[require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants.ATTR_MAX_SCROLL_DEPTH_PIXELS]: this._maxScrollY,
[require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants.ATTR_MAX_SCROLL_DEPTH_PERCENT]: scrollPercent,
[require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants.ATTR_MAX_SCROLL_DEPTH_DID_SCROLL]: this._hasScrolled,
[require_instrumentations_max_scroll_depth_MaxScrollDepthInstrumentation_constants.ATTR_MAX_SCROLL_DEPTH_DOCUMENT_HEIGHT]: documentHeight
}
});
this._hasScrolled = false;
this._maxScrollY = window.scrollY;
}
};
//#endregion
exports.MaxScrollDepthInstrumentation = MaxScrollDepthInstrumentation;
//# sourceMappingURL=MaxScrollDepthInstrumentation.cjs.map