web-collect-events-narayana
Version:
An sdk for collecting events from web pages
1 lines • 4.5 kB
JavaScript
;var __awaiter=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(i,s){function n(e){try{c(o.next(e))}catch(e){s(e)}}function a(e){try{c(o.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,a)}c((o=o.apply(e,t||[])).next())}))},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const browserProfile_1=__importDefault(require("./helpers/browserProfile")),scrollState_1=__importDefault(require("./helpers/scrollState")),dateTimeIndex_1=__importDefault(require("./helpers/dateTimeIndex")),userId_1=__importDefault(require("./helpers/userId")),domNodeProfile_1=__importDefault(require("./helpers/domNodeProfile")),validNode_1=require("./helpers/validNode"),api_1=require("./helpers/api");class GTETWebSDK{constructor(){this.isPushNotificationScope=!1,this.isCustomEventScope=!1,this._data=[],this.checkScope(),this.initStorage(),console.log("GTETWebSDK started")}initStorage(){this._data=[]}setData(e){this._data.push(e)}getData(){return this._data}removeData(){this._data=[]}checkScope(){this.isPushNotificationScope="undefined"!=typeof window&&"undefined"!=typeof navigator,this.isCustomEventScope="undefined"!=typeof window}handleDocumentClick(e){const t=e.target;if(console.log(t),(0,validNode_1.isNodeValid)(t)){const t={eventType:"click",browserProfile:(0,browserProfile_1.default)(),scrollState:(0,scrollState_1.default)(),dateTimeIndex:(0,dateTimeIndex_1.default)(),userId:(0,userId_1.default)(),domNodeProfile:(0,domNodeProfile_1.default)(e)};this.setData(t)}}sendData(){const e=this.getData(),t=`${process.env.BASE_URL}/api/v1/stream`;if(e.length>0){if(!t)return void console.log("POST_URL environment variable is not defined.");(0,api_1.postData)(t,e).then((t=>{t.ok?(console.log("Data sent successfully:",e),this.removeData()):console.log("Failed to perform the POST request.")})).catch((e=>{console.log(e)}))}}selfAnalyze(e){this.isCustomEventScope?(document.addEventListener("click",this.handleDocumentClick.bind(this)),document.addEventListener("visibilitychange",(()=>{const t=new FormData;t.append("data",JSON.stringify(this.getData())),navigator.sendBeacon(`${e}/api/v1/stream`,t)})),setInterval(this.sendData.bind(this,e),6e4),console.log("Self analyze started")):console.log("This code is not running in a browser environment for selfAnalyze.")}customEvent(e,t){if(this.isCustomEventScope){const r={eventType:e,data:t};this.setData(r),this.sendData()}else console.log("This code is not running in a browser environment for customEvent.")}pushNotification(e,t,r){this.isPushNotificationScope?"serviceWorker"in navigator&&"PushManager"in window?r?this.registerServiceWorkerWithFile(r).then((()=>this.sendSubscriptionToServer(e,t))).catch((e=>console.error("Error:",e))):this.registerServiceWorkerWithoutFile().then((()=>this.sendSubscriptionToServer(e,t))).catch((e=>console.error("Error:",e))):console.log("Service Worker or PushManager is not supported in this browser."):console.log("This code is not running in a browser environment for pushNotification.")}registerServiceWorkerWithFile(e){return __awaiter(this,void 0,void 0,(function*(){try{const t=yield navigator.serviceWorker.register(`/${e}.js`,{scope:"/"});console.log("Service Worker registered:",t)}catch(e){throw console.error("Failed to register Service Worker:",e),e}}))}registerServiceWorkerWithoutFile(){return __awaiter(this,void 0,void 0,(function*(){try{const e=yield navigator.serviceWorker.register("https://push-notifications-narayana.s3.ap-south-1.amazonaws.com/service-worker.js",{scope:"/"});console.log("Service Worker registered:",e)}catch(e){throw console.error("Failed to register Service Worker:",e),e}}))}sendSubscriptionToServer(e,t){return __awaiter(this,void 0,void 0,(function*(){try{const r=yield navigator.serviceWorker.ready,o=yield r.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:this.urlBase64ToUint8Array(e)});yield fetch(t,{method:"POST",body:JSON.stringify(o),headers:{"content-type":"application/json"}}),console.log("Push Sent...",JSON.stringify(o))}catch(e){throw console.error("Failed to send subscription to the server:",e),e}}))}urlBase64ToUint8Array(e){const t=(e+"=".repeat((4-e.length%4)%4)).replace(/\-/g,"+").replace(/_/g,"/"),r=window.atob(t),o=new Uint8Array(r.length);for(let e=0;e<r.length;++e)o[e]=r.charCodeAt(e);return o}}exports.default=GTETWebSDK;