@traqio/web 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";(()=>{var vt=Object.create;var x=Object.defineProperty;var It=Object.getOwnPropertyDescriptor;var kt=Object.getOwnPropertyNames;var St=Object.getPrototypeOf,wt=Object.prototype.hasOwnProperty;var _t=(i,t,e)=>()=>{if(e)throw e[0];try{return i&&(t=i(i=0)),t}catch(s){throw e=[s],s}};var k=(i,t)=>()=>{try{return t||i((t={exports:{}}).exports,t),t.exports}catch(e){throw t=0,e}};var $=(i,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of kt(t))!wt.call(i,n)&&n!==e&&x(i,n,{get:()=>t[n],enumerable:!(s=It(t,n))||s.enumerable});return i};var Et=(i,t,e)=>(e=i!=null?vt(St(i)):{},$(t||!i||!i.__esModule?x(e,"default",{value:i,enumerable:!0}):e,i)),C=i=>$(x({},"__esModule",{value:!0}),i);var O=k(c=>{"use strict";Object.defineProperty(c,"__esModule",{value:!0});c.Skan=c.SKAN_STATE_KEY=c.CONVERSION_WINDOWS=void 0;c.windowForAge=H;c.shouldLockWindow=j;c.fineValueForRevenue=X;c.fineValueForEvents=z;c.coarseValueForRevenue=Z;c.CONVERSION_WINDOWS=[{window:0,startHours:0,endHours:48},{window:1,startHours:48,endHours:168},{window:2,startHours:168,endHours:840}];function H(i){let t=c.CONVERSION_WINDOWS.find(e=>i>=e.startHours&&i<e.endHours);return t?t.window:null}function j(i,t){let e=H(t);if(e==null||!i||!i.length)return!1;let s=i.find(n=>n.window===e);return!s||!s.lock?!1:s.lockAfterHours==null?!0:t>=s.lockAfterHours}var At=63,Tt=6;function X(i,t){let e=0;for(let s=0;s<Math.min(t.length,At+1);s++)i>=t[s]&&(e=s);return e}function z(i,t){let e=new Set(i),s=0;return t.slice(0,Tt).forEach((n,r)=>{e.has(n)&&(s|=1<<r)}),s}function Z(i,t){if(!t)return;let e,s=-1/0;for(let n of["low","medium","high"]){let r=t[n];r!=null&&i>=r&&r>=s&&(e=n,s=r)}return e}var J={revenue:0,events:[],lastFine:null};c.SKAN_STATE_KEY="traq_skan_state";var R=class{constructor(){this.mapping=null,this.state={...J},this.enabled=!1}async init(t){this.opts=t,this.enabled=!!t.native,this.enabled&&(await this.restore(),this.state.firstSeenAt==null&&(this.state.firstSeenAt=this.now(),await this.persist()),this.mapping=await this.fetchMapping(),this.mapping&&this.push())}addRevenue(t){!this.enabled||!(t>0)||!Number.isFinite(t)||(this.state.revenue+=t,this.persist(),this.push())}markEvent(t){!this.enabled||this.state.events.includes(t)||(this.state.events.push(t),this.persist(),this.push())}get activeMapping(){return this.mapping}push(){var n,r;let t=this.mapping;if(!t||!this.opts.native)return;let e,s;if(t.mode==="events"){if(!((n=t.eventBits)!=null&&n.length))return;e=z(this.state.events,t.eventBits)}else{if(!((r=t.revenueBuckets)!=null&&r.length))return;e=X(this.state.revenue,t.revenueBuckets),s=Z(this.state.revenue,t.coarseRevenue)}this.state.lastFine==null&&e===0||this.state.lastFine!=null&&e<=this.state.lastFine||(this.state.lastFine=e,this.persist(),this.opts.native.updateConversionValue(e,s,this.lockWindowNow()))}lockWindowNow(){var s;let t=this.state.firstSeenAt;if(t==null)return!1;let e=(this.now()-t)/36e5;return j((s=this.mapping)==null?void 0:s.postbackWindows,e)}now(){return this.opts.nowFn?this.opts.nowFn():Date.now()}async fetchMapping(){var e;let t=(e=this.opts.fetchFn)!=null?e:typeof fetch=="function"?fetch:void 0;if(!t)return null;try{let s=await t(`${this.opts.endpoint}/skan/mapping/${encodeURIComponent(this.opts.sdkKey)}`);if(!s.ok)return null;let n=await s.json();return n&&typeof n=="object"?n:null}catch{return null}}async restore(){var t;try{let e=await((t=this.opts.storage)==null?void 0:t.getItem(c.SKAN_STATE_KEY));e&&(this.state={...J,...JSON.parse(e)})}catch{}}async persist(){var t;try{await((t=this.opts.storage)==null?void 0:t.setItem(c.SKAN_STATE_KEY,JSON.stringify(this.state)))}catch{}}};c.Skan=R});var M=k(d=>{"use strict";Object.defineProperty(d,"__esModule",{value:!0});d.ADSERVICES_ATTEMPTS_KEY=d.ADSERVICES_SENT_KEY=void 0;d.reportAdServicesToken=Dt;d.ADSERVICES_SENT_KEY="traq_adservices_sent";d.ADSERVICES_ATTEMPTS_KEY="traq_adservices_attempts";var bt=5;async function Dt(i){var s,n;if(!((s=i.native)!=null&&s.getAttributionToken))return"unsupported";if(await G(i,d.ADSERVICES_SENT_KEY))return"already-sent";let t=null;try{t=await i.native.getAttributionToken()}catch{}if(!t||!t.trim()){let r=Number(await G(i,d.ADSERVICES_ATTEMPTS_KEY))||0;return await N(i,d.ADSERVICES_ATTEMPTS_KEY,String(r+1)),r+1>=bt&&await N(i,d.ADSERVICES_SENT_KEY,"1"),"no-token"}let e=(n=i.fetchFn)!=null?n:typeof fetch=="function"?fetch:void 0;if(!e)return"post-failed";try{if(!(await e(`${i.endpoint}/ingest/adservices`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:i.sdkKey,token:t.trim(),deviceId:i.deviceId,appUserId:i.userId})})).ok)return"post-failed"}catch{return"post-failed"}return await N(i,d.ADSERVICES_SENT_KEY,"1"),"sent"}async function G(i,t){var e,s;try{return(s=await((e=i.storage)==null?void 0:e.getItem(t)))!=null?s:null}catch{return null}}async function N(i,t,e){var s;try{await((s=i.storage)==null?void 0:s.setItem(t,e))}catch{}}});var K=k(S=>{"use strict";Object.defineProperty(S,"__esModule",{value:!0});S.isTrackingUrl=Ct;S.toDeepLink=Rt;var xt=/^\/c\/[A-Za-z0-9_-]{1,50}\/?$/;function Ct(i){var e;let t=/^https?:\/\/[^/?#]+(\/[^?#]*)?/i.exec(i);return!!(t&&xt.test((e=t[1])!=null?e:""))}function Rt(i,t,e){return!(i!=null&&i.matched)||!i.via?null:{deepLink:i.deepLink,code:i.code,deferred:t,via:i.via,...e?{url:e}:{}}}});var q={};var w=_t(()=>{"use strict"});var lt=k(o=>{"use strict";Object.defineProperty(o,"__esModule",{value:!0});o.Traqio=o.Client=o.isTrackingUrl=o.reportAdServicesToken=o.Skan=o.SDK_VERSION=void 0;o.normalizeAdvertisingId=dt;o.fromMaxAd=ht;var Ot=O(),Nt=M(),L=K();o.SDK_VERSION="0.1.2";var Mt=O();Object.defineProperty(o,"Skan",{enumerable:!0,get:function(){return Mt.Skan}});var Kt=M();Object.defineProperty(o,"reportAdServicesToken",{enumerable:!0,get:function(){return Kt.reportAdServicesToken}});var qt=K();Object.defineProperty(o,"isTrackingUrl",{enumerable:!0,get:function(){return qt.isTrackingUrl}});var F="traq_install_sent",tt="traq_install_attempts",et="traq_referrer",st="traq_queue",it="traq_ad_queue",nt="traq_device_id",rt="traq_last_session",ot="traq_erased",_="traq_deferred_link",at="traq_consent",Lt=20,Ft=new Set([400,413,422]),ct=500,Pt=100,ut=1e3,P=1e3,Ut=300*1e3,Vt=3e3,Qt=3,Yt=1800*1e3,Bt={exact:"precise",publisher_defined:"publisher_provided",estimated:"estimated"},Wt={BANNER:"banner",LEADER:"banner",MREC:"banner",INTER:"interstitial",INTERSTITIAL:"interstitial",REWARDED:"rewarded",REWARDED_INTER:"rewarded_interstitial",NATIVE:"native",APPOPEN:"app_open",APP_OPEN:"app_open"},$t="00000000-0000-0000-0000-000000000000";function dt(i){if(typeof i!="string")return;let t=i.trim();if(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(t))return t===$t?void 0:t}function ht(i){var t,e,s;if(typeof i.revenue=="number"&&Number.isFinite(i.revenue)&&i.revenue>=0)return{valueMicros:Math.round(i.revenue*1e6),currency:"USD",precision:(e=Bt[(t=i.revenuePrecision)!=null?t:""])!=null?e:"unknown",adUnitId:i.adUnitId,adFormat:Wt[((s=i.format)!=null?s:"").toUpperCase()],adSourceName:i.networkName,ts:i.ts}}function y(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,i=>{let t=Math.random()*16|0;return(i==="x"?t:t&3|8).toString(16)})}function Jt(){try{return Intl.DateTimeFormat().resolvedOptions().locale}catch{return}}function Ht(){var i;try{let t=(w(),C(q)),e=(i=t==null?void 0:t.default)!=null?i:t;if(typeof(e==null?void 0:e.getItem)=="function"&&typeof(e==null?void 0:e.setItem)=="function")return{getItem:s=>e.getItem(s),setItem:(s,n)=>e.setItem(s,n)}}catch{}}var E=class{constructor(){this.queue=[],this.adQueue=[],this.adBatchId=null,this.context={sdkVersion:o.SDK_VERSION},this.referrer=null,this.started=!1,this.flushing=!1,this.flushTimer=null,this.retryMs=P,this.skan=new Ot.Skan,this.erased=!1,this.deepLinkListeners=new Set,this.undelivered=[],this.deferred=null,this.consent=null}async init(t){var e;if(!this.started&&(this.started=!0,this.opts={collectContext:!0,...t,storage:(e=t.storage)!=null?e:Ht()},this.opts.storage||console.warn("[traqio] no storage available \u2014 pass `storage` to init() or install @react-native-async-storage/async-storage, otherwise the install is re-sent at every launch."),await this.restoreConsent(t.consent),await this.restoreQueue(),await this.restoreAdQueue(),await this.buildContext(),!await this.restoreErased())){if(!this.isWeb){let s=!!this.opts.storage&&!await this.opts.storage.getItem(F).catch(()=>"1");await this.sendInstallOnce(),this.opts.deepLinking!==!1&&(this.deferred=this.resolveDeferredOnce(s),this.wireDeepLinks())}await this.maybeStartSession(!this.isWeb),await this.initSkan(),await this.initAppleAds(),this.wireLifecycle(),(this.queue.length||this.adQueue.length)&&this.scheduleFlush(0)}}track(t,e){var n,r;if(!this.started||this.erased)return;let s=((n=e==null?void 0:e.transactionId)==null?void 0:n.trim())||void 0;this.enqueue({name:t,eventId:y(),value:e==null?void 0:e.value,currency:e==null?void 0:e.currency,...s?{transactionId:s,store:(r=e==null?void 0:e.store)!=null?r:this.defaultStore()}:{}}),this.skan.markEvent(t),e!=null&&e.value&&this.skan.addRevenue(e.value)}defaultStore(){var e;let t=(e=this.opts.native)==null?void 0:e.platform;return t==="ios"?"app_store":t==="android"?"play_store":void 0}trackAdRevenue(t){this.queueAdRevenue(t,"admob")}trackMaxAdRevenue(t){let e=ht(t);e&&this.queueAdRevenue(e,"applovin_max")}queueAdRevenue(t,e){var s;!this.started||this.erased||!(t.valueMicros>=0)||!t.currency||(this.adQueue.push({...t,source:e,impressionId:y(),ts:(s=t.ts)!=null?s:Math.floor(Date.now()/1e3)}),this.skan.addRevenue(t.valueMicros/1e6),this.adQueue.length>ut&&this.adQueue.splice(0,this.adQueue.length-ut),this.persistAdQueue(),this.scheduleFlush())}getInstallReferrer(){return this.started?this.readReferrer():Promise.resolve({status:"unavailable",retryable:!1})}onDeepLink(t){this.deepLinkListeners.add(t);let e=this.undelivered.splice(0);for(let s of e)t(s);return()=>{this.deepLinkListeners.delete(t)}}async handleUrl(t){if(!this.started||this.erased||this.opts.deepLinking===!1||!(0,L.isTrackingUrl)(t))return null;let e=await this.postJson({url:t},"/deeplink/open"),s=(0,L.toDeepLink)(e,!1,t);return s&&this.emitDeepLink(s),s}getDeferredDeepLink(){var t;return(t=this.deferred)!=null?t:Promise.resolve(null)}async forgetMe(){return this.started?this.erased?!0:await this.post({deviceId:this.context.deviceId,appUserId:this.opts.userId},"/privacy/forget")?(await this.markErased(),!0):!1:!1}async setConsent(t){var e,s;this.consent=t?"granted":"denied",await((s=(e=this.opts)==null?void 0:e.storage)==null?void 0:s.setItem(at,this.consent).catch(()=>{}))}get forgotten(){return this.erased}setUserId(t){this.opts&&(this.opts.userId=t)}flush(){this.erased||(this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.drain())}appOpened(){!this.started||this.erased||this.maybeStartSession()}get skanMapping(){return this.skan.activeMapping}async initAppleAds(){if(this.opts.appleAds===!1)return;let t=this.opts.native;(t==null?void 0:t.platform)!=="ios"||!t.getAttributionToken||await(0,Nt.reportAdServicesToken)({endpoint:this.opts.endpoint,sdkKey:this.opts.sdkKey,deviceId:this.context.deviceId,userId:this.opts.userId,storage:this.opts.storage,native:{getAttributionToken:()=>t.getAttributionToken()}})}async initSkan(){if(this.opts.skan===!1)return;let t=this.opts.native;(t==null?void 0:t.platform)!=="ios"||!t.updateConversionValue||await this.skan.init({endpoint:this.opts.endpoint,sdkKey:this.opts.sdkKey,storage:this.opts.storage,native:{updateConversionValue:(e,s,n)=>{var r;return(r=t.updateConversionValue)==null?void 0:r.call(t,e,s,n)}}})}async resolveDeferredOnce(t){var a,l;let e=this.opts.storage;try{let g=await(e==null?void 0:e.getItem(_));if(g)return JSON.parse(g)}catch{}if(!t&&!await(e==null?void 0:e.getItem(`${_}_pending`).catch(()=>null)))return null;let s=(l=(a=this.opts.native)==null?void 0:a.platform)!=null?l:"android",n=s==="android"?await this.readReferrer():null;await(e==null?void 0:e.setItem(`${_}_pending`,"1").catch(()=>{}));let r=await this.postJson({platform:s,installReferrer:n==null?void 0:n.referrer},"/deeplink/deferred");if(!r)return null;let u=(0,L.toDeepLink)(r,!0);try{await(e==null?void 0:e.setItem(_,JSON.stringify(u)))}catch{}return u&&this.emitDeepLink(u),u}emitDeepLink(t){if(!this.deepLinkListeners.size){this.undelivered.push(t);return}for(let e of this.deepLinkListeners)try{e(t)}catch{}}wireDeepLinks(){try{let{Linking:t}=(w(),C(q));if(!t)return;t.getInitialURL().then(e=>e&&this.handleUrl(e)).catch(()=>{}),t.addEventListener("url",({url:e})=>{this.handleUrl(e)})}catch{}}get isWeb(){var t;return((t=this.opts)==null?void 0:t.platform)==="web"}wireLifecycle(){let t={background:()=>this.flush(),foreground:()=>this.appOpened()};if(this.opts.lifecycle){this.opts.lifecycle(t);return}try{let{AppState:e}=(w(),C(q));e.addEventListener("change",s=>{if(s!=="active"){this.scheduleFlush(0);return}this.maybeStartSession()})}catch{}}async restoreErased(){var t;try{this.erased=await((t=this.opts.storage)==null?void 0:t.getItem(ot))==="1"}catch{this.erased=!1}return this.erased}async markErased(){var t;this.erased=!0,this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.queue=[],this.adQueue=[],this.adBatchId=null,await this.persistQueue(),await this.persistAdQueue();try{await((t=this.opts.storage)==null?void 0:t.setItem(ot,"1"))}catch{}}enqueue(t){this.queue.push(t),this.queue.length>ct&&this.queue.splice(0,this.queue.length-ct),this.persistQueue(),this.scheduleFlush()}async restoreQueue(){var t;try{let e=await((t=this.opts.storage)==null?void 0:t.getItem(st));e&&(this.queue=JSON.parse(e))}catch{}}async persistQueue(){var t;try{await((t=this.opts.storage)==null?void 0:t.setItem(st,JSON.stringify(this.queue)))}catch{}}async restoreAdQueue(){var t,e,s;try{let n=await((t=this.opts.storage)==null?void 0:t.getItem(it));if(!n)return;let r=JSON.parse(n);this.adQueue=(e=r.items)!=null?e:[],this.adBatchId=(s=r.batchId)!=null?s:null}catch{}}async persistAdQueue(){var t;try{let e={batchId:this.adBatchId,items:this.adQueue};await((t=this.opts.storage)==null?void 0:t.setItem(it,JSON.stringify(e)))}catch{}}async maybeStartSession(t=!1){var r,u,a;if(this.erased)return;let e=Date.now();if(!t){let l=Number(await((r=this.opts.storage)==null?void 0:r.getItem(rt).catch(()=>null)))||0;if(l&&e-l<Yt)return}let s=(u=this.context.deviceId)!=null?u:await this.getOrCreateDeviceId();if(await this.post({deviceId:s,appUserId:this.opts.userId,sessionId:y(),ts:Math.floor(e/1e3),context:this.context},"/ingest/session"))try{await((a=this.opts.storage)==null?void 0:a.setItem(rt,String(e)))}catch{}}async buildContext(){var e,s,n;if(!this.opts.collectContext){this.context={sdkVersion:o.SDK_VERSION};return}let t={};try{t=(n=await((s=(e=this.opts.native)==null?void 0:e.getContext)==null?void 0:s.call(e)))!=null?n:{}}catch{}this.context={deviceId:await this.getOrCreateDeviceId(),deviceModel:t.deviceModel,osVersion:t.osVersion,appVersion:this.opts.appVersion,locale:Jt(),sdkVersion:o.SDK_VERSION}}async getOrCreateDeviceId(){var e,s;try{let n=await((e=this.opts.storage)==null?void 0:e.getItem(nt));if(n)return n}catch{}let t=y();try{await((s=this.opts.storage)==null?void 0:s.setItem(nt,t))}catch{}return t}async readReferrer(){var s,n;if(this.referrer)return this.referrer;try{let r=await((s=this.opts.storage)==null?void 0:s.getItem(et));if(r)return this.referrer=JSON.parse(r)}catch{}let t=this.opts.native;if((t==null?void 0:t.platform)!=="android")return{status:t?"not_supported_platform":"unavailable",retryable:!1};let e=await t.getInstallReferrer().catch(()=>({status:"error",retryable:!0}));if(!e.retryable){this.referrer=e;try{await((n=this.opts.storage)==null?void 0:n.setItem(et,JSON.stringify(e)))}catch{}}return e}async readAdvertisingId(){var t;if(!(!this.opts.collectAdvertisingId||!((t=this.opts.native)!=null&&t.getAdvertisingId)))try{return dt(await this.opts.native.getAdvertisingId())}catch{return}}async sendInstallOnce(){var r,u;let t=this.opts.storage;if(t&&await t.getItem(F))return;let e=(u=(r=this.opts.native)==null?void 0:r.platform)!=null?u:"android",s=await this.readReferrer();if(s.retryable&&t){let a=Number(await t.getItem(tt))||0;if(a+1<Qt){await t.setItem(tt,String(a+1));return}}await this.post({install:{platform:e,installReferrer:s.referrer,appUserId:this.opts.userId,advertisingId:await this.readAdvertisingId(),context:this.context}})&&await(t==null?void 0:t.setItem(F,"1"))}scheduleFlush(t=Vt){this.flushTimer||(this.flushTimer=setTimeout(()=>{this.flushTimer=null,this.drain()},t))}async drain(){var t;if(!this.erased&&!(this.flushing||!this.queue.length&&!this.adQueue.length)){this.flushing=!0;try{for(;this.queue.length;){let e=this.queue.slice(0,Lt),s=await this.send({events:e,appUserId:this.opts.userId,deviceId:this.context.deviceId});if(s==="retry")return this.backOff();s==="refused"&&console.warn(`[traqio] the server refused ${e.length} event(s) as malformed \u2014 dropped, not retried`),this.queue.splice(0,e.length),await this.persistQueue(),this.retryMs=P}for(;this.adQueue.length;){let e=(t=this.adQueue[0].source)!=null?t:"admob",s=this.adQueue.findIndex(u=>{var a;return((a=u.source)!=null?a:"admob")!==e}),n=this.adQueue.slice(0,Math.min(Pt,s===-1?this.adQueue.length:s));if(this.adBatchId||(this.adBatchId=y(),await this.persistAdQueue()),!await this.post({batchId:this.adBatchId,source:e,environment:this.opts.testAds?"TEST":"PRODUCTION",deviceId:this.context.deviceId,appUserId:this.opts.userId,impressions:n.map(({source:u,...a})=>a)},"/ingest/ad-revenue"))return this.backOff();this.adQueue.splice(0,n.length),this.adBatchId=null,await this.persistAdQueue(),this.retryMs=P}}finally{this.flushing=!1}}}backOff(){this.retryMs=Math.min(this.retryMs*2,Ut),this.scheduleFlush(this.retryMs)}async postJson(t,e){try{let s=await fetch(`${this.opts.endpoint}${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:this.opts.sdkKey,...t}),...this.keepalive()});return s.ok?await s.json():null}catch{return null}}async restoreConsent(t){var s;if(t!==void 0)return this.setConsent(t);if(this.consent)return this.setConsent(this.consent==="granted");let e=await((s=this.opts.storage)==null?void 0:s.getItem(at).catch(()=>null));(e==="granted"||e==="denied")&&(this.consent=e)}keepalive(){return this.isWeb?{keepalive:!0}:{}}async post(t,e="/ingest"){return await this.send(t,e)==="ok"}async send(t,e="/ingest"){let s=this.consent&&(e==="/ingest"||e==="/ingest/session")?{consent:this.consent}:{};try{let n=await fetch(`${this.opts.endpoint}${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:this.opts.sdkKey,...t,...s}),...this.keepalive()});return n.ok?"ok":Ft.has(n.status)?"refused":"retry"}catch{return"retry"}}};o.Client=E;o.Traqio=new E});var gt=Et(lt(),1);var V="traq_";function Q(){return typeof document=="undefined"?void 0:document}function v(i){let t=Q();if(!t)return null;for(let e of t.cookie?t.cookie.split(";"):[]){let s=e.indexOf("=");if(!(s<0)&&e.slice(0,s).trim()===i)try{return decodeURIComponent(e.slice(s+1).trim())}catch{return null}}return null}function U(i,t,e,s){let n=typeof location!="undefined"&&location.protocol==="https:"?"; Secure":"",r=s?`; Domain=${s}`:"";return`${i}=${encodeURIComponent(t)}; Max-Age=${Math.floor(e)}; Path=/${r}; SameSite=Lax${n}`}function ft(i){let t=Q();if(!t||!i.includes(".")||/^[\d.]+$/.test(i)||i.includes(":"))return null;let e=i.split("."),s=`${V}probe`;for(let n=e.length-2;n>=0;n--){let r=e.slice(n).join(".");if(t.cookie=U(s,"1",10,r),v(s)==="1")return t.cookie=U(s,"",0,r),r}return null}function p(i,t,e,s){let n=Q();if(n)try{n.cookie=U(i,t,e,s)}catch{}}function I(i,t){p(i,"",0,t)}var b="traq_device_id",Y=new Set([b]),jt=395*24*60*60;function A(){try{return typeof localStorage=="undefined"?void 0:localStorage}catch{return}}var T=class{constructor(t,e,s){this.persistent=t;this.cookieDomain=e;this.cookies=s;this.memory=new Map}get isPersistent(){return this.persistent}async getItem(t){return this.getSync(t)}async setItem(t,e){this.setSync(t,e)}getSync(t){var s,n,r;if(this.memory.has(t))return(s=this.memory.get(t))!=null?s:null;if(!this.persistent)return null;let e=null;try{e=(r=(n=A())==null?void 0:n.getItem(t))!=null?r:null}catch{}return e===null&&this.cookies&&Y.has(t)&&(e=v(t)),e}setSync(t,e){if(!this.persistent){this.memory.set(t,e);return}let s=!1;try{let n=A();n&&(n.setItem(t,e),s=!0)}catch{}s||this.memory.set(t,e),this.cookies&&Y.has(t)&&p(t,e,jt,this.cookieDomain())}removeSync(t){var e;this.memory.delete(t);try{(e=A())==null||e.removeItem(t)}catch{}this.cookies&&I(t,this.cookieDomain())}enable(){if(this.persistent)return;this.persistent=!0;let t=[...this.memory];this.memory.clear();for(let[e,s]of t)this.setSync(e,s)}disable(){if(!this.persistent)return;let t=A(),e=[];try{for(let s=0;t&&s<t.length;s++){let n=t.key(s);n&&n.startsWith(V)&&e.push(n)}}catch{}for(let s of Y)e.includes(s)||e.push(s);for(let s of e){let n=this.getSync(s);n!==null&&this.memory.set(s,n)}this.persistent=!1;for(let s of e){try{t==null||t.removeItem(s)}catch{}this.cookies&&I(s,this.cookieDomain())}}};var Xt="traq_click",f="traq_click",pt=/^[A-Za-z0-9_-]{8,64}$/;function zt(i){try{let t=new URL(i).searchParams.get(Xt);return t&&pt.test(t)?t:null}catch{return null}}var D=class{constructor(t,e,s,n){this.store=t;this.maxAgeMs=e;this.cookieDomain=s;this.cookies=n}capture(t,e=Date.now()){let s=zt(t);return s&&this.write(s,e),s}get(t=Date.now()){let e=this.store.getSync(f);if(e)try{let s=JSON.parse(e);if(s&&typeof s.id=="string"&&s.exp>t)return s.id}catch{}if(this.cookies&&this.store.isPersistent){let s=v(f);if(s&&pt.test(s))return s}return null}clear(){this.store.removeSync(f),this.cookies&&I(f,this.cookieDomain())}persistCookie(t=Date.now()){let e=this.store.getSync(f);if(!(!e||!this.cookies||!this.store.isPersistent))try{let s=JSON.parse(e);s.exp>t&&p(f,s.id,(s.exp-t)/1e3,this.cookieDomain())}catch{}}write(t,e){let s={id:t,exp:e+this.maxAgeMs};this.store.setSync(f,JSON.stringify(s)),this.cookies&&this.store.isPersistent&&p(f,t,this.maxAgeMs/1e3,this.cookieDomain())}};var Zt="https://api.traqio.app",Gt=30,te=1440*60*1e3;function ee(){return typeof window!="undefined"&&typeof document!="undefined"}function se(){try{return typeof crypto!="undefined"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():null}catch{return null}}var B=class{constructor(){this.core=new gt.Client;this.store=null;this.trackIds=null;this.waitForConsent=!1;this.started=!1}async init(t){var a,l,g;if(this.started||!ee())return;this.started=!0,this.waitForConsent=t.waitForConsent===!0;let e=t.cookies!==!1,s,n=()=>s===void 0?s=ft(location.hostname):s,r=(a=t.consent)!=null?a:this.earlyConsent,u=!this.waitForConsent||r===!0;if(this.store=new T(u,n,e),this.trackIds=new D(this.store,((l=t.trackIdDays)!=null?l:Gt)*te,n,e),this.trackIds.capture(location.href),!this.store.getSync(b)){let m=se();m&&this.store.setSync(b,m)}await this.core.init({sdkKey:t.sdkKey,endpoint:((g=t.endpoint)!=null?g:Zt).replace(/\/+$/,""),storage:this.store,platform:"web",userId:t.userId,consent:r,lifecycle:({background:m,foreground:yt})=>{document.addEventListener("visibilitychange",()=>document.visibilityState==="hidden"?m():yt()),window.addEventListener("pagehide",m)}})}track(t,e){this.core.track(t,e)}setUserId(t){this.core.setUserId(t)}async setConsent(t){var e;this.started||(this.earlyConsent=t),this.store&&this.waitForConsent&&(t?(this.store.enable(),(e=this.trackIds)==null||e.persistCookie()):this.store.disable()),await this.core.setConsent(t)}getTrackId(){var t,e;return(e=(t=this.trackIds)==null?void 0:t.get())!=null?e:null}async forgetMe(){var e;let t=await this.core.forgetMe();return t&&((e=this.trackIds)==null||e.clear()),t}flush(){this.core.flush()}get forgotten(){return this.core.forgotten}},W=new B;window.Traqio=W;var h=document.currentScript,mt=h==null?void 0:h.dataset.sdkKey;mt&&W.init({sdkKey:mt,endpoint:(h==null?void 0:h.dataset.endpoint)||void 0,waitForConsent:(h==null?void 0:h.dataset.waitForConsent)==="true"});})();
1
+ "use strict";(()=>{var _e=Object.create;var O=Object.defineProperty;var Ee=Object.getOwnPropertyDescriptor;var Ae=Object.getOwnPropertyNames;var Te=Object.getPrototypeOf,De=Object.prototype.hasOwnProperty;var be=(s,e,t)=>()=>{if(t)throw t[0];try{return s&&(e=s(s=0)),e}catch(i){throw t=[i],i}};var _=(s,e)=>()=>{try{return e||s((e={exports:{}}).exports,e),e.exports}catch(t){throw e=0,t}};var $=(s,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Ae(e))!De.call(s,n)&&n!==t&&O(s,n,{get:()=>e[n],enumerable:!(i=Ee(e,n))||i.enumerable});return s};var xe=(s,e,t)=>(t=s!=null?_e(Te(s)):{},$(e||!s||!s.__esModule?O(t,"default",{value:s,enumerable:!0}):t,s)),C=s=>$(O({},"__esModule",{value:!0}),s);var N=_(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.Skan=u.SKAN_STATE_KEY=u.CONVERSION_WINDOWS=void 0;u.windowForAge=H;u.shouldLockWindow=X;u.fineValueForRevenue=z;u.fineValueForEvents=Z;u.coarseValueForRevenue=G;u.CONVERSION_WINDOWS=[{window:0,startHours:0,endHours:48},{window:1,startHours:48,endHours:168},{window:2,startHours:168,endHours:840}];function H(s){let e=u.CONVERSION_WINDOWS.find(t=>s>=t.startHours&&s<t.endHours);return e?e.window:null}function X(s,e){let t=H(e);if(t==null||!s||!s.length)return!1;let i=s.find(n=>n.window===t);return!i||!i.lock?!1:i.lockAfterHours==null?!0:e>=i.lockAfterHours}var Oe=63,Ce=6;function z(s,e){let t=0;for(let i=0;i<Math.min(e.length,Oe+1);i++)s>=e[i]&&(t=i);return t}function Z(s,e){let t=new Set(s),i=0;return e.slice(0,Ce).forEach((n,r)=>{t.has(n)&&(i|=1<<r)}),i}function G(s,e){if(!e)return;let t,i=-1/0;for(let n of["low","medium","high"]){let r=e[n];r!=null&&s>=r&&r>=i&&(t=n,i=r)}return t}var j={revenue:0,events:[],lastFine:null};u.SKAN_STATE_KEY="traq_skan_state";var R=class{constructor(){this.mapping=null,this.state={...j},this.enabled=!1}async init(e){this.opts=e,this.enabled=!!e.native,this.enabled&&(await this.restore(),this.state.firstSeenAt==null&&(this.state.firstSeenAt=this.now(),await this.persist()),this.mapping=await this.fetchMapping(),this.mapping&&this.push())}addRevenue(e){!this.enabled||!(e>0)||!Number.isFinite(e)||(this.state.revenue+=e,this.persist(),this.push())}markEvent(e){!this.enabled||this.state.events.includes(e)||(this.state.events.push(e),this.persist(),this.push())}get activeMapping(){return this.mapping}push(){var n,r;let e=this.mapping;if(!e||!this.opts.native)return;let t,i;if(e.mode==="events"){if(!((n=e.eventBits)!=null&&n.length))return;t=Z(this.state.events,e.eventBits)}else{if(!((r=e.revenueBuckets)!=null&&r.length))return;t=z(this.state.revenue,e.revenueBuckets),i=G(this.state.revenue,e.coarseRevenue)}this.state.lastFine==null&&t===0||this.state.lastFine!=null&&t<=this.state.lastFine||(this.state.lastFine=t,this.persist(),this.opts.native.updateConversionValue(t,i,this.lockWindowNow()))}lockWindowNow(){var i;let e=this.state.firstSeenAt;if(e==null)return!1;let t=(this.now()-e)/36e5;return X((i=this.mapping)==null?void 0:i.postbackWindows,t)}now(){return this.opts.nowFn?this.opts.nowFn():Date.now()}async fetchMapping(){var t;let e=(t=this.opts.fetchFn)!=null?t:typeof fetch=="function"?fetch:void 0;if(!e)return null;try{let i=await e(`${this.opts.endpoint}/skan/mapping/${encodeURIComponent(this.opts.sdkKey)}`);if(!i.ok)return null;let n=await i.json();return n&&typeof n=="object"?n:null}catch{return null}}async restore(){var e;try{let t=await((e=this.opts.storage)==null?void 0:e.getItem(u.SKAN_STATE_KEY));t&&(this.state={...j,...JSON.parse(t)})}catch{}}async persist(){var e;try{await((e=this.opts.storage)==null?void 0:e.setItem(u.SKAN_STATE_KEY,JSON.stringify(this.state)))}catch{}}};u.Skan=R});var L=_(d=>{"use strict";Object.defineProperty(d,"__esModule",{value:!0});d.ADSERVICES_ATTEMPTS_KEY=d.ADSERVICES_SENT_KEY=void 0;d.reportAdServicesToken=Ne;d.ADSERVICES_SENT_KEY="traq_adservices_sent";d.ADSERVICES_ATTEMPTS_KEY="traq_adservices_attempts";var Re=5;async function Ne(s){var i,n;if(!((i=s.native)!=null&&i.getAttributionToken))return"unsupported";if(await ee(s,d.ADSERVICES_SENT_KEY))return"already-sent";let e=null;try{e=await s.native.getAttributionToken()}catch{}if(!e||!e.trim()){let r=Number(await ee(s,d.ADSERVICES_ATTEMPTS_KEY))||0;return await M(s,d.ADSERVICES_ATTEMPTS_KEY,String(r+1)),r+1>=Re&&await M(s,d.ADSERVICES_SENT_KEY,"1"),"no-token"}let t=(n=s.fetchFn)!=null?n:typeof fetch=="function"?fetch:void 0;if(!t)return"post-failed";try{if(!(await t(`${s.endpoint}/ingest/adservices`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:s.sdkKey,token:e.trim(),deviceId:s.deviceId,appUserId:s.userId})})).ok)return"post-failed"}catch{return"post-failed"}return await M(s,d.ADSERVICES_SENT_KEY,"1"),"sent"}async function ee(s,e){var t,i;try{return(i=await((t=s.storage)==null?void 0:t.getItem(e)))!=null?i:null}catch{return null}}async function M(s,e,t){var i;try{await((i=s.storage)==null?void 0:i.setItem(e,t))}catch{}}});var K=_(y=>{"use strict";Object.defineProperty(y,"__esModule",{value:!0});y.isTrackingUrl=Le;y.isOpenUrl=Ke;y.toDeepLink=se;y.upgradeStoredDeepLink=qe;var Me=/^\/c\/[A-Za-z0-9_-]{1,50}\/?$/,te=/^\/o\/[0-9a-fA-F-]{36}\/?$/;function ie(s){var t;let e=/^https?:\/\/[^/?#]+(\/[^?#]*)?/i.exec(s);return e?(t=e[1])!=null?t:"":null}function Le(s){let e=ie(s);return e!==null&&(Me.test(e)||te.test(e))}function Ke(s){let e=ie(s);return e!==null&&te.test(e)}function se(s,e,t){var r,o,a;if(!(s!=null&&s.matched)||!s.via)return null;let i=(r=s.matchType)!=null?r:s.via==="ip_match"?"probabilistic":s.via,n=(o=s.payload)!=null?o:{deepLink:s.deepLink,value:null,params:{}};return{payload:n,matchType:i,confidence:i==="probabilistic"?(a=s.confidence)!=null?a:"low":null,deferred:e,code:s.code,deepLink:n.deepLink,via:s.via,...t?{url:t}:{}}}function qe(s){var t,i,n;if(!s||typeof s!="object")return null;let e=s;return e.payload&&e.matchType?e:e.via?se({matched:!0,via:e.via,code:(t=e.code)!=null?t:null,deepLink:(i=e.deepLink)!=null?i:null},(n=e.deferred)!=null?n:!0,e.url):null}});var q={};var E=be(()=>{"use strict"});var ye=_(c=>{"use strict";Object.defineProperty(c,"__esModule",{value:!0});c.Traqio=c.Client=c.isTrackingUrl=c.isOpenUrl=c.reportAdServicesToken=c.Skan=c.SDK_VERSION=void 0;c.normalizeAdvertisingId=ge;c.fromMaxAd=me;var Fe=N(),Pe=L(),p=K();c.SDK_VERSION="0.2.0";var Ue=N();Object.defineProperty(c,"Skan",{enumerable:!0,get:function(){return Ue.Skan}});var Ve=L();Object.defineProperty(c,"reportAdServicesToken",{enumerable:!0,get:function(){return Ve.reportAdServicesToken}});var pe=K();Object.defineProperty(c,"isOpenUrl",{enumerable:!0,get:function(){return pe.isOpenUrl}});Object.defineProperty(c,"isTrackingUrl",{enumerable:!0,get:function(){return pe.isTrackingUrl}});var F="traq_install_sent",ne="traq_install_attempts",re="traq_referrer",oe="traq_queue",ae="traq_ad_queue",ce="traq_device_id",ue="traq_last_session",de="traq_erased",m="traq_deferred_link",P="traq_deferred_consent_retry_until",Ye=60*6e4,he="traq_consent",Qe=20,We=new Set([400,413,422]),le=500,Be=100,fe=1e3,U=1e3,Je=300*1e3,$e=3e3,je=3,He=1800*1e3,Xe={exact:"precise",publisher_defined:"publisher_provided",estimated:"estimated"},ze={BANNER:"banner",LEADER:"banner",MREC:"banner",INTER:"interstitial",INTERSTITIAL:"interstitial",REWARDED:"rewarded",REWARDED_INTER:"rewarded_interstitial",NATIVE:"native",APPOPEN:"app_open",APP_OPEN:"app_open"},Ze="00000000-0000-0000-0000-000000000000";function ge(s){if(typeof s!="string")return;let e=s.trim();if(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(e))return e===Ze?void 0:e}function me(s){var e,t,i;if(typeof s.revenue=="number"&&Number.isFinite(s.revenue)&&s.revenue>=0)return{valueMicros:Math.round(s.revenue*1e6),currency:"USD",precision:(t=Xe[(e=s.revenuePrecision)!=null?e:""])!=null?t:"unknown",adUnitId:s.adUnitId,adFormat:ze[((i=s.format)!=null?i:"").toUpperCase()],adSourceName:s.networkName,ts:s.ts}}function I(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,s=>{let e=Math.random()*16|0;return(s==="x"?e:e&3|8).toString(16)})}function Ge(){try{return Intl.DateTimeFormat().resolvedOptions().locale}catch{return}}function et(){var s;try{let e=(E(),C(q)),t=(s=e==null?void 0:e.default)!=null?s:e;if(typeof(t==null?void 0:t.getItem)=="function"&&typeof(t==null?void 0:t.setItem)=="function")return{getItem:i=>t.getItem(i),setItem:(i,n)=>t.setItem(i,n)}}catch{}}var A=class{constructor(){this.queue=[],this.adQueue=[],this.adBatchId=null,this.context={sdkVersion:c.SDK_VERSION},this.referrer=null,this.started=!1,this.flushing=!1,this.flushTimer=null,this.retryMs=U,this.skan=new Fe.Skan,this.erased=!1,this.deepLinkListeners=new Set,this.undelivered=[],this.deferred=null,this.consent=null}async init(e){var t;if(!this.started&&(this.started=!0,this.opts={collectContext:!0,...e,storage:(t=e.storage)!=null?t:et()},this.opts.storage||console.warn("[traqio] no storage available \u2014 pass `storage` to init() or install @react-native-async-storage/async-storage, otherwise the install is re-sent at every launch."),await this.restoreConsent(e.consent),await this.restoreQueue(),await this.restoreAdQueue(),await this.buildContext(),!await this.restoreErased())){if(!this.isWeb){let i=!!this.opts.storage&&!await this.opts.storage.getItem(F).catch(()=>"1");await this.sendInstallOnce(),this.opts.deepLinking!==!1&&(this.deferred=this.resolveDeferredOnce(i).then(n=>n!=null?n:this.retryDeferredWithConsent()),this.wireDeepLinks())}await this.maybeStartSession(!this.isWeb),await this.initSkan(),await this.initAppleAds(),this.wireLifecycle(),(this.queue.length||this.adQueue.length)&&this.scheduleFlush(0)}}track(e,t){var n,r;if(!this.started||this.erased)return;let i=((n=t==null?void 0:t.transactionId)==null?void 0:n.trim())||void 0;this.enqueue({name:e,eventId:I(),value:t==null?void 0:t.value,currency:t==null?void 0:t.currency,...i?{transactionId:i,store:(r=t==null?void 0:t.store)!=null?r:this.defaultStore()}:{}}),this.skan.markEvent(e),t!=null&&t.value&&this.skan.addRevenue(t.value)}defaultStore(){var t;let e=(t=this.opts.native)==null?void 0:t.platform;return e==="ios"?"app_store":e==="android"?"play_store":void 0}trackAdRevenue(e){this.queueAdRevenue(e,"admob")}trackMaxAdRevenue(e){let t=me(e);t&&this.queueAdRevenue(t,"applovin_max")}queueAdRevenue(e,t){var i;!this.started||this.erased||!(e.valueMicros>=0)||!e.currency||(this.adQueue.push({...e,source:t,impressionId:I(),ts:(i=e.ts)!=null?i:Math.floor(Date.now()/1e3)}),this.skan.addRevenue(e.valueMicros/1e6),this.adQueue.length>fe&&this.adQueue.splice(0,this.adQueue.length-fe),this.persistAdQueue(),this.scheduleFlush())}getInstallReferrer(){return this.started?this.readReferrer():Promise.resolve({status:"unavailable",retryable:!1})}onDeepLink(e){this.deepLinkListeners.add(e);let t=this.undelivered.splice(0);for(let i of t)e(i);return()=>{this.deepLinkListeners.delete(e)}}async handleUrl(e){if(!this.started||this.erased||this.opts.deepLinking===!1||!(0,p.isTrackingUrl)(e))return null;let t=await this.postJson({url:e},"/deeplink/open"),i=(0,p.isOpenUrl)(e),n=(0,p.toDeepLink)(t,i,e);return n&&i&&await this.keepDeferred(n),n&&this.emitDeepLink(n),n}getDeferredDeepLink(){var e;return(e=this.deferred)!=null?e:Promise.resolve(null)}async redeemDeepLinkCode(e){if(!this.started||this.erased||this.opts.deepLinking===!1||!e.trim())return null;let t=await this.postJson({code:e},"/deeplink/code"),i=(0,p.toDeepLink)(t,!0);return i?(await this.keepDeferred(i),this.emitDeepLink(i),i):null}async forgetMe(){return this.started?this.erased?!0:await this.post({deviceId:this.context.deviceId,appUserId:this.opts.userId},"/privacy/forget")?(await this.markErased(),!0):!1:!1}async setConsent(e){var t,i;this.consent=e?"granted":"denied",await((i=(t=this.opts)==null?void 0:t.storage)==null?void 0:i.setItem(he,this.consent).catch(()=>{})),e&&this.deferred&&!this.erased&&(this.deferred=this.deferred.then(n=>n!=null?n:this.retryDeferredWithConsent()),await this.deferred)}get forgotten(){return this.erased}setUserId(e){this.opts&&(this.opts.userId=e)}flush(){this.erased||(this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.drain())}appOpened(){!this.started||this.erased||this.maybeStartSession()}get skanMapping(){return this.skan.activeMapping}async initAppleAds(){if(this.opts.appleAds===!1)return;let e=this.opts.native;(e==null?void 0:e.platform)!=="ios"||!e.getAttributionToken||await(0,Pe.reportAdServicesToken)({endpoint:this.opts.endpoint,sdkKey:this.opts.sdkKey,deviceId:this.context.deviceId,userId:this.opts.userId,storage:this.opts.storage,native:{getAttributionToken:()=>e.getAttributionToken()}})}async initSkan(){if(this.opts.skan===!1)return;let e=this.opts.native;(e==null?void 0:e.platform)!=="ios"||!e.updateConversionValue||await this.skan.init({endpoint:this.opts.endpoint,sdkKey:this.opts.sdkKey,storage:this.opts.storage,native:{updateConversionValue:(t,i,n)=>{var r;return(r=e.updateConversionValue)==null?void 0:r.call(e,t,i,n)}}})}async resolveDeferredOnce(e){var h,k;let t=this.opts.storage;try{let f=await(t==null?void 0:t.getItem(m));if(f)return(0,p.upgradeStoredDeepLink)(JSON.parse(f))}catch{}if(!e&&!await(t==null?void 0:t.getItem(`${m}_pending`).catch(()=>null)))return null;let i=(k=(h=this.opts.native)==null?void 0:h.platform)!=null?k:"android",n=i==="android"?await this.readReferrer():null;await(t==null?void 0:t.setItem(`${m}_pending`,"1").catch(()=>{}));let r=await this.askDeferred(i,n==null?void 0:n.referrer);if(!r)return null;let o=(0,p.toDeepLink)(r,!0),a=await this.storedDeferred();if(a)return a;try{await(t==null?void 0:t.setItem(m,JSON.stringify(o))),!o&&this.consent!=="granted"&&await(t==null?void 0:t.setItem(P,String(Date.now()+Ye)))}catch{}return o&&this.emitDeepLink(o),o}askDeferred(e,t){return this.postJson({platform:e,installReferrer:t,...this.consent?{consent:this.consent}:{},...this.context.osVersion?{osVersion:this.context.osVersion}:{},...this.context.deviceModel?{deviceModel:this.context.deviceModel}:{}},"/deeplink/deferred")}async retryDeferredWithConsent(){var r,o,a,h;let e=(r=this.opts)==null?void 0:r.storage;if(!e||this.consent!=="granted"||this.opts.deepLinking===!1||this.isWeb)return null;let t=Number(await e.getItem(P).catch(()=>null));if(!t||(await e.setItem(P,"").catch(()=>{}),Date.now()>t))return null;let i=await this.storedDeferred();if(i)return i;let n=(0,p.toDeepLink)(await this.askDeferred((a=(o=this.opts.native)==null?void 0:o.platform)!=null?a:"ios",void 0),!0);return n?(await((h=this.opts.storage)==null?void 0:h.setItem(m,JSON.stringify(n)).catch(()=>{})),this.emitDeepLink(n),n):null}async storedDeferred(){var e;try{let t=await((e=this.opts.storage)==null?void 0:e.getItem(m));return t?(0,p.upgradeStoredDeepLink)(JSON.parse(t)):null}catch{return null}}async keepDeferred(e){var t;await this.storedDeferred()||(await((t=this.opts.storage)==null?void 0:t.setItem(m,JSON.stringify(e)).catch(()=>{})),this.deferred=Promise.resolve(e))}emitDeepLink(e){if(!this.deepLinkListeners.size){this.undelivered.push(e);return}for(let t of this.deepLinkListeners)try{t(e)}catch{}}wireDeepLinks(){try{let{Linking:e}=(E(),C(q));if(!e)return;e.getInitialURL().then(t=>t&&this.handleUrl(t)).catch(()=>{}),e.addEventListener("url",({url:t})=>{this.handleUrl(t)})}catch{}}get isWeb(){var e;return((e=this.opts)==null?void 0:e.platform)==="web"}wireLifecycle(){let e={background:()=>this.flush(),foreground:()=>this.appOpened()};if(this.opts.lifecycle){this.opts.lifecycle(e);return}try{let{AppState:t}=(E(),C(q));t.addEventListener("change",i=>{if(i!=="active"){this.scheduleFlush(0);return}this.maybeStartSession()})}catch{}}async restoreErased(){var e;try{this.erased=await((e=this.opts.storage)==null?void 0:e.getItem(de))==="1"}catch{this.erased=!1}return this.erased}async markErased(){var e;this.erased=!0,this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.queue=[],this.adQueue=[],this.adBatchId=null,await this.persistQueue(),await this.persistAdQueue();try{await((e=this.opts.storage)==null?void 0:e.setItem(de,"1"))}catch{}}enqueue(e){this.queue.push(e),this.queue.length>le&&this.queue.splice(0,this.queue.length-le),this.persistQueue(),this.scheduleFlush()}async restoreQueue(){var e;try{let t=await((e=this.opts.storage)==null?void 0:e.getItem(oe));t&&(this.queue=JSON.parse(t))}catch{}}async persistQueue(){var e;try{await((e=this.opts.storage)==null?void 0:e.setItem(oe,JSON.stringify(this.queue)))}catch{}}async restoreAdQueue(){var e,t,i;try{let n=await((e=this.opts.storage)==null?void 0:e.getItem(ae));if(!n)return;let r=JSON.parse(n);this.adQueue=(t=r.items)!=null?t:[],this.adBatchId=(i=r.batchId)!=null?i:null}catch{}}async persistAdQueue(){var e;try{let t={batchId:this.adBatchId,items:this.adQueue};await((e=this.opts.storage)==null?void 0:e.setItem(ae,JSON.stringify(t)))}catch{}}async maybeStartSession(e=!1){var r,o,a;if(this.erased)return;let t=Date.now();if(!e){let h=Number(await((r=this.opts.storage)==null?void 0:r.getItem(ue).catch(()=>null)))||0;if(h&&t-h<He)return}let i=(o=this.context.deviceId)!=null?o:await this.getOrCreateDeviceId();if(await this.post({deviceId:i,appUserId:this.opts.userId,sessionId:I(),ts:Math.floor(t/1e3),context:this.context},"/ingest/session"))try{await((a=this.opts.storage)==null?void 0:a.setItem(ue,String(t)))}catch{}}async buildContext(){var t,i,n;if(!this.opts.collectContext){this.context={sdkVersion:c.SDK_VERSION};return}let e={};try{e=(n=await((i=(t=this.opts.native)==null?void 0:t.getContext)==null?void 0:i.call(t)))!=null?n:{}}catch{}this.context={deviceId:await this.getOrCreateDeviceId(),deviceModel:e.deviceModel,osVersion:e.osVersion,appVersion:this.opts.appVersion,locale:Ge(),sdkVersion:c.SDK_VERSION}}async getOrCreateDeviceId(){var t,i;try{let n=await((t=this.opts.storage)==null?void 0:t.getItem(ce));if(n)return n}catch{}let e=I();try{await((i=this.opts.storage)==null?void 0:i.setItem(ce,e))}catch{}return e}async readReferrer(){var i,n;if(this.referrer)return this.referrer;try{let r=await((i=this.opts.storage)==null?void 0:i.getItem(re));if(r)return this.referrer=JSON.parse(r)}catch{}let e=this.opts.native;if((e==null?void 0:e.platform)!=="android")return{status:e?"not_supported_platform":"unavailable",retryable:!1};let t=await e.getInstallReferrer().catch(()=>({status:"error",retryable:!0}));if(!t.retryable){this.referrer=t;try{await((n=this.opts.storage)==null?void 0:n.setItem(re,JSON.stringify(t)))}catch{}}return t}async readAdvertisingId(){var e;if(!(!this.opts.collectAdvertisingId||!((e=this.opts.native)!=null&&e.getAdvertisingId)))try{return ge(await this.opts.native.getAdvertisingId())}catch{return}}async sendInstallOnce(){var r,o;let e=this.opts.storage;if(e&&await e.getItem(F))return;let t=(o=(r=this.opts.native)==null?void 0:r.platform)!=null?o:"android",i=await this.readReferrer();if(i.retryable&&e){let a=Number(await e.getItem(ne))||0;if(a+1<je){await e.setItem(ne,String(a+1));return}}await this.post({install:{platform:t,installReferrer:i.referrer,appUserId:this.opts.userId,advertisingId:await this.readAdvertisingId(),context:this.context}})&&await(e==null?void 0:e.setItem(F,"1"))}scheduleFlush(e=$e){this.flushTimer||(this.flushTimer=setTimeout(()=>{this.flushTimer=null,this.drain()},e))}async drain(){var e;if(!this.erased&&!(this.flushing||!this.queue.length&&!this.adQueue.length)){this.flushing=!0;try{for(;this.queue.length;){let t=this.queue.slice(0,Qe),i=await this.send({events:t,appUserId:this.opts.userId,deviceId:this.context.deviceId});if(i==="retry")return this.backOff();i==="refused"&&console.warn(`[traqio] the server refused ${t.length} event(s) as malformed \u2014 dropped, not retried`),this.queue.splice(0,t.length),await this.persistQueue(),this.retryMs=U}for(;this.adQueue.length;){let t=(e=this.adQueue[0].source)!=null?e:"admob",i=this.adQueue.findIndex(o=>{var a;return((a=o.source)!=null?a:"admob")!==t}),n=this.adQueue.slice(0,Math.min(Be,i===-1?this.adQueue.length:i));if(this.adBatchId||(this.adBatchId=I(),await this.persistAdQueue()),!await this.post({batchId:this.adBatchId,source:t,environment:this.opts.testAds?"TEST":"PRODUCTION",deviceId:this.context.deviceId,appUserId:this.opts.userId,impressions:n.map(({source:o,...a})=>a)},"/ingest/ad-revenue"))return this.backOff();this.adQueue.splice(0,n.length),this.adBatchId=null,await this.persistAdQueue(),this.retryMs=U}}finally{this.flushing=!1}}}backOff(){this.retryMs=Math.min(this.retryMs*2,Je),this.scheduleFlush(this.retryMs)}async postJson(e,t){try{let i=await fetch(`${this.opts.endpoint}${t}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:this.opts.sdkKey,...e}),...this.keepalive()});return i.ok?await i.json():null}catch{return null}}async restoreConsent(e){var i;if(e!==void 0)return this.setConsent(e);if(this.consent)return this.setConsent(this.consent==="granted");let t=await((i=this.opts.storage)==null?void 0:i.getItem(he).catch(()=>null));(t==="granted"||t==="denied")&&(this.consent=t)}keepalive(){return this.isWeb?{keepalive:!0}:{}}async post(e,t="/ingest"){return await this.send(e,t)==="ok"}async send(e,t="/ingest"){let i=this.consent&&(t==="/ingest"||t==="/ingest/session")?{consent:this.consent}:{};try{let n=await fetch(`${this.opts.endpoint}${t}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({sdkKey:this.opts.sdkKey,...e,...i}),...this.keepalive()});return n.ok?"ok":We.has(n.status)?"refused":"retry"}catch{return"retry"}}};c.Client=A;c.Traqio=new A});var Ie=xe(ye(),1);var Y="traq_";function Q(){return typeof document=="undefined"?void 0:document}function S(s){let e=Q();if(!e)return null;for(let t of e.cookie?e.cookie.split(";"):[]){let i=t.indexOf("=");if(!(i<0)&&t.slice(0,i).trim()===s)try{return decodeURIComponent(t.slice(i+1).trim())}catch{return null}}return null}function V(s,e,t,i){let n=typeof location!="undefined"&&location.protocol==="https:"?"; Secure":"",r=i?`; Domain=${i}`:"";return`${s}=${encodeURIComponent(e)}; Max-Age=${Math.floor(t)}; Path=/${r}; SameSite=Lax${n}`}function ve(s){let e=Q();if(!e||!s.includes(".")||/^[\d.]+$/.test(s)||s.includes(":"))return null;let t=s.split("."),i=`${Y}probe`;for(let n=t.length-2;n>=0;n--){let r=t.slice(n).join(".");if(e.cookie=V(i,"1",10,r),S(i)==="1")return e.cookie=V(i,"",0,r),r}return null}function v(s,e,t,i){let n=Q();if(n)try{n.cookie=V(s,e,t,i)}catch{}}function w(s,e){v(s,"",0,e)}var b="traq_device_id",W=new Set([b]),tt=395*24*60*60;function T(){try{return typeof localStorage=="undefined"?void 0:localStorage}catch{return}}var D=class{constructor(e,t,i){this.persistent=e;this.cookieDomain=t;this.cookies=i;this.memory=new Map}get isPersistent(){return this.persistent}async getItem(e){return this.getSync(e)}async setItem(e,t){this.setSync(e,t)}getSync(e){var i,n,r;if(this.memory.has(e))return(i=this.memory.get(e))!=null?i:null;if(!this.persistent)return null;let t=null;try{t=(r=(n=T())==null?void 0:n.getItem(e))!=null?r:null}catch{}return t===null&&this.cookies&&W.has(e)&&(t=S(e)),t}setSync(e,t){if(!this.persistent){this.memory.set(e,t);return}let i=!1;try{let n=T();n&&(n.setItem(e,t),i=!0)}catch{}i||this.memory.set(e,t),this.cookies&&W.has(e)&&v(e,t,tt,this.cookieDomain())}removeSync(e){var t;this.memory.delete(e);try{(t=T())==null||t.removeItem(e)}catch{}this.cookies&&w(e,this.cookieDomain())}enable(){if(this.persistent)return;this.persistent=!0;let e=[...this.memory];this.memory.clear();for(let[t,i]of e)this.setSync(t,i)}disable(){if(!this.persistent)return;let e=T(),t=[];try{for(let i=0;e&&i<e.length;i++){let n=e.key(i);n&&n.startsWith(Y)&&t.push(n)}}catch{}for(let i of W)t.includes(i)||t.push(i);for(let i of t){let n=this.getSync(i);n!==null&&this.memory.set(i,n)}this.persistent=!1;for(let i of t){try{e==null||e.removeItem(i)}catch{}this.cookies&&w(i,this.cookieDomain())}}};var it="traq_click",g="traq_click",ke=/^[A-Za-z0-9_-]{8,64}$/;function st(s){try{let e=new URL(s).searchParams.get(it);return e&&ke.test(e)?e:null}catch{return null}}var x=class{constructor(e,t,i,n){this.store=e;this.maxAgeMs=t;this.cookieDomain=i;this.cookies=n}capture(e,t=Date.now()){let i=st(e);return i&&this.write(i,t),i}get(e=Date.now()){let t=this.store.getSync(g);if(t)try{let i=JSON.parse(t);if(i&&typeof i.id=="string"&&i.exp>e)return i.id}catch{}if(this.cookies&&this.store.isPersistent){let i=S(g);if(i&&ke.test(i))return i}return null}clear(){this.store.removeSync(g),this.cookies&&w(g,this.cookieDomain())}persistCookie(e=Date.now()){let t=this.store.getSync(g);if(!(!t||!this.cookies||!this.store.isPersistent))try{let i=JSON.parse(t);i.exp>e&&v(g,i.id,(i.exp-e)/1e3,this.cookieDomain())}catch{}}write(e,t){let i={id:e,exp:t+this.maxAgeMs};this.store.setSync(g,JSON.stringify(i)),this.cookies&&this.store.isPersistent&&v(g,e,this.maxAgeMs/1e3,this.cookieDomain())}};var nt="https://api.traqio.app",rt=30,ot=1440*60*1e3;function at(){return typeof window!="undefined"&&typeof document!="undefined"}function ct(){try{return typeof crypto!="undefined"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():null}catch{return null}}var B=class{constructor(){this.core=new Ie.Client;this.store=null;this.trackIds=null;this.waitForConsent=!1;this.started=!1}async init(e){var a,h,k;if(this.started||!at())return;this.started=!0,this.waitForConsent=e.waitForConsent===!0;let t=e.cookies!==!1,i,n=()=>i===void 0?i=ve(location.hostname):i,r=(a=e.consent)!=null?a:this.earlyConsent,o=!this.waitForConsent||r===!0;if(this.store=new D(o,n,t),this.trackIds=new x(this.store,((h=e.trackIdDays)!=null?h:rt)*ot,n,t),this.trackIds.capture(location.href),!this.store.getSync(b)){let f=ct();f&&this.store.setSync(b,f)}await this.core.init({sdkKey:e.sdkKey,endpoint:((k=e.endpoint)!=null?k:nt).replace(/\/+$/,""),storage:this.store,platform:"web",userId:e.userId,consent:r,lifecycle:({background:f,foreground:we})=>{document.addEventListener("visibilitychange",()=>document.visibilityState==="hidden"?f():we()),window.addEventListener("pagehide",f)}})}track(e,t){this.core.track(e,t)}setUserId(e){this.core.setUserId(e)}async setConsent(e){var t;this.started||(this.earlyConsent=e),this.store&&this.waitForConsent&&(e?(this.store.enable(),(t=this.trackIds)==null||t.persistCookie()):this.store.disable()),await this.core.setConsent(e)}getTrackId(){var e,t;return(t=(e=this.trackIds)==null?void 0:e.get())!=null?t:null}async forgetMe(){var t;let e=await this.core.forgetMe();return e&&((t=this.trackIds)==null||t.clear()),e}flush(){this.core.flush()}get forgotten(){return this.core.forgotten}},J=new B;window.Traqio=J;var l=document.currentScript,Se=l==null?void 0:l.dataset.sdkKey;Se&&J.init({sdkKey:Se,endpoint:(l==null?void 0:l.dataset.endpoint)||void 0,waitForConsent:(l==null?void 0:l.dataset.waitForConsent)==="true"});})();
2
2
  //# sourceMappingURL=traqio.min.js.map