{"version":3,"file":"ClicksInstrumentation.cjs","names":["EmbraceInstrumentationBase","getHTMLElementFriendlyName"],"sources":["../../../../src/instrumentations/clicks/ClicksInstrumentation/ClicksInstrumentation.ts"],"sourcesContent":["/*\n  This instrumentation is taking code from here as a starting point:\n    https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts\n\n  But not using that instrumentation directly because:\n\n    1) We want to record clicks as span events on the Session span\n    2) There is a bug with the instrumentation that causes a large number of spans to be created for a single click\n      when zone context isn't available. To avoid the bug we take a simpler approach here and add a listener directly\n      to `document` rather than trying to patch the `addEventListener` method on the prototype, this does mean we miss\n      recording click events for which `stopPropagation` is called. See https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1368\n */\n\nimport { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport type { ClicksInstrumentationArgs } from './types.ts';\n\nimport { getHTMLElementFriendlyName } from './utils.ts';\n\nexport class ClicksInstrumentation extends EmbraceInstrumentationBase {\n  private readonly _onClickHandler: (event: MouseEvent) => void;\n\n  public constructor({\n    diag,\n    perf,\n    shouldTrack,\n    innerTextForElement,\n  }: ClicksInstrumentationArgs = {}) {\n    super({\n      instrumentationName: 'ClicksInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      perf,\n      config: {},\n    });\n\n    this._onClickHandler = (event: MouseEvent) => {\n      const element = event.target;\n\n      if (!(element instanceof HTMLElement)) {\n        return;\n      }\n      if (element.hasAttribute('disabled')) {\n        return;\n      }\n\n      if (shouldTrack && !shouldTrack(element)) {\n        return;\n      }\n\n      try {\n        const currentSessionPartSpan =\n          this.userSessionManager.getSessionPartSpan();\n        if (currentSessionPartSpan) {\n          currentSessionPartSpan.addEvent(\n            'click',\n            {\n              'emb.type': 'ux.tap',\n              'view.name': getHTMLElementFriendlyName(\n                element,\n                innerTextForElement\n                  ? innerTextForElement(element)\n                  : // biome-ignore lint/nursery/useDomNodeTextContent: we want to capture what the user sees\n                    element.innerText,\n              ),\n              'tap.coords': `${event.x.toString()},${event.y.toString()}`,\n            },\n            this.perf.epochMillisFromOrigin(event.timeStamp),\n          );\n        }\n      } catch (e) {\n        this._diag.error('failed to create new user interaction span event', e);\n      }\n    };\n\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public override onDisable(): void {\n    document.removeEventListener('click', this._onClickHandler);\n  }\n\n  public override onEnable(): void {\n    document.addEventListener('click', this._onClickHandler);\n  }\n}\n"],"mappings":";;;;AAkBA,IAAa,wBAAb,cAA2CA,+EAAAA,2BAA2B;CACpE;CAEA,YAAmB,EACjB,MACA,MACA,aACA,wBAC6B,CAAC,GAAG;EACjC,MAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA;GACA,QAAQ,CAAC;EACX,CAAC;EAED,KAAK,mBAAmB,UAAsB;GAC5C,MAAM,UAAU,MAAM;GAEtB,IAAI,EAAE,mBAAmB,cACvB;GAEF,IAAI,QAAQ,aAAa,UAAU,GACjC;GAGF,IAAI,eAAe,CAAC,YAAY,OAAO,GACrC;GAGF,IAAI;IACF,MAAM,yBACJ,KAAK,mBAAmB,mBAAmB;IAC7C,IAAI,wBACF,uBAAuB,SACrB,SACA;KACE,YAAY;KACZ,aAAaC,4DAAAA,2BACX,SACA,sBACI,oBAAoB,OAAO,IAE3B,QAAQ,SACd;KACA,cAAc,GAAG,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,EAAE,SAAS;IAC1D,GACA,KAAK,KAAK,sBAAsB,MAAM,SAAS,CACjD;GAEJ,SAAS,GAAG;IACV,KAAK,MAAM,MAAM,oDAAoD,CAAC;GACxE;EACF;EAEA,IAAI,KAAK,QAAQ,SACf,KAAK,OAAO;CAEhB;CAEA,YAAkC;EAChC,SAAS,oBAAoB,SAAS,KAAK,eAAe;CAC5D;CAEA,WAAiC;EAC/B,SAAS,iBAAiB,SAAS,KAAK,eAAe;CACzD;AACF"}