clvision
Version:
Customer Lifetime Value tracking and analytics library for CLVision
2 lines (1 loc) • 4.45 kB
JavaScript
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).CLVision={})}(this,(function(t){"use strict";function i(t,i,e,n){return new(e||(e=Promise))((function(o,s){function r(t){try{a(n.next(t))}catch(t){s(t)}}function c(t){try{a(n.throw(t))}catch(t){s(t)}}function a(t){var i;t.done?o(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,c)}a((n=n.apply(t,i||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class e{constructor(t){this.config=t}post(t,e){return i(this,void 0,void 0,(function*(){const i=yield fetch(`${this.config.baseURL}${t}`,{method:"POST",headers:this.config.headers,body:JSON.stringify(e)});if(!i.ok)throw new Error(`HTTP error! status: ${i.status}`);return i.json()}))}}class n{static generateUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{const i=16*Math.random()|0;return("x"===t?i:3&i|8).toString(16)}))}static reorderEmails(t,i){const e=t.filter((t=>t!==i));return[i,...e].slice(0,this.MAX_EMAILS)}static setCookie(t,i){const e={cookieId:t,emails:i},n=new Date;n.setDate(n.getDate()+this.COOKIE_EXPIRY_DAYS),document.cookie=`${this.COOKIE_NAME}=${JSON.stringify(e)};expires=${n.toUTCString()};path=/;SameSite=Strict`}static getCookieData(){const t=document.cookie.match(new RegExp(`(^| )${this.COOKIE_NAME}=([^;]+)`));if(!t)return null;try{return JSON.parse(t[2])}catch(t){return null}}static getCookieId(){const t=this.getCookieData();if(!t){const t=this.generateUUID();return this.setCookie(t,[]),t}return t.cookieId}static getEmails(){const t=this.getCookieData();return(null==t?void 0:t.emails)||[]}static addEmail(t){const i=this.getCookieData();if(!i){const i=this.generateUUID();return this.setCookie(i,[]),this.addEmail(t)}const e=i.emails||[],n=this.reorderEmails(e,t);this.setCookie(i.cookieId,n)}}n.COOKIE_NAME="clv_uid",n.COOKIE_EXPIRY_DAYS=365,n.MAX_EMAILS=5;class o{constructor(t){if(this.currentPageUrl=null,!t.organizationId)throw new Error("Organization ID is required");this.cookieId=n.getCookieId(),this.config={headers:{"X-Organization-Id":t.organizationId,"X-Cookie-Id":this.cookieId,"Content-Type":"application/json",Accept:"application/json"},baseURL:t.baseURL||o.DEFAULT_BASE_URL},this.http=new e(this.config),"undefined"!=typeof window&&this.initializeClient()}initializeClient(){if(!o.isInitialized&&(this.doInitialize(),"complete"!==document.readyState&&window.addEventListener("load",(()=>this.doInitialize())),"navigation"in window&&window.navigation.addEventListener("navigate",(()=>{this.trackPageView()})),"undefined"!=typeof window)){const t=history.pushState;history.pushState=(...i)=>{t.apply(history,i),this.trackPageView()},window.addEventListener("popstate",(()=>this.trackPageView()))}}doInitialize(){o.isInitialized||(this.cookieId=this.initializeCookieId(),this.config.headers["X-Cookie-Id"]=this.cookieId,this.trackPageView(),o.isInitialized=!0)}static getInstance(t){return o.instance||(o.instance=new o(t)),o.instance}initializeCookieId(){if("undefined"==typeof window)return"";let t=n.getCookieId();return t||(t=n.generateUUID(),n.setCookie(t,[])),t}initializePageViewTracking(){"undefined"!=typeof window&&(this.trackPageView(),window.addEventListener("popstate",(()=>this.trackPageView())))}trackPageView(){return i(this,void 0,void 0,(function*(){const t=window.location.href;if(t===this.currentPageUrl)return;this.currentPageUrl=t;const i={url:t,referrer:document.referrer||"",timestamp:Date.now(),cookieId:this.cookieId};try{yield this.http.post("/events/pageview",i)}catch(t){}}))}trackSignup(t){return i(this,void 0,void 0,(function*(){const i={email:t,cookieId:this.cookieId,timestamp:Date.now(),url:window.location.href,referrer:document.referrer||""};try{yield this.http.post("/events/signup",i),n.addEmail(t)}catch(t){}}))}trackSignin(t){return i(this,void 0,void 0,(function*(){const i={email:t,cookieId:this.cookieId,timestamp:Date.now(),url:window.location.href,referrer:document.referrer||""};try{yield this.http.post("/events/signin",i),n.addEmail(t)}catch(t){}}))}getConfig(){return this.config}}o.instance=null,o.isInitialized=!1,o.DEFAULT_BASE_URL="https://clvision.app/api/tracking";let s=null;"undefined"!=typeof window&&(window.CLVision=o),t.CLVision=o,t.initialize=function(t){return s||(s=new o(t)),s}}));//# sourceMappingURL=clvision.min.js.map