@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 3.62 kB
JavaScript
import{debounce as t,DEFAULT_DEBOUNCE_TIME_MS as e}from"../../utils/debounce.js";import{processAnalyticsData as s}from"./utils.js";import{AnalyticsEventCategory as n}from"./events/AbstractAnalyticsEvent.js";import i from"../../utils/Storage.js";import{PLATFORM as o,CHANNEL as r,LIBRARY_BUNDLE_TYPE as a,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:n}={}){try{var i;const h=this.storage.get(),l=function(t){if(!(null==t?void 0:t.timestamp))return!1;const e=Date.now()-9e5;return t.timestamp>e}(h),{applicationInfo:u,checkoutAttemptId:v}=null!==(i=s(this.analyticsData))&&void 0!==i?i:{},p=l?null==h?void 0:h.id:v,m=function(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{},n=Object.keys(s);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(s).filter(function(t){return Object.getOwnPropertyDescriptor(s,t).enumerable}))),n.forEach(function(e){d(t,e,s[e])})}return t}({version:c,buildType:a,channel:r.WEB,platform:o,locale:n,referrer:window.location.href,screenWidth:window.screen.width,checkoutStage:e||"checkout",level:this.enabled?"all":"initial"},u&&{applicationInfo:u},t&&{sessionId:t},p&&{checkoutAttemptId:p});this.checkoutAttemptId=await this.service.requestCheckoutAttemptId(m),this.dispatchPendingAnalytics(),this.storage.set({id:this.checkoutAttemptId,timestamp:l?h.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()===n.info?this.debouncedSendInfoEvents():this.debouncedSendEvents()}async sendEvents(){if(!this.checkoutAttemptId||!this.enabled)return;if(!this.eventsQueue.hasEvents)return;const t={channel:r.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:n,analyticsData:o}){d(this,"analyticsData",void 0),d(this,"service",void 0),d(this,"eventsQueue",void 0),d(this,"storage",new i("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!==n&&(this.enabled=n),o&&(this.analyticsData=o)}}export{l as default};
//# sourceMappingURL=Analytics.js.map