UNPKG

@adyen/adyen-web

Version:

[![npm](https://img.shields.io/npm/v/@adyen/adyen-web.svg)](https://www.npmjs.com/package/@adyen/adyen-web)

3 lines (2 loc) 3.25 kB
import{debounce as t,DEFAULT_DEBOUNCE_TIME_MS as e}from"../../utils/debounce.js";import{processAnalyticsData as s}from"./utils.js";import{AnalyticsEventCategory as i}from"./events/AbstractAnalyticsEvent.js";import n from"../../utils/Storage.js";import{PLATFORM as o,CHANNEL as a,LIBRARY_BUNDLE_TYPE as r,LIBRARY_VERSION as c}from"../config.js";function d(t,e,s){return e in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}const h=e;class l{get checkoutAttemptId(){return this._checkoutAttemptId}set checkoutAttemptId(t){this._checkoutAttemptId=t}async setUp({sessionId:t,checkoutStage:e,locale:i}={}){try{const n=this.storage.get(),d=function(t){if(!t?.timestamp)return!1;const e=Date.now()-9e5;return t.timestamp>e}(n),{applicationInfo:h,checkoutAttemptId:l}=s(this.analyticsData)??{},u=d?n?.id:l,v={version:c,buildType:r,channel:a.WEB,platform:o,locale:i,referrer:window.location.href,screenWidth:window.screen.width,checkoutStage:e||"checkout",level:this.enabled?"all":"initial",...h&&{applicationInfo:h},...t&&{sessionId:t},...u&&{checkoutAttemptId:u}};this.checkoutAttemptId=await this.service.requestCheckoutAttemptId(v),this.dispatchPendingAnalytics(),this.storage.set({id:this.checkoutAttemptId,timestamp:d?n.timestamp:Date.now()})}catch(t){this.enabled=!1,console.warn("Analytics: Error setting up",t)}}sendAnalytics(t){if(this.enabled)try{this.addEventToQueue(t)}catch(t){console.warn("Analytics: Error adding event to queue",t)}}async sendFlavor(t){this.isFlavorReported||(this.checkoutAttemptId?await this.dispatchFlavor(t):this.pendingFlavor||(this.pendingFlavor=t))}async dispatchFlavor(t){if(!this.isFlavorReported&&this.checkoutAttemptId){this.isFlavorReported=!0;try{await this.service.reportIntegrationFlavor(t,this.checkoutAttemptId)}catch(t){console.warn("Analytics: Error reporting flavor",t)}}}flush(){this.sendEvents()}addEventToQueue(t){this.eventsQueue.add(t),t.getEventCategory()===i.info?this.debouncedSendInfoEvents():this.debouncedSendEvents()}async sendEvents(){if(!this.checkoutAttemptId||!this.enabled)return;if(!this.eventsQueue.hasEvents)return;const t={channel:a.WEB,platform:o,info:this.eventsQueue.infoEvents,errors:this.eventsQueue.errorEvents,logs:this.eventsQueue.logEvents};this.eventsQueue.clear();try{await this.service.sendEvents(t,this.checkoutAttemptId)}catch(t){console.warn("Analytics: Error sending events",t)}}dispatchPendingAnalytics(){if(!this.isFlavorReported&&this.pendingFlavor){const t=this.pendingFlavor;this.pendingFlavor=void 0,this.dispatchFlavor(t)}this.sendEvents()}constructor({service:e,eventQueue:s,enabled:i,analyticsData:o}){d(this,"analyticsData",void 0),d(this,"service",void 0),d(this,"eventsQueue",void 0),d(this,"storage",new n("checkout-attempt-id","sessionStorage")),d(this,"debouncedSendInfoEvents",void 0),d(this,"debouncedSendEvents",void 0),d(this,"_checkoutAttemptId",void 0),d(this,"enabled",!0),d(this,"isFlavorReported",!1),d(this,"pendingFlavor",void 0),this.service=e,this.eventsQueue=s,this.debouncedSendInfoEvents=t(this.sendEvents.bind(this),1e4),this.debouncedSendEvents=t(this.sendEvents.bind(this),h),void 0!==i&&(this.enabled=i),o&&(this.analyticsData=o)}}export{l as default}; //# sourceMappingURL=Analytics.js.map