@embrace-io/web-sdk
Version:
112 lines (111 loc) • 4.54 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils_getSelector = require("../../../utils/getSelector.cjs");
const require_constants_attributes = require("../../../constants/attributes.cjs");
const require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase = require("../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.cjs");
const require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants = require("./constants.cjs");
let _opentelemetry_api_logs = require("@opentelemetry/api-logs");
//#region src/instrumentations/first-interaction/FirstInteractionInstrumentation/FirstInteractionInstrumentation.ts
var FirstInteractionInstrumentation = class extends require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase.EmbraceInstrumentationBase {
_onClick;
_onKeyDown;
_onScroll;
_listenersAttached = false;
constructor({ diag, perf } = {}) {
super({
instrumentationName: "FirstInteractionInstrumentation",
instrumentationVersion: "1.0.0",
diag,
perf,
config: {}
});
this._onClick = (event) => {
this._handle({
interactionType: event.pointerType === "touch" ? "tap" : "click",
target: event.target instanceof Element ? event.target : null,
timestamp: event.timeStamp,
x: event.clientX,
y: event.clientY
});
};
this._onKeyDown = (event) => {
this._handle({
interactionType: "keypress",
target: event.target instanceof Element ? event.target : null,
timestamp: event.timeStamp
});
};
this._onScroll = (event) => {
this._handle({
interactionType: "scroll",
target: null,
timestamp: event.timeStamp
});
};
if (this._config.enabled) this.enable();
}
onEnable() {
this._attachListeners();
this.setSessionPartListeners({ start: () => {
this._attachListeners();
} });
}
onDisable() {
this._detachListeners();
}
_handle(data) {
if (!this._listenersAttached) return;
this._detachListeners();
try {
this._emit(data);
} catch (e) {
this._diag.error("failed to process first-interaction candidate", e);
}
}
_attachListeners() {
if (this._listenersAttached) return;
const opts = {
capture: true,
passive: true
};
document.addEventListener("click", this._onClick, opts);
document.addEventListener("keydown", this._onKeyDown, opts);
document.addEventListener("scroll", this._onScroll, opts);
this._listenersAttached = true;
}
_detachListeners() {
if (!this._listenersAttached) return;
const opts = { capture: true };
document.removeEventListener("click", this._onClick, opts);
document.removeEventListener("keydown", this._onKeyDown, opts);
document.removeEventListener("scroll", this._onScroll, opts);
this._listenersAttached = false;
}
_emit(data) {
let elementType = "document";
let elementSelector = "";
if (data.interactionType !== "scroll" && data.target) {
elementType = data.target.tagName.toLowerCase();
elementSelector = require_utils_getSelector.getSelector(data.target);
}
const attributes = {
[require_constants_attributes.KEY_EMB_TYPE]: "emb.otel_log",
[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_INTERACTION_TYPE]: data.interactionType,
[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_ELEMENT_TYPE]: elementType,
[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_ELEMENT_SELECTOR]: elementSelector,
[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_TIME]: this.perf.millisFromZeroTime(data.timestamp)
};
if (data.x !== void 0 && data.y !== void 0) {
attributes[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_X] = data.x;
attributes[require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.ATTR_FIRST_INTERACTION_Y] = data.y;
}
this.logger.emit({
timestamp: this.perf.epochMillisFromOrigin(data.timestamp),
eventName: require_instrumentations_first_interaction_FirstInteractionInstrumentation_constants.FIRST_INTERACTION_EVENT_NAME,
severityNumber: _opentelemetry_api_logs.SeverityNumber.INFO,
attributes
});
}
};
//#endregion
exports.FirstInteractionInstrumentation = FirstInteractionInstrumentation;
//# sourceMappingURL=FirstInteractionInstrumentation.cjs.map