@indiebob/tracker 0.2.4 → 0.4.1

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@indiebob/tracker",
3
- "version": "0.2.4",
4
- "description": "Privacy-first analytics SDK for web applications. <5KB gzipped. No cookies.",
3
+ "version": "0.4.1",
4
+ "description": "Analytics for IndieBob. Stores nothing in the visitor's browser by default; no cookies; about 5.7 KB gzipped.",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "author": "IndieBob <info@indiebob.com>",
@@ -48,7 +48,8 @@
48
48
  "scripts": {
49
49
  "build": "tsup && node scripts/flatten-global.mjs",
50
50
  "dev": "tsup --watch",
51
- "type-check": "tsc --noEmit",
51
+ "test": "vitest run",
52
+ "type-check": "tsc --noEmit && tsc --noEmit -p test",
52
53
  "cdn:publish": "tsx scripts/publish-cdn.mts",
53
54
  "prepublishOnly": "pnpm run build"
54
55
  },
@@ -1,2 +0,0 @@
1
- var J=Object.defineProperty,z=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var k=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var A=(e,t,o)=>t in e?J(e,t,{enumerable:true,configurable:true,writable:true,value:o}):e[t]=o,d=(e,t)=>{for(var o in t||(t={}))W.call(t,o)&&A(e,o,t[o]);if(k)for(var o of k(t))Z.call(t,o)&&A(e,o,t[o]);return e},x=(e,t)=>z(e,G(t));var a=(e,t,o)=>new Promise((n,i)=>{var r=s=>{try{l(o.next(s));}catch(c){i(c);}},g=s=>{try{l(o.throw(s));}catch(c){i(c);}},l=s=>s.done?n(s.value):Promise.resolve(s.value).then(r,g);l((o=o.apply(e,t)).next());});var $="https://www.indiebob.com/api/collect";function D(e){var t,o,n,i;if(!e.projectId||!e.projectId.startsWith("proj_"))throw new Error(`[IndieBob] Invalid projectId "${e.projectId}". Must start with "proj_".`);return {projectId:e.projectId,endpoint:(t=e.endpoint)!=null?t:$,debug:(o=e.debug)!=null?o:false,respectDnt:(n=e.respectDnt)!=null?n:true,respectGpc:(i=e.respectGpc)!=null?i:true}}var u="_ib_anon";function h(){let e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-",t="";if(typeof crypto!="undefined"&&crypto.getRandomValues){let o=new Uint8Array(21);crypto.getRandomValues(o);for(let n of o)t+=e[n%e.length];}else for(let o=0;o<21;o++)t+=e[Math.floor(Math.random()*e.length)];return t}function v(){try{let e=localStorage.getItem(u);return e||(e=h(),localStorage.setItem(u,e)),e}catch(e){try{let t=sessionStorage.getItem(u);return t||(t=h(),sessionStorage.setItem(u,t)),t}catch(t){return h()}}}function P(){try{localStorage.removeItem(u);}catch(e){}try{sessionStorage.removeItem(u);}catch(e){}}var y="_ib_session",I="_ib_session_last";function N(){try{return crypto.randomUUID()}catch(e){return Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)}}function S(){var e;try{let t=Date.now(),o=parseInt((e=sessionStorage.getItem(I))!=null?e:"0",10),n=sessionStorage.getItem(y),i=o>0&&t-o>18e5;return (!n||i)&&(n=N(),sessionStorage.setItem(y,n)),sessionStorage.setItem(I,String(t)),n}catch(t){return N()}}function U(){try{sessionStorage.removeItem(y);}catch(e){}try{sessionStorage.removeItem(I);}catch(e){}}var b="_ib_first_utm",M="_ib_last_utm",_="_ib_referrer";function q(e){let t=new URLSearchParams(e),o={},n=false;return t.has("utm_source")&&(o.source=t.get("utm_source"),n=true),t.has("utm_medium")&&(o.medium=t.get("utm_medium"),n=true),t.has("utm_campaign")&&(o.campaign=t.get("utm_campaign"),n=true),t.has("utm_term")&&(o.term=t.get("utm_term"),n=true),t.has("utm_content")&&(o.content=t.get("utm_content"),n=true),n?o:null}function B(e,t){let o=q(e);try{o&&!localStorage.getItem(b)&&localStorage.setItem(b,JSON.stringify(o)),o&&localStorage.setItem(M,JSON.stringify(o)),t&&!localStorage.getItem(_)&&localStorage.setItem(_,t);}catch(n){}}function F(e){var t;return (t=q(e))!=null?t:void 0}function L(){try{localStorage.removeItem(b);}catch(e){}try{localStorage.removeItem(M);}catch(e){}try{localStorage.removeItem(_);}catch(e){}}var p="_ib_optout",tt="_ib_queue";function f(e){try{if(localStorage.getItem(p))return !1}catch(t){}return !(e.respectDnt&&typeof navigator!="undefined"&&(navigator.doNotTrack==="1"||navigator.msDoNotTrack==="1")||e.respectGpc&&typeof navigator!="undefined"&&navigator.globalPrivacyControl===true)}function j(){try{localStorage.setItem(p,"1");}catch(e){}P(),U(),L();try{localStorage.removeItem(tt);}catch(e){}}function Y(){try{localStorage.removeItem(p);}catch(e){}}function K(){try{return !!localStorage.getItem(p)}catch(e){return false}}var et=[1e3,2e3,4e3];function ot(e){return a(this,null,function*(){return new Promise(t=>setTimeout(t,e))})}function w(e,t,o=false){return a(this,null,function*(){let n=JSON.stringify(e);if(o&&typeof navigator!="undefined"&&navigator.sendBeacon){let i=new Blob([n],{type:"application/json"}),r=navigator.sendBeacon(t.endpoint,i);return t.debug&&console.log("[IndieBob] sendBeacon:",r,e.batch.length,"events"),r}for(let i=0;i<3;i++){try{let r=yield fetch(t.endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:n,keepalive:!0});if(r.ok)return t.debug&&console.log("[IndieBob] sent:",e.batch.length,"events"),!0;if(r.status>=400&&r.status<500)return t.debug&&console.warn("[IndieBob] 4xx error, not retrying:",r.status),!1}catch(r){t.debug&&console.warn("[IndieBob] fetch error (attempt",i+1,"):",r);}i<2&&(yield ot(et[i]));}return t.debug&&console.warn("[IndieBob] failed to send after",3,"attempts"),false})}var E="_ib_queue",nt=10,it=5e3,rt=50,m=class{constructor(t){this.queue=[];this.timer=null;this.flushing=false;this.config=t,this.startTimer(),this.setupVisibilityFlush(),this.setupOnlineFlush();}push(t){this.queue.push(t),this.queue.length>=nt&&this.flush();}flush(t=false){return a(this,null,function*(){if(this.flushing&&!t||this.queue.length===0)return;this.flushing=true;let o=this.queue.splice(0);(yield w({batch:o},this.config,t))||this.pushToOfflineQueue(o),this.flushing=false;})}flushOfflineQueue(){return a(this,null,function*(){let t=this.getOfflineQueue();if(t.length===0)return;this.clearOfflineQueue(),yield w({batch:t},this.config);})}destroy(){this.timer&&clearInterval(this.timer);}startTimer(){this.timer=setInterval(()=>{this.queue.length>0&&this.flush();},it);}setupVisibilityFlush(){typeof document!="undefined"&&document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.flush(true);},{passive:true});}setupOnlineFlush(){typeof window!="undefined"&&(window.addEventListener("online",()=>{this.flushOfflineQueue();}),navigator.onLine&&this.flushOfflineQueue());}pushToOfflineQueue(t){try{let i=[...this.getOfflineQueue(),...t].slice(-rt);localStorage.setItem(E,JSON.stringify(i));}catch(o){}}getOfflineQueue(){try{let t=localStorage.getItem(E);return t?JSON.parse(t):[]}catch(t){return []}}clearOfflineQueue(){try{localStorage.removeItem(E);}catch(t){}}};function Q(){if(typeof window=="undefined")return "desktop";let e=window.innerWidth;return e<=768?"mobile":e<=1024?"tablet":"desktop"}function V(){return typeof window=="undefined"?{width:0,height:0}:{width:window.innerWidth,height:window.innerHeight}}function T(e){var c,O,R,C;let t=typeof window=="undefined",o=(c=e==null?void 0:e.path)!=null?c:t?"":window.location.pathname+window.location.search,n=t?"":window.location.href,i=(O=e==null?void 0:e.title)!=null?O:t?"":document.title,r=(R=e==null?void 0:e.referrer)!=null?R:t?"":document.referrer,g=V(),l=Q(),s=t?void 0:F(window.location.search);return {page:{path:o,url:n,title:i,referrer:r},device:x(d({},g),{type:l}),utm:s,locale:t?"en":navigator.language,timezone:t?"UTC":(C=Intl.DateTimeFormat().resolvedOptions().timeZone)!=null?C:"UTC",sdkVersion:"0.1.1"}}function H(e){if(typeof window=="undefined")return ()=>{};let t=history.pushState.bind(history),o=history.replaceState.bind(history);return history.pushState=function(...n){t(...n),e();},history.replaceState=function(...n){o(...n);},window.addEventListener("popstate",e),()=>{history.pushState=t,history.replaceState=o,window.removeEventListener("popstate",e);}}var X=class e{constructor(t){this.cleanupHistory=null;this.config=t,this.queue=new m(t),typeof window!="undefined"&&B(window.location.search,document.referrer);}static init(t){let o=D(t);return new e(o)}page(t){f(this.config)&&this.queue.push(this.buildEvent("page",{context:T(t)}));}track(t,o){f(this.config)&&this.queue.push(this.buildEvent("track",{event:t,properties:o}));}identify(t,o){f(this.config)&&(this.userId=t,this.queue.push(this.buildEvent("identify",{traits:o,userId:t})));}revenue(t){var o;f(this.config)&&this.queue.push(this.buildEvent("revenue",{revenue:{amount:t.amount,currency:(o=t.currency)!=null?o:"USD",plan:t.plan,type:t.type}}));}optOut(){this.queue.flush(),j(),this.userId=void 0,this.config.debug&&console.log("[IndieBob] opted out");}optIn(){Y(),this.config.debug&&console.log("[IndieBob] opted in");}isOptedOut(){return K()}flush(){return a(this,null,function*(){yield this.queue.flush();})}getAnonymousId(){return v()}getSessionId(){return S()}getConfig(){return Object.freeze(d({},this.config))}enableAutoPage(){var t;return (t=this.cleanupHistory)==null||t.call(this),this.cleanupHistory=H(()=>this.page()),()=>{var o;(o=this.cleanupHistory)==null||o.call(this),this.cleanupHistory=null;}}buildEvent(t,o){var n,i;return {v:1,projectId:this.config.projectId,type:t,anonymousId:v(),userId:(n=this.userId)!=null?n:o.userId,sessionId:S(),timestamp:new Date().toISOString(),context:(i=o.context)!=null?i:T(),event:o.event,properties:o.properties,traits:o.traits,revenue:o.revenue}}};
2
- export{X as a};
@@ -1,2 +0,0 @@
1
- var tt=Object.defineProperty,et=Object.defineProperties;var nt=Object.getOwnPropertyDescriptors;var P=Object.getOwnPropertySymbols;var ot=Object.prototype.hasOwnProperty,it=Object.prototype.propertyIsEnumerable;var k=(e,t,n)=>t in e?tt(e,t,{enumerable:true,configurable:true,writable:true,value:n}):e[t]=n,d=(e,t)=>{for(var n in t||(t={}))ot.call(t,n)&&k(e,n,t[n]);if(P)for(var n of P(t))it.call(t,n)&&k(e,n,t[n]);return e},A=(e,t)=>et(e,nt(t));var c=(e,t,n)=>new Promise((o,i)=>{var r=a=>{try{f(n.next(a));}catch(u){i(u);}},s=a=>{try{f(n.throw(a));}catch(u){i(u);}},f=a=>a.done?o(a.value):Promise.resolve(a.value).then(r,s);f((n=n.apply(e,t)).next());});var rt="https://www.indiebob.com/api/collect";function N(e){var t,n,o,i,r;if(!e.projectId||!e.projectId.startsWith("proj_"))throw new Error(`[IndieBob] Invalid projectId "${e.projectId}". Must start with "proj_".`);return {projectId:e.projectId,endpoint:(t=e.endpoint)!=null?t:rt,debug:(n=e.debug)!=null?n:false,respectDnt:(o=e.respectDnt)!=null?o:true,respectGpc:(i=e.respectGpc)!=null?i:true,autoDetect:(r=e.autoDetect)!=null?r:true}}var l="_ib_anon";function m(){let e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-",t="";if(typeof crypto!="undefined"&&crypto.getRandomValues){let n=new Uint8Array(21);crypto.getRandomValues(n);for(let o of n)t+=e[o%e.length];}else for(let n=0;n<21;n++)t+=e[Math.floor(Math.random()*e.length)];return t}function v(){try{let e=localStorage.getItem(l);return e||(e=m(),localStorage.setItem(l,e)),e}catch(e){try{let t=sessionStorage.getItem(l);return t||(t=m(),sessionStorage.setItem(l,t)),t}catch(t){return m()}}}function U(){try{localStorage.removeItem(l);}catch(e){}try{sessionStorage.removeItem(l);}catch(e){}}var y="_ib_session",I="_ib_session_last";function M(){try{return crypto.randomUUID()}catch(e){return Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)}}function S(){var e;try{let t=Date.now(),n=parseInt((e=sessionStorage.getItem(I))!=null?e:"0",10),o=sessionStorage.getItem(y),i=n>0&&t-n>18e5;return (!o||i)&&(o=M(),sessionStorage.setItem(y,o)),sessionStorage.setItem(I,String(t)),o}catch(t){return M()}}function F(){try{sessionStorage.removeItem(y);}catch(e){}try{sessionStorage.removeItem(I);}catch(e){}}var b="_ib_first_utm",q="_ib_last_utm",_="_ib_referrer";function B(e){let t=new URLSearchParams(e),n={},o=false;return t.has("utm_source")&&(n.source=t.get("utm_source"),o=true),t.has("utm_medium")&&(n.medium=t.get("utm_medium"),o=true),t.has("utm_campaign")&&(n.campaign=t.get("utm_campaign"),o=true),t.has("utm_term")&&(n.term=t.get("utm_term"),o=true),t.has("utm_content")&&(n.content=t.get("utm_content"),o=true),o?n:null}function j(e,t){let n=B(e);try{n&&!localStorage.getItem(b)&&localStorage.setItem(b,JSON.stringify(n)),n&&localStorage.setItem(q,JSON.stringify(n)),t&&!localStorage.getItem(_)&&localStorage.setItem(_,t);}catch(o){}}function L(e){var t;return (t=B(e))!=null?t:void 0}function Y(){try{localStorage.removeItem(b);}catch(e){}try{localStorage.removeItem(q);}catch(e){}try{localStorage.removeItem(_);}catch(e){}}var g="_ib_optout",st="_ib_queue";function p(e){try{if(localStorage.getItem(g))return !1}catch(t){}return !(e.respectDnt&&typeof navigator!="undefined"&&(navigator.doNotTrack==="1"||navigator.msDoNotTrack==="1")||e.respectGpc&&typeof navigator!="undefined"&&navigator.globalPrivacyControl===true)}function K(){try{localStorage.setItem(g,"1");}catch(e){}U(),F(),Y();try{localStorage.removeItem(st);}catch(e){}}function Q(){try{localStorage.removeItem(g);}catch(e){}}function V(){try{return !!localStorage.getItem(g)}catch(e){return false}}var at=[1e3,2e3,4e3];function ct(e){return c(this,null,function*(){return new Promise(t=>setTimeout(t,e))})}function E(e,t,n=false){return c(this,null,function*(){let o=JSON.stringify(e);if(n&&typeof navigator!="undefined"&&navigator.sendBeacon){let i=new Blob([o],{type:"application/json"}),r=navigator.sendBeacon(t.endpoint,i);return t.debug&&console.log("[IndieBob] sendBeacon:",r,e.batch.length,"events"),r}for(let i=0;i<3;i++){try{let r=yield fetch(t.endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:o,keepalive:!0});if(r.ok)return t.debug&&console.log("[IndieBob] sent:",e.batch.length,"events"),!0;if(r.status>=400&&r.status<500)return t.debug&&console.warn("[IndieBob] 4xx error, not retrying:",r.status),!1}catch(r){t.debug&&console.warn("[IndieBob] fetch error (attempt",i+1,"):",r);}i<2&&(yield ct(at[i]));}return t.debug&&console.warn("[IndieBob] failed to send after",3,"attempts"),false})}var w="_ib_queue",ut=10,lt=5e3,ft=50,h=class{constructor(t){this.queue=[];this.timer=null;this.flushing=false;this.config=t,this.startTimer(),this.setupVisibilityFlush(),this.setupOnlineFlush();}push(t){this.queue.push(t),this.queue.length>=ut&&this.flush();}flush(t=false){return c(this,null,function*(){if(this.flushing&&!t||this.queue.length===0)return;this.flushing=true;let n=this.queue.splice(0);(yield E({batch:n},this.config,t))||this.pushToOfflineQueue(n),this.flushing=false;})}flushOfflineQueue(){return c(this,null,function*(){let t=this.getOfflineQueue();if(t.length===0)return;this.clearOfflineQueue(),yield E({batch:t},this.config);})}destroy(){this.timer&&clearInterval(this.timer);}startTimer(){this.timer=setInterval(()=>{this.queue.length>0&&this.flush();},lt);}setupVisibilityFlush(){typeof document!="undefined"&&document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.flush(true);},{passive:true});}setupOnlineFlush(){typeof window!="undefined"&&(window.addEventListener("online",()=>{this.flushOfflineQueue();}),navigator.onLine&&this.flushOfflineQueue());}pushToOfflineQueue(t){try{let i=[...this.getOfflineQueue(),...t].slice(-ft);localStorage.setItem(w,JSON.stringify(i));}catch(n){}}getOfflineQueue(){try{let t=localStorage.getItem(w);return t?JSON.parse(t):[]}catch(t){return []}}clearOfflineQueue(){try{localStorage.removeItem(w);}catch(t){}}};function H(){if(typeof window=="undefined")return "desktop";let e=window.innerWidth;return e<=768?"mobile":e<=1024?"tablet":"desktop"}function X(){return typeof window=="undefined"?{width:0,height:0}:{width:window.innerWidth,height:window.innerHeight}}function T(e){var u,C,x,D;let t=typeof window=="undefined",n=(u=e==null?void 0:e.path)!=null?u:t?"":window.location.pathname+window.location.search,o=t?"":window.location.href,i=(C=e==null?void 0:e.title)!=null?C:t?"":document.title,r=(x=e==null?void 0:e.referrer)!=null?x:t?"":document.referrer,s=X(),f=H(),a=t?void 0:L(window.location.search);return {page:{path:n,url:o,title:i,referrer:r},device:A(d({},s),{type:f}),utm:a,locale:t?"en":navigator.language,timezone:t?"UTC":(D=Intl.DateTimeFormat().resolvedOptions().timeZone)!=null?D:"UTC",sdkVersion:"0.1.1"}}function J(e){if(typeof window=="undefined")return ()=>{};let t=history.pushState.bind(history),n=history.replaceState.bind(history);return history.pushState=function(...o){t(...o),e();},history.replaceState=function(...o){n(...o);},window.addEventListener("popstate",e),()=>{history.pushState=t,history.replaceState=n,window.removeEventListener("popstate",e);}}var R={signup:[/\/(sign-?up|register|join|create-?account|get-?started|onboard)/i],login:[/\/(log-?in|sign-?in|auth(\/login)?)/i],pricing:[/\/(pricing|plans|upgrade|subscribe)/i]};function z(e,t){var o,i,r;let n={signup:(o=t==null?void 0:t.signup)!=null?o:R.signup,login:(i=t==null?void 0:t.login)!=null?i:R.login,pricing:(r=t==null?void 0:t.pricing)!=null?r:R.pricing};return n.signup.some(s=>s.test(e))?"signup_start":n.login.some(s=>s.test(e))?"login_view":n.pricing.some(s=>s.test(e))?"pricing_view":null}var O="ib_identified";function $(){return typeof localStorage=="undefined"?true:!localStorage.getItem(O)}function G(){typeof localStorage!="undefined"&&localStorage.setItem(O,"1");}function W(){typeof localStorage!="undefined"&&localStorage.removeItem(O);}var Z=class e{constructor(t){this.cleanupHistory=null;this.config=t,this.queue=new h(t),typeof window!="undefined"&&j(window.location.search,document.referrer);}static init(t){let n=N(t);return new e(n)}page(t){if(!p(this.config))return;let n=T(t);if(this.queue.push(this.buildEvent("page",{context:n})),this.config.autoDetect!==false){let o=n.page.path,i=typeof this.config.autoDetect=="object"?{signup:this.config.autoDetect.signupPaths,login:this.config.autoDetect.loginPaths,pricing:this.config.autoDetect.pricingPaths}:void 0,r=z(o,i);r&&(this.queue.push(this.buildEvent("track",{event:r,properties:{auto:true},context:n})),this.config.debug&&console.log(`[IndieBob] auto-detected: ${r} on ${o}`));}}track(t,n){p(this.config)&&this.queue.push(this.buildEvent("track",{event:t,properties:n}));}identify(t,n){if(p(this.config)&&(this.userId=t,this.queue.push(this.buildEvent("identify",{traits:n,userId:t})),this.config.autoDetect!==false)){let o=$(),i=o?"signup":"login";this.queue.push(this.buildEvent("track",{event:i,properties:{auto:true}})),o&&G(),this.config.debug&&console.log(`[IndieBob] auto-detected: ${i} for ${t}`);}}revenue(t){var n;p(this.config)&&this.queue.push(this.buildEvent("revenue",{revenue:{amount:t.amount,currency:(n=t.currency)!=null?n:"USD",plan:t.plan,type:t.type}}));}optOut(){this.queue.flush(),K(),W(),this.userId=void 0,this.config.debug&&console.log("[IndieBob] opted out");}optIn(){Q(),this.config.debug&&console.log("[IndieBob] opted in");}isOptedOut(){return V()}flush(){return c(this,null,function*(){yield this.queue.flush();})}getAnonymousId(){return v()}getSessionId(){return S()}getConfig(){return Object.freeze(d({},this.config))}enableAutoPage(){var t;return (t=this.cleanupHistory)==null||t.call(this),this.cleanupHistory=J(()=>this.page()),()=>{var n;(n=this.cleanupHistory)==null||n.call(this),this.cleanupHistory=null;}}buildEvent(t,n){var o,i;return {v:1,projectId:this.config.projectId,type:t,anonymousId:v(),userId:(o=this.userId)!=null?o:n.userId,sessionId:S(),timestamp:new Date().toISOString(),context:(i=n.context)!=null?i:T(),event:n.event,properties:n.properties,traits:n.traits,revenue:n.revenue}}};
2
- export{Z as a};
@@ -1,2 +0,0 @@
1
- var J=Object.defineProperty,z=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var k=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var A=(e,t,o)=>t in e?J(e,t,{enumerable:true,configurable:true,writable:true,value:o}):e[t]=o,d=(e,t)=>{for(var o in t||(t={}))W.call(t,o)&&A(e,o,t[o]);if(k)for(var o of k(t))Z.call(t,o)&&A(e,o,t[o]);return e},x=(e,t)=>z(e,G(t));var a=(e,t,o)=>new Promise((n,i)=>{var r=s=>{try{l(o.next(s));}catch(c){i(c);}},g=s=>{try{l(o.throw(s));}catch(c){i(c);}},l=s=>s.done?n(s.value):Promise.resolve(s.value).then(r,g);l((o=o.apply(e,t)).next());});var $="https://indiebob.com/api/collect";function D(e){var t,o,n,i;if(!e.projectId||!e.projectId.startsWith("proj_"))throw new Error(`[IndieBob] Invalid projectId "${e.projectId}". Must start with "proj_".`);return {projectId:e.projectId,endpoint:(t=e.endpoint)!=null?t:$,debug:(o=e.debug)!=null?o:false,respectDnt:(n=e.respectDnt)!=null?n:true,respectGpc:(i=e.respectGpc)!=null?i:true}}var u="_ib_anon";function h(){let e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-",t="";if(typeof crypto!="undefined"&&crypto.getRandomValues){let o=new Uint8Array(21);crypto.getRandomValues(o);for(let n of o)t+=e[n%e.length];}else for(let o=0;o<21;o++)t+=e[Math.floor(Math.random()*e.length)];return t}function v(){try{let e=localStorage.getItem(u);return e||(e=h(),localStorage.setItem(u,e)),e}catch(e){try{let t=sessionStorage.getItem(u);return t||(t=h(),sessionStorage.setItem(u,t)),t}catch(t){return h()}}}function P(){try{localStorage.removeItem(u);}catch(e){}try{sessionStorage.removeItem(u);}catch(e){}}var y="_ib_session",I="_ib_session_last";function N(){try{return crypto.randomUUID()}catch(e){return Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)}}function S(){var e;try{let t=Date.now(),o=parseInt((e=sessionStorage.getItem(I))!=null?e:"0",10),n=sessionStorage.getItem(y),i=o>0&&t-o>18e5;return (!n||i)&&(n=N(),sessionStorage.setItem(y,n)),sessionStorage.setItem(I,String(t)),n}catch(t){return N()}}function U(){try{sessionStorage.removeItem(y);}catch(e){}try{sessionStorage.removeItem(I);}catch(e){}}var b="_ib_first_utm",M="_ib_last_utm",_="_ib_referrer";function q(e){let t=new URLSearchParams(e),o={},n=false;return t.has("utm_source")&&(o.source=t.get("utm_source"),n=true),t.has("utm_medium")&&(o.medium=t.get("utm_medium"),n=true),t.has("utm_campaign")&&(o.campaign=t.get("utm_campaign"),n=true),t.has("utm_term")&&(o.term=t.get("utm_term"),n=true),t.has("utm_content")&&(o.content=t.get("utm_content"),n=true),n?o:null}function B(e,t){let o=q(e);try{o&&!localStorage.getItem(b)&&localStorage.setItem(b,JSON.stringify(o)),o&&localStorage.setItem(M,JSON.stringify(o)),t&&!localStorage.getItem(_)&&localStorage.setItem(_,t);}catch(n){}}function F(e){var t;return (t=q(e))!=null?t:void 0}function L(){try{localStorage.removeItem(b);}catch(e){}try{localStorage.removeItem(M);}catch(e){}try{localStorage.removeItem(_);}catch(e){}}var p="_ib_optout",tt="_ib_queue";function f(e){try{if(localStorage.getItem(p))return !1}catch(t){}return !(e.respectDnt&&typeof navigator!="undefined"&&(navigator.doNotTrack==="1"||navigator.msDoNotTrack==="1")||e.respectGpc&&typeof navigator!="undefined"&&navigator.globalPrivacyControl===true)}function j(){try{localStorage.setItem(p,"1");}catch(e){}P(),U(),L();try{localStorage.removeItem(tt);}catch(e){}}function Y(){try{localStorage.removeItem(p);}catch(e){}}function K(){try{return !!localStorage.getItem(p)}catch(e){return false}}var et=[1e3,2e3,4e3];function ot(e){return a(this,null,function*(){return new Promise(t=>setTimeout(t,e))})}function w(e,t,o=false){return a(this,null,function*(){let n=JSON.stringify(e);if(o&&typeof navigator!="undefined"&&navigator.sendBeacon){let i=new Blob([n],{type:"application/json"}),r=navigator.sendBeacon(t.endpoint,i);return t.debug&&console.log("[IndieBob] sendBeacon:",r,e.batch.length,"events"),r}for(let i=0;i<3;i++){try{let r=yield fetch(t.endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:n,keepalive:!0});if(r.ok)return t.debug&&console.log("[IndieBob] sent:",e.batch.length,"events"),!0;if(r.status>=400&&r.status<500)return t.debug&&console.warn("[IndieBob] 4xx error, not retrying:",r.status),!1}catch(r){t.debug&&console.warn("[IndieBob] fetch error (attempt",i+1,"):",r);}i<2&&(yield ot(et[i]));}return t.debug&&console.warn("[IndieBob] failed to send after",3,"attempts"),false})}var E="_ib_queue",nt=10,it=5e3,rt=50,m=class{constructor(t){this.queue=[];this.timer=null;this.flushing=false;this.config=t,this.startTimer(),this.setupVisibilityFlush(),this.setupOnlineFlush();}push(t){this.queue.push(t),this.queue.length>=nt&&this.flush();}flush(t=false){return a(this,null,function*(){if(this.flushing&&!t||this.queue.length===0)return;this.flushing=true;let o=this.queue.splice(0);(yield w({batch:o},this.config,t))||this.pushToOfflineQueue(o),this.flushing=false;})}flushOfflineQueue(){return a(this,null,function*(){let t=this.getOfflineQueue();if(t.length===0)return;this.clearOfflineQueue(),yield w({batch:t},this.config);})}destroy(){this.timer&&clearInterval(this.timer);}startTimer(){this.timer=setInterval(()=>{this.queue.length>0&&this.flush();},it);}setupVisibilityFlush(){typeof document!="undefined"&&document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.flush(true);},{passive:true});}setupOnlineFlush(){typeof window!="undefined"&&(window.addEventListener("online",()=>{this.flushOfflineQueue();}),navigator.onLine&&this.flushOfflineQueue());}pushToOfflineQueue(t){try{let i=[...this.getOfflineQueue(),...t].slice(-rt);localStorage.setItem(E,JSON.stringify(i));}catch(o){}}getOfflineQueue(){try{let t=localStorage.getItem(E);return t?JSON.parse(t):[]}catch(t){return []}}clearOfflineQueue(){try{localStorage.removeItem(E);}catch(t){}}};function Q(){if(typeof window=="undefined")return "desktop";let e=window.innerWidth;return e<=768?"mobile":e<=1024?"tablet":"desktop"}function V(){return typeof window=="undefined"?{width:0,height:0}:{width:window.innerWidth,height:window.innerHeight}}function T(e){var c,O,R,C;let t=typeof window=="undefined",o=(c=e==null?void 0:e.path)!=null?c:t?"":window.location.pathname+window.location.search,n=t?"":window.location.href,i=(O=e==null?void 0:e.title)!=null?O:t?"":document.title,r=(R=e==null?void 0:e.referrer)!=null?R:t?"":document.referrer,g=V(),l=Q(),s=t?void 0:F(window.location.search);return {page:{path:o,url:n,title:i,referrer:r},device:x(d({},g),{type:l}),utm:s,locale:t?"en":navigator.language,timezone:t?"UTC":(C=Intl.DateTimeFormat().resolvedOptions().timeZone)!=null?C:"UTC",sdkVersion:"0.1.0"}}function H(e){if(typeof window=="undefined")return ()=>{};let t=history.pushState.bind(history),o=history.replaceState.bind(history);return history.pushState=function(...n){t(...n),e();},history.replaceState=function(...n){o(...n);},window.addEventListener("popstate",e),()=>{history.pushState=t,history.replaceState=o,window.removeEventListener("popstate",e);}}var X=class e{constructor(t){this.cleanupHistory=null;this.config=t,this.queue=new m(t),typeof window!="undefined"&&B(window.location.search,document.referrer);}static init(t){let o=D(t);return new e(o)}page(t){f(this.config)&&this.queue.push(this.buildEvent("page",{context:T(t)}));}track(t,o){f(this.config)&&this.queue.push(this.buildEvent("track",{event:t,properties:o}));}identify(t,o){f(this.config)&&(this.userId=t,this.queue.push(this.buildEvent("identify",{traits:o,userId:t})));}revenue(t){var o;f(this.config)&&this.queue.push(this.buildEvent("revenue",{revenue:{amount:t.amount,currency:(o=t.currency)!=null?o:"USD",plan:t.plan,type:t.type}}));}optOut(){this.queue.flush(),j(),this.userId=void 0,this.config.debug&&console.log("[IndieBob] opted out");}optIn(){Y(),this.config.debug&&console.log("[IndieBob] opted in");}isOptedOut(){return K()}flush(){return a(this,null,function*(){yield this.queue.flush();})}getAnonymousId(){return v()}getSessionId(){return S()}getConfig(){return Object.freeze(d({},this.config))}enableAutoPage(){var t;return (t=this.cleanupHistory)==null||t.call(this),this.cleanupHistory=H(()=>this.page()),()=>{var o;(o=this.cleanupHistory)==null||o.call(this),this.cleanupHistory=null;}}buildEvent(t,o){var n,i;return {v:1,projectId:this.config.projectId,type:t,anonymousId:v(),userId:(n=this.userId)!=null?n:o.userId,sessionId:S(),timestamp:new Date().toISOString(),context:(i=o.context)!=null?i:T(),event:o.event,properties:o.properties,traits:o.traits,revenue:o.revenue}}};
2
- export{X as a};