@inferevents/sdk 0.1.0
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/dist/index.cjs +2 -0
- package/dist/index.d.cts +81 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +2 -0
- package/package.json +36 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var h=false;function E(t){h=t;}function a(t,...e){h&&console.log(`[infer] ${t}`,...e);}function g(t,...e){h&&console.warn(`[infer] ${t}`,...e);}function S(t,...e){h&&console.error(`[infer] ${t}`,...e);}var b="infer_event_queue",p=1e3,i=[];function _(){try{return typeof localStorage<"u"?localStorage:null}catch{return null}}function T(t){i.length>=p&&(g("Queue full, dropping oldest event"),i.shift()),i.push(t),a(`Queued event: ${t.event_name} (${i.length} in queue)`);}function I(t){return i.splice(0,t)}function m(){return i.length}function l(t){i=[...t,...i],i.length>p&&(i=i.slice(i.length-p));}function d(){let t=_();if(!(!t||i.length===0))try{t.setItem(b,JSON.stringify(i)),a("Persisted queue to localStorage:",i.length);}catch{}}function C(){let t=_();if(t)try{let e=t.getItem(b);if(e){let n=JSON.parse(e);Array.isArray(n)&&(i=[...n,...i],i.length>p&&(i=i.slice(i.length-p)),a("Restored queue from localStorage:",n.length)),t.removeItem(b);}}catch{}}var D="0.1.0";function O(){return typeof window<"u"&&typeof document<"u"}function x(){let t={platform:"web",sdk_version:D};if(!O())return t;typeof navigator<"u"&&(t.browser=navigator.userAgent,t.locale=navigator.language),typeof screen<"u"&&(t.screen_width=screen.width,t.screen_height=screen.height);try{t.timezone=Intl.DateTimeFormat().resolvedOptions().timeZone;}catch{}return t.page_url=window.location.href,t.page_title=document.title,t}var k="infer_anonymous_id",u=null,y=null,v={};function A(){try{return typeof localStorage<"u"?localStorage:null}catch{return null}}function R(){if(u)return u;let t=A();if(t){let e=t.getItem(k);if(e)return u=e,u}if(u=crypto.randomUUID(),a("Generated anonymous_id:",u),t)try{t.setItem(k,u);}catch{}return u}function L(){return y}function N(t,e){y=t,e&&(v={...v,...e}),a("Identity set:",y,v);}function M(){y=null,v={},u=null;let t=A();if(t)try{t.removeItem(k);}catch{}}var U="infer_session_last_active";var Q={pageView:true,session:true,click:true,formSubmit:true,error:true};function j(t){return t===false?null:t===true?{...Q}:{...Q,...t}}function q(t,e){if(typeof window>"u")return ()=>{};let n=[];return e.session&&$(t),e.pageView&&n.push(z(t)),e.click&&n.push(P(t)),e.formSubmit&&n.push(B(t)),e.error&&n.push(F(t)),()=>{for(let r of n)r();}}function $(t){try{let e=Date.now(),n=sessionStorage.getItem(U);(!n||e-Number(n)>18e5)&&t.trackInternal("session_start",{}),sessionStorage.setItem(U,String(e));}catch{}}function z(t){t.trackInternal("page_view",{url:window.location.href,path:window.location.pathname,title:document.title});let e=history.pushState.bind(history),n=history.replaceState.bind(history),r=()=>{t.trackInternal("page_view",{url:window.location.href,path:window.location.pathname,title:document.title});};return history.pushState=function(...o){e(...o),r();},history.replaceState=function(...o){n(...o),r();},window.addEventListener("popstate",r),()=>{history.pushState=e,history.replaceState=n,window.removeEventListener("popstate",r);}}function P(t){let e=n=>{let r=n.target;if(!r)return;let o=r.closest("a, button, [role='button'], input[type='submit']");if(!o)return;let c={tag:o.tagName.toLowerCase()};o.id&&(c.element_id=o.id),o.className&&typeof o.className=="string"&&(c.element_class=o.className),o.tagName==="A"&&(c.href=o.href),t.trackInternal("click",c);};return document.addEventListener("click",e,{capture:true}),()=>document.removeEventListener("click",e,{capture:true})}function B(t){let e=n=>{let r=n.target;if(!r)return;let o={};r.id&&(o.form_id=r.id),r.action&&(o.form_action=r.action),r.method&&(o.form_method=r.method.toUpperCase()),t.trackInternal("form_submit",o);};return document.addEventListener("submit",e,{capture:true}),()=>document.removeEventListener("submit",e,{capture:true})}function F(t){let e=r=>{t.trackInternal("error",{message:r.message||"Unknown error",filename:r.filename||null,lineno:r.lineno||null,colno:r.colno||null});},n=r=>{let o=r.reason instanceof Error?r.reason.message:typeof r.reason=="string"?r.reason:"Unhandled promise rejection";t.trackInternal("error",{message:o,type:"unhandledrejection"});};return window.addEventListener("error",e),window.addEventListener("unhandledrejection",n),()=>{window.removeEventListener("error",e),window.removeEventListener("unhandledrejection",n);}}var Y="https://api.infer.events",V=20,H=1e4,J=3e4,K=1e3,w=class{config;flushTimer=null;teardownAutoTrack=null;flushing=false;retryCount=0;constructor(e){if(!e.projectId||typeof e.projectId!="string")throw new Error("[infer] projectId is required");if(E(e.debug??false),this.config={projectId:e.projectId,endpoint:e.endpoint??Y,batchSize:e.batchSize??V,flushInterval:e.flushInterval??H},a("Initialized with config:",this.config),C(),this.flushTimer=setInterval(()=>{this.flush();},this.config.flushInterval),typeof window<"u"){let r=()=>{d(),this.sendBeacon();};window.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&r();}),window.addEventListener("pagehide",r);}let n=j(e.autoTrack??false);n&&(this.teardownAutoTrack=q(this,n));}track(e,n){this.enqueueEvent("track",e,n??{});}identify(e,n){N(e,n),this.enqueueEvent("identify","identify",n??{});}page(e){let n={};e&&(n.name=e),typeof window<"u"&&(n.url=window.location.href,n.path=window.location.pathname,n.title=document.title),this.enqueueEvent("page","page_view",n);}screen(e){this.enqueueEvent("screen","screen_view",{name:e});}trackInternal(e,n){this.enqueueEvent("track",e,n);}async flush(){if(this.flushing||m()===0)return;this.flushing=true;let e=I(this.config.batchSize);a(`Flushing ${e.length} events`);try{let n=await fetch(`${this.config.endpoint}/v1/events`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:e})});n.ok?(this.retryCount=0,a(`Flushed ${e.length} events successfully`)):n.status>=500?(g(`Server error ${n.status}, will retry`),l(e),this.scheduleRetry()):S(`Client error ${n.status}, dropping batch`);}catch(n){g("Network error, will retry:",n),l(e),this.scheduleRetry();}finally{this.flushing=false;}m()>0&&this.flush();}destroy(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null),this.teardownAutoTrack&&(this.teardownAutoTrack(),this.teardownAutoTrack=null),d();}enqueueEvent(e,n,r){let o={event_id:crypto.randomUUID(),project_id:this.config.projectId,anonymous_id:R(),event_name:n,event_type:e,properties:r,context:x(),timestamp:new Date().toISOString()},c=L();c&&(o.user_id=c),T(o),m()>=this.config.batchSize&&this.flush();}sendBeacon(){let e=I(this.config.batchSize);if(e.length!==0)try{if(typeof navigator<"u"&&navigator.sendBeacon){let n=new Blob([JSON.stringify({events:e})],{type:"application/json"});navigator.sendBeacon(`${this.config.endpoint}/v1/events`,n)?a(`Beacon sent ${e.length} events`):(l(e),d());}else l(e),d();}catch{l(e),d();}}scheduleRetry(){this.retryCount++;let e=Math.min(K*Math.pow(2,this.retryCount-1)+Math.random()*1e3,J);a(`Retry #${this.retryCount} in ${Math.round(e)}ms`),setTimeout(()=>{this.flush();},e);}};var s=null,f=[];function le(t){s&&s.destroy(),s=new w(t);for(let e of f)switch(e.type){case "track":s.track(...e.args);break;case "identify":s.identify(...e.args);break;case "page":s.page(...e.args);break;case "screen":s.screen(...e.args);break}return f=[],s}function de(t,e){if(!s){f.push({type:"track",args:[t,e]});return}s.track(t,e);}function fe(t,e){if(!s){f.push({type:"identify",args:[t,e]});return}s.identify(t,e);}function ge(t){if(!s){f.push({type:"page",args:[t]});return}s.page(t);}function pe(t){if(!s){f.push({type:"screen",args:[t]});return}s.screen(t);}async function he(){s&&await s.flush();}function me(){s&&(s.destroy(),s=null);}
|
|
2
|
+
exports.destroy=me;exports.flush=he;exports.identify=fe;exports.init=le;exports.page=ge;exports.reset=M;exports.screen=pe;exports.track=de;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
interface AutoTrackConfig {
|
|
2
|
+
pageView: boolean;
|
|
3
|
+
session: boolean;
|
|
4
|
+
click: boolean;
|
|
5
|
+
formSubmit: boolean;
|
|
6
|
+
error: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface InferConfig {
|
|
10
|
+
projectId: string;
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
autoTrack?: boolean | Partial<AutoTrackConfig>;
|
|
13
|
+
batchSize?: number;
|
|
14
|
+
flushInterval?: number;
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare class InferClient {
|
|
18
|
+
private config;
|
|
19
|
+
private flushTimer;
|
|
20
|
+
private teardownAutoTrack;
|
|
21
|
+
private flushing;
|
|
22
|
+
private retryCount;
|
|
23
|
+
constructor(config: InferConfig);
|
|
24
|
+
track(eventName: string, properties?: Record<string, string | number | boolean | null>): void;
|
|
25
|
+
identify(newUserId: string, traits?: Record<string, string | number | boolean | null>): void;
|
|
26
|
+
page(name?: string): void;
|
|
27
|
+
screen(name: string): void;
|
|
28
|
+
/** Internal method for auto-tracking — do not use directly. */
|
|
29
|
+
trackInternal(eventName: string, properties: Record<string, string | number | boolean | null>): void;
|
|
30
|
+
flush(): Promise<void>;
|
|
31
|
+
destroy(): void;
|
|
32
|
+
private enqueueEvent;
|
|
33
|
+
private sendBeacon;
|
|
34
|
+
private scheduleRetry;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface QueuedEvent {
|
|
38
|
+
event_id: string;
|
|
39
|
+
project_id: string;
|
|
40
|
+
user_id?: string;
|
|
41
|
+
anonymous_id: string;
|
|
42
|
+
event_name: string;
|
|
43
|
+
event_type: "track" | "identify" | "page" | "screen";
|
|
44
|
+
properties: Record<string, string | number | boolean | null>;
|
|
45
|
+
context: Record<string, unknown>;
|
|
46
|
+
timestamp: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare function resetIdentity(): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Initialize the Infer SDK. Must be called before tracking events.
|
|
53
|
+
* Events queued before init() will be replayed automatically.
|
|
54
|
+
*/
|
|
55
|
+
declare function init(config: InferConfig): InferClient;
|
|
56
|
+
/**
|
|
57
|
+
* Track a custom event.
|
|
58
|
+
*/
|
|
59
|
+
declare function track(eventName: string, properties?: Record<string, string | number | boolean | null>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Identify a user and link to anonymous_id.
|
|
62
|
+
*/
|
|
63
|
+
declare function identify(userId: string, traits?: Record<string, string | number | boolean | null>): void;
|
|
64
|
+
/**
|
|
65
|
+
* Track a page view. Optionally provide a page name.
|
|
66
|
+
*/
|
|
67
|
+
declare function page(name?: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Track a screen view (for React Native / mobile).
|
|
70
|
+
*/
|
|
71
|
+
declare function screen(name: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Manually flush the event queue.
|
|
74
|
+
*/
|
|
75
|
+
declare function flush(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Destroy the client and clean up timers/listeners.
|
|
78
|
+
*/
|
|
79
|
+
declare function destroy(): void;
|
|
80
|
+
|
|
81
|
+
export { type AutoTrackConfig, InferClient, type InferConfig, type QueuedEvent, destroy, flush, identify, init, page, resetIdentity as reset, screen, track };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
interface AutoTrackConfig {
|
|
2
|
+
pageView: boolean;
|
|
3
|
+
session: boolean;
|
|
4
|
+
click: boolean;
|
|
5
|
+
formSubmit: boolean;
|
|
6
|
+
error: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface InferConfig {
|
|
10
|
+
projectId: string;
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
autoTrack?: boolean | Partial<AutoTrackConfig>;
|
|
13
|
+
batchSize?: number;
|
|
14
|
+
flushInterval?: number;
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare class InferClient {
|
|
18
|
+
private config;
|
|
19
|
+
private flushTimer;
|
|
20
|
+
private teardownAutoTrack;
|
|
21
|
+
private flushing;
|
|
22
|
+
private retryCount;
|
|
23
|
+
constructor(config: InferConfig);
|
|
24
|
+
track(eventName: string, properties?: Record<string, string | number | boolean | null>): void;
|
|
25
|
+
identify(newUserId: string, traits?: Record<string, string | number | boolean | null>): void;
|
|
26
|
+
page(name?: string): void;
|
|
27
|
+
screen(name: string): void;
|
|
28
|
+
/** Internal method for auto-tracking — do not use directly. */
|
|
29
|
+
trackInternal(eventName: string, properties: Record<string, string | number | boolean | null>): void;
|
|
30
|
+
flush(): Promise<void>;
|
|
31
|
+
destroy(): void;
|
|
32
|
+
private enqueueEvent;
|
|
33
|
+
private sendBeacon;
|
|
34
|
+
private scheduleRetry;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface QueuedEvent {
|
|
38
|
+
event_id: string;
|
|
39
|
+
project_id: string;
|
|
40
|
+
user_id?: string;
|
|
41
|
+
anonymous_id: string;
|
|
42
|
+
event_name: string;
|
|
43
|
+
event_type: "track" | "identify" | "page" | "screen";
|
|
44
|
+
properties: Record<string, string | number | boolean | null>;
|
|
45
|
+
context: Record<string, unknown>;
|
|
46
|
+
timestamp: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare function resetIdentity(): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Initialize the Infer SDK. Must be called before tracking events.
|
|
53
|
+
* Events queued before init() will be replayed automatically.
|
|
54
|
+
*/
|
|
55
|
+
declare function init(config: InferConfig): InferClient;
|
|
56
|
+
/**
|
|
57
|
+
* Track a custom event.
|
|
58
|
+
*/
|
|
59
|
+
declare function track(eventName: string, properties?: Record<string, string | number | boolean | null>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Identify a user and link to anonymous_id.
|
|
62
|
+
*/
|
|
63
|
+
declare function identify(userId: string, traits?: Record<string, string | number | boolean | null>): void;
|
|
64
|
+
/**
|
|
65
|
+
* Track a page view. Optionally provide a page name.
|
|
66
|
+
*/
|
|
67
|
+
declare function page(name?: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Track a screen view (for React Native / mobile).
|
|
70
|
+
*/
|
|
71
|
+
declare function screen(name: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Manually flush the event queue.
|
|
74
|
+
*/
|
|
75
|
+
declare function flush(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Destroy the client and clean up timers/listeners.
|
|
78
|
+
*/
|
|
79
|
+
declare function destroy(): void;
|
|
80
|
+
|
|
81
|
+
export { type AutoTrackConfig, InferClient, type InferConfig, type QueuedEvent, destroy, flush, identify, init, page, resetIdentity as reset, screen, track };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var h=false;function E(t){h=t;}function a(t,...e){h&&console.log(`[infer] ${t}`,...e);}function g(t,...e){h&&console.warn(`[infer] ${t}`,...e);}function S(t,...e){h&&console.error(`[infer] ${t}`,...e);}var b="infer_event_queue",p=1e3,i=[];function _(){try{return typeof localStorage<"u"?localStorage:null}catch{return null}}function T(t){i.length>=p&&(g("Queue full, dropping oldest event"),i.shift()),i.push(t),a(`Queued event: ${t.event_name} (${i.length} in queue)`);}function I(t){return i.splice(0,t)}function m(){return i.length}function l(t){i=[...t,...i],i.length>p&&(i=i.slice(i.length-p));}function d(){let t=_();if(!(!t||i.length===0))try{t.setItem(b,JSON.stringify(i)),a("Persisted queue to localStorage:",i.length);}catch{}}function C(){let t=_();if(t)try{let e=t.getItem(b);if(e){let n=JSON.parse(e);Array.isArray(n)&&(i=[...n,...i],i.length>p&&(i=i.slice(i.length-p)),a("Restored queue from localStorage:",n.length)),t.removeItem(b);}}catch{}}var D="0.1.0";function O(){return typeof window<"u"&&typeof document<"u"}function x(){let t={platform:"web",sdk_version:D};if(!O())return t;typeof navigator<"u"&&(t.browser=navigator.userAgent,t.locale=navigator.language),typeof screen<"u"&&(t.screen_width=screen.width,t.screen_height=screen.height);try{t.timezone=Intl.DateTimeFormat().resolvedOptions().timeZone;}catch{}return t.page_url=window.location.href,t.page_title=document.title,t}var k="infer_anonymous_id",u=null,y=null,v={};function A(){try{return typeof localStorage<"u"?localStorage:null}catch{return null}}function R(){if(u)return u;let t=A();if(t){let e=t.getItem(k);if(e)return u=e,u}if(u=crypto.randomUUID(),a("Generated anonymous_id:",u),t)try{t.setItem(k,u);}catch{}return u}function L(){return y}function N(t,e){y=t,e&&(v={...v,...e}),a("Identity set:",y,v);}function M(){y=null,v={},u=null;let t=A();if(t)try{t.removeItem(k);}catch{}}var U="infer_session_last_active";var Q={pageView:true,session:true,click:true,formSubmit:true,error:true};function j(t){return t===false?null:t===true?{...Q}:{...Q,...t}}function q(t,e){if(typeof window>"u")return ()=>{};let n=[];return e.session&&$(t),e.pageView&&n.push(z(t)),e.click&&n.push(P(t)),e.formSubmit&&n.push(B(t)),e.error&&n.push(F(t)),()=>{for(let r of n)r();}}function $(t){try{let e=Date.now(),n=sessionStorage.getItem(U);(!n||e-Number(n)>18e5)&&t.trackInternal("session_start",{}),sessionStorage.setItem(U,String(e));}catch{}}function z(t){t.trackInternal("page_view",{url:window.location.href,path:window.location.pathname,title:document.title});let e=history.pushState.bind(history),n=history.replaceState.bind(history),r=()=>{t.trackInternal("page_view",{url:window.location.href,path:window.location.pathname,title:document.title});};return history.pushState=function(...o){e(...o),r();},history.replaceState=function(...o){n(...o),r();},window.addEventListener("popstate",r),()=>{history.pushState=e,history.replaceState=n,window.removeEventListener("popstate",r);}}function P(t){let e=n=>{let r=n.target;if(!r)return;let o=r.closest("a, button, [role='button'], input[type='submit']");if(!o)return;let c={tag:o.tagName.toLowerCase()};o.id&&(c.element_id=o.id),o.className&&typeof o.className=="string"&&(c.element_class=o.className),o.tagName==="A"&&(c.href=o.href),t.trackInternal("click",c);};return document.addEventListener("click",e,{capture:true}),()=>document.removeEventListener("click",e,{capture:true})}function B(t){let e=n=>{let r=n.target;if(!r)return;let o={};r.id&&(o.form_id=r.id),r.action&&(o.form_action=r.action),r.method&&(o.form_method=r.method.toUpperCase()),t.trackInternal("form_submit",o);};return document.addEventListener("submit",e,{capture:true}),()=>document.removeEventListener("submit",e,{capture:true})}function F(t){let e=r=>{t.trackInternal("error",{message:r.message||"Unknown error",filename:r.filename||null,lineno:r.lineno||null,colno:r.colno||null});},n=r=>{let o=r.reason instanceof Error?r.reason.message:typeof r.reason=="string"?r.reason:"Unhandled promise rejection";t.trackInternal("error",{message:o,type:"unhandledrejection"});};return window.addEventListener("error",e),window.addEventListener("unhandledrejection",n),()=>{window.removeEventListener("error",e),window.removeEventListener("unhandledrejection",n);}}var Y="https://api.infer.events",V=20,H=1e4,J=3e4,K=1e3,w=class{config;flushTimer=null;teardownAutoTrack=null;flushing=false;retryCount=0;constructor(e){if(!e.projectId||typeof e.projectId!="string")throw new Error("[infer] projectId is required");if(E(e.debug??false),this.config={projectId:e.projectId,endpoint:e.endpoint??Y,batchSize:e.batchSize??V,flushInterval:e.flushInterval??H},a("Initialized with config:",this.config),C(),this.flushTimer=setInterval(()=>{this.flush();},this.config.flushInterval),typeof window<"u"){let r=()=>{d(),this.sendBeacon();};window.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&r();}),window.addEventListener("pagehide",r);}let n=j(e.autoTrack??false);n&&(this.teardownAutoTrack=q(this,n));}track(e,n){this.enqueueEvent("track",e,n??{});}identify(e,n){N(e,n),this.enqueueEvent("identify","identify",n??{});}page(e){let n={};e&&(n.name=e),typeof window<"u"&&(n.url=window.location.href,n.path=window.location.pathname,n.title=document.title),this.enqueueEvent("page","page_view",n);}screen(e){this.enqueueEvent("screen","screen_view",{name:e});}trackInternal(e,n){this.enqueueEvent("track",e,n);}async flush(){if(this.flushing||m()===0)return;this.flushing=true;let e=I(this.config.batchSize);a(`Flushing ${e.length} events`);try{let n=await fetch(`${this.config.endpoint}/v1/events`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:e})});n.ok?(this.retryCount=0,a(`Flushed ${e.length} events successfully`)):n.status>=500?(g(`Server error ${n.status}, will retry`),l(e),this.scheduleRetry()):S(`Client error ${n.status}, dropping batch`);}catch(n){g("Network error, will retry:",n),l(e),this.scheduleRetry();}finally{this.flushing=false;}m()>0&&this.flush();}destroy(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null),this.teardownAutoTrack&&(this.teardownAutoTrack(),this.teardownAutoTrack=null),d();}enqueueEvent(e,n,r){let o={event_id:crypto.randomUUID(),project_id:this.config.projectId,anonymous_id:R(),event_name:n,event_type:e,properties:r,context:x(),timestamp:new Date().toISOString()},c=L();c&&(o.user_id=c),T(o),m()>=this.config.batchSize&&this.flush();}sendBeacon(){let e=I(this.config.batchSize);if(e.length!==0)try{if(typeof navigator<"u"&&navigator.sendBeacon){let n=new Blob([JSON.stringify({events:e})],{type:"application/json"});navigator.sendBeacon(`${this.config.endpoint}/v1/events`,n)?a(`Beacon sent ${e.length} events`):(l(e),d());}else l(e),d();}catch{l(e),d();}}scheduleRetry(){this.retryCount++;let e=Math.min(K*Math.pow(2,this.retryCount-1)+Math.random()*1e3,J);a(`Retry #${this.retryCount} in ${Math.round(e)}ms`),setTimeout(()=>{this.flush();},e);}};var s=null,f=[];function le(t){s&&s.destroy(),s=new w(t);for(let e of f)switch(e.type){case "track":s.track(...e.args);break;case "identify":s.identify(...e.args);break;case "page":s.page(...e.args);break;case "screen":s.screen(...e.args);break}return f=[],s}function de(t,e){if(!s){f.push({type:"track",args:[t,e]});return}s.track(t,e);}function fe(t,e){if(!s){f.push({type:"identify",args:[t,e]});return}s.identify(t,e);}function ge(t){if(!s){f.push({type:"page",args:[t]});return}s.page(t);}function pe(t){if(!s){f.push({type:"screen",args:[t]});return}s.screen(t);}async function he(){s&&await s.flush();}function me(){s&&(s.destroy(),s=null);}
|
|
2
|
+
export{me as destroy,he as flush,fe as identify,le as init,ge as page,M as reset,pe as screen,de as track};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inferevents/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lightweight JavaScript SDK for Infer analytics",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"lint": "tsc --noEmit"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8",
|
|
32
|
+
"typescript": "^5.7"
|
|
33
|
+
},
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"license": "MIT"
|
|
36
|
+
}
|