{"version":3,"file":"MaxScrollDepthInstrumentation.cjs","names":["EmbraceInstrumentationBase","MAX_SCROLL_DEPTH_EVENT_NAME","SeverityNumber","KEY_EMB_TYPE","ATTR_MAX_SCROLL_DEPTH_PIXELS","ATTR_MAX_SCROLL_DEPTH_PERCENT","ATTR_MAX_SCROLL_DEPTH_DID_SCROLL","ATTR_MAX_SCROLL_DEPTH_DOCUMENT_HEIGHT"],"sources":["../../../../src/instrumentations/max-scroll-depth/MaxScrollDepthInstrumentation/MaxScrollDepthInstrumentation.ts"],"sourcesContent":["import { SeverityNumber } from '@opentelemetry/api-logs';\nimport { EMB_TYPES, KEY_EMB_TYPE } from '../../../constants/attributes.ts';\nimport { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport {\n  ATTR_MAX_SCROLL_DEPTH_DID_SCROLL,\n  ATTR_MAX_SCROLL_DEPTH_DOCUMENT_HEIGHT,\n  ATTR_MAX_SCROLL_DEPTH_PERCENT,\n  ATTR_MAX_SCROLL_DEPTH_PIXELS,\n  MAX_SCROLL_DEPTH_EVENT_NAME,\n} from './constants.ts';\nimport type { MaxScrollDepthInstrumentationArgs } from './types.ts';\n\n/*\n  Tracks how far the user scrolls during and emits telemetry when the session part ends\n*/\nexport class MaxScrollDepthInstrumentation extends EmbraceInstrumentationBase {\n  private readonly _onScrollHandler: () => void;\n  private _hasScrolled = false;\n  private _maxScrollY = 0;\n\n  public constructor({ diag, perf }: MaxScrollDepthInstrumentationArgs = {}) {\n    super({\n      instrumentationName: 'MaxScrollDepthInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      perf,\n      config: {},\n    });\n\n    this._onScrollHandler = (): void => {\n      try {\n        // Reading scrollY here does not force a layout, so the work the listener does can stay minimal.\n        const scrollY = window.scrollY;\n        if (scrollY > this._maxScrollY) {\n          this._maxScrollY = scrollY;\n        }\n        this._hasScrolled = true;\n      } catch (e) {\n        this._diag.error('failed to process scroll', e);\n      }\n    };\n\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public override onEnable(): void {\n    window.addEventListener('scroll', this._onScrollHandler, { passive: true });\n    this.setSessionPartListeners({\n      end: () => {\n        try {\n          this._emit();\n        } catch (e) {\n          this._diag.error('failed to emit max-scroll-depth log', e);\n        }\n      },\n    });\n  }\n\n  public override onDisable(): void {\n    window.removeEventListener('scroll', this._onScrollHandler);\n  }\n\n  private _emit(): void {\n    // Read scrollHeight once here since it forces a layout reflow rather than on every scroll event.\n    // https://developer.chrome.com/docs/performance/insights/forced-reflow\n    const documentHeight = document.documentElement.scrollHeight;\n    const viewportHeight = window.innerHeight;\n    const scrollBottom = documentHeight - viewportHeight;\n    const scrollPercent =\n      scrollBottom > 0\n        ? Math.min(\n            100,\n            Math.max(0, Math.round((this._maxScrollY / scrollBottom) * 100)),\n          )\n        : 0;\n\n    this.logger.emit({\n      eventName: MAX_SCROLL_DEPTH_EVENT_NAME,\n      severityNumber: SeverityNumber.INFO,\n      attributes: {\n        [KEY_EMB_TYPE]: EMB_TYPES.OTelLog,\n        [ATTR_MAX_SCROLL_DEPTH_PIXELS]: this._maxScrollY,\n        [ATTR_MAX_SCROLL_DEPTH_PERCENT]: scrollPercent,\n        [ATTR_MAX_SCROLL_DEPTH_DID_SCROLL]: this._hasScrolled,\n        [ATTR_MAX_SCROLL_DEPTH_DOCUMENT_HEIGHT]: documentHeight,\n      },\n    });\n\n    // Initial state for the next part will be wherever the user left off the scroll position\n    this._hasScrolled = false;\n    this._maxScrollY = window.scrollY;\n  }\n}\n"],"mappings":";;;;;;AAeA,IAAa,gCAAb,cAAmDA,+EAAAA,2BAA2B;CAC5E;CACA,eAAuB;CACvB,cAAsB;CAEtB,YAAmB,EAAE,MAAM,SAA4C,CAAC,GAAG;EACzE,MAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA;GACA,QAAQ,CAAC;EACX,CAAC;EAED,KAAK,yBAA+B;GAClC,IAAI;IAEF,MAAM,UAAU,OAAO;IACvB,IAAI,UAAU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