@faststats/web 0.3.0 → 0.4.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/chunks/api-urls-DYXBPqSP.js +1 -0
- package/dist/chunks/{identifiers-wylBcW5K.js → identifiers-CWMNnPWe.js} +1 -1
- package/dist/chunks/{replay-Dpl1P6cw.d.ts → replay-BL2Ev5IF.d.ts} +14 -11
- package/dist/chunks/send-data-BXrEi1iz.js +1 -0
- package/dist/error.js +2 -2
- package/dist/feature-flags.js +1 -1
- package/dist/index.d.ts +41 -54
- package/dist/index.js +1 -1
- package/dist/replay.d.ts +1 -1
- package/dist/replay.js +1 -1
- package/dist/web-vitals.d.ts +1 -1
- package/dist/web-vitals.js +1 -1
- package/package.json +2 -2
- package/dist/chunks/api-urls-nKmJnyjD.js +0 -1
- package/dist/chunks/send-data-D6WdryL7.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return e.replace(/\/+$/,``)}function t(t,n){return t===void 0||t===``?n:e(t)||n}function n(e){return t(e,`https://metrics.faststats.dev`)}function r(e){return t(e,`https://flags.faststats.dev`)}const i={events:`/v1/web`,identify:`/v1/identify`,replay:`/v1/replay`,vitals:`/v1/vitals`,flags:`/v1/check`},a=[`utm_source`,`utm_medium`,`utm_campaign`,`utm_term`,`utm_content`];function o(){let e={page:location.pathname,url:location.href,referrer:document.referrer||null,title:document.title||``};if(!location.search)return e;let t=new URLSearchParams(location.search);for(let n of a){let r=t.get(n);r&&(e[n]=r)}return e}export{r as i,o as n,n as r,i as t};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{o as e,r as t}from"./send-data-BXrEi1iz.js";const n=`faststats_anon_id`;function r(){try{return globalThis.localStorage}catch{return}}function i(){let e=r();if(!e)return``;try{let t=e.getItem(n);if(t)return t}catch{return``}let i=t();try{e.setItem(n,i)}catch{return``}return i}function a(t){return t??e()?``:i()}function o(e){if(e)return``;try{let e=r();if(!e)return``;e.removeItem(n)}catch{return``}return i()}export{o as n,a as t};
|
|
@@ -30,6 +30,11 @@ declare class ReplayTracker {
|
|
|
30
30
|
private readonly options;
|
|
31
31
|
private readonly endpoint;
|
|
32
32
|
private readonly compressionSupported;
|
|
33
|
+
private readonly flushInterval;
|
|
34
|
+
private readonly maxEvents;
|
|
35
|
+
private readonly maxPendingBatches;
|
|
36
|
+
private readonly maxQueueSizeBytes;
|
|
37
|
+
private readonly minReplayLengthMs;
|
|
33
38
|
private readonly sampled;
|
|
34
39
|
private readonly events;
|
|
35
40
|
private readonly pending;
|
|
@@ -37,6 +42,7 @@ declare class ReplayTracker {
|
|
|
37
42
|
private viewId;
|
|
38
43
|
private chunkStartedAt;
|
|
39
44
|
private started;
|
|
45
|
+
private startGeneration;
|
|
40
46
|
private startTime;
|
|
41
47
|
private sequence;
|
|
42
48
|
private intervalId;
|
|
@@ -45,18 +51,16 @@ declare class ReplayTracker {
|
|
|
45
51
|
private minLengthFlushTask;
|
|
46
52
|
private stopRecording?;
|
|
47
53
|
private sending;
|
|
54
|
+
private flushQueued;
|
|
55
|
+
private flushQueuedLowLatency;
|
|
48
56
|
private unsubscribeRotation?;
|
|
49
57
|
private cookieless;
|
|
50
58
|
constructor(options: ReplayTrackerOptions);
|
|
51
|
-
private get flushInterval();
|
|
52
|
-
private get maxEvents();
|
|
53
|
-
private get maxPendingBatches();
|
|
54
|
-
private get maxQueueSizeBytes();
|
|
55
|
-
private get minReplayLengthMs();
|
|
56
59
|
private log;
|
|
57
60
|
private sessionContext;
|
|
61
|
+
private clearFlushTimers;
|
|
62
|
+
private clearQueues;
|
|
58
63
|
setCookielessMode(cookieless: boolean): void;
|
|
59
|
-
private resetChunk;
|
|
60
64
|
start(): void;
|
|
61
65
|
trackPageChange(url?: string): void;
|
|
62
66
|
onPageHidden(persisted?: boolean): void;
|
|
@@ -69,15 +73,14 @@ declare class ReplayTracker {
|
|
|
69
73
|
private hasReachedMinLength;
|
|
70
74
|
private requestFlush;
|
|
71
75
|
private scheduleMinLengthFlush;
|
|
76
|
+
private enqueueEventsIfReady;
|
|
72
77
|
private createBatch;
|
|
73
|
-
private
|
|
74
|
-
private
|
|
78
|
+
private batchSizeBytes;
|
|
79
|
+
private dropOldestPending;
|
|
75
80
|
private enqueueBatch;
|
|
76
|
-
private encodeBatch;
|
|
77
81
|
private flush;
|
|
78
82
|
private scheduleRetry;
|
|
79
|
-
private
|
|
80
|
-
private send;
|
|
83
|
+
private sendBatch;
|
|
81
84
|
}
|
|
82
85
|
//#endregion
|
|
83
86
|
export { ReplayTrackerOptions as n, ReplayTracker as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=`faststats_session`;let t=!1,n=null;const r=new Set;function i(){try{return globalThis.localStorage}catch{return null}}function a(){try{return globalThis.sessionStorage}catch{return null}}function o(){return typeof crypto<`u`&&`randomUUID`in crypto?crypto.randomUUID():`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}function s(e){let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n),t|=0;return Math.abs(t)}function c(){let t=i();if(!t)return n;let r=t.getItem(e);if(r)try{let e=JSON.parse(r);if(e.id)return e}catch{}let a=t.getItem(`faststats_session_id`),o=t.getItem(`faststats_session_activity`),s=o===null?NaN:Number(o);if(!a||!Number.isFinite(s))return n;let c=t.getItem(`faststats_session_start`),u=c===null?s:Number(c),d={id:a,start:Number.isFinite(u)?u:s,activity:s};return l(d),t.removeItem(`faststats_session_id`),t.removeItem(`faststats_session_activity`),t.removeItem(`faststats_session_start`),d}function l(t){let r=i();if(!r){n=t;return}try{r.setItem(e,JSON.stringify(t))}catch{n=t}}function u(){n=null;try{i()?.removeItem(e)}catch{}}function d(e,t){return t-e.activity>=18e5||t-e.start>=864e5}function f(e,t){for(let n of r)n(e,t)}function p(e,t){let r=Date.now();if(e)return n?t&&(n.activity=r):n={id:o(),start:r,activity:r},{session:n,rotated:!1,previous:null};let i=c();if(i&&!d(i,r))return t&&(i.activity=r,l(i)),{session:i,rotated:!1,previous:null};let a=i,s={id:o(),start:r,activity:r};return l(s),{session:s,rotated:a!==null,previous:a}}function m(e,t,n){if(t)return n;let r=`faststats_window_id_${e}`,i=a();if(!i)return n;try{let e=i.getItem(r);if(e)return e;let t=o();return i.setItem(r,t),t}catch{return n}}function h(e,t,n,r){let i=r??m(e,n,t.id);return{sessionId:t.id,windowId:i,sessionStart:t.start}}function g(e){t=e,n=null}function _(){return t}function v(e,n){let r=n??t,{session:i}=p(r,!1);return h(e,i,r)}function y(e,n){let r=n??t,{session:i,rotated:a,previous:o}=p(r,!0);if(!a||!o)return null;let s=m(e,r,i.id),c={prev:h(e,o,r,s),next:h(e,i,r,s)};return f(c.prev,c.next),c}function b(e){let r=t,i=v(e,r);if(u(),r){let e=Date.now();n={id:o(),start:e,activity:e}}else try{a()?.removeItem(`faststats_window_id_${e}`)}catch{}let s=v(e,r);return i.sessionId!==s.sessionId&&f(i,s),s}function x(e){return r.add(e),()=>r.delete(e)}function S(e){return typeof e!=`number`||!Number.isFinite(e)?100:Math.max(0,Math.min(100,e))}async function C(e){let{url:t,data:n,contentType:r=`application/json`,headers:i={},debug:a=!1,debugPrefix:o=`[Analytics]`,useBeacon:s=!0,keepalive:c=!0,signal:l}=e;if(l?.aborted)return!1;if(s&&typeof globalThis.navigator?.sendBeacon==`function`)try{let e=n instanceof Blob||typeof Blob>`u`?n:new Blob([n],{type:r});if(globalThis.navigator.sendBeacon(t,e))return a&&console.log(`${o} Sent via beacon`),!0}catch{}try{let e=await fetch(t,{method:`POST`,body:n,headers:{"Content-Type":r,...i},keepalive:c,credentials:`omit`,signal:l}),s=e.ok;if(a){let t=s?`${o} Sent via fetch`:`${o} Failed: ${e.status}`;console[s?`log`:`warn`](t)}return s}catch{return a&&console.warn(`${o} Failed to send`),!1}}export{s as a,b as c,v as i,g as l,C as n,_ as o,o as r,x as s,S as t,y as u};
|
package/dist/error.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{n as e,r as t,t as n}from"./chunks/api-urls-
|
|
2
|
-
`).map(e=>e.trim()).filter(Boolean)}:{error:e,message:typeof t==`string`?t:r,handled:n}}function
|
|
1
|
+
import{n as e,r as t,t as n}from"./chunks/api-urls-DYXBPqSP.js";import{i as r,n as i}from"./chunks/send-data-BXrEi1iz.js";import{t as a}from"./chunks/identifiers-CWMNnPWe.js";const o=/(?:^|\()(chrome|moz|ms-browser|safari-web)-extension:\/\//;function s(e,t,n,r){return t instanceof Error?{error:e,message:t.message||r,handled:n,stack:t.stack?.split(`
|
|
2
|
+
`).map(e=>e.trim()).filter(Boolean)}:{error:e,message:typeof t==`string`?t:r,handled:n}}function c({error:e,message:t,handled:n}){return`${e}\0${t??``}\0${n}`}function l(e,t){let n=t instanceof Error?t.stack??``:``;return o.test(`${e}\n${n}`)}var u=class{endpoint;queue=new Map;timer=null;started=!1;flushing=!1;constructor(e){this.options=e,this.endpoint=`${t(e.baseUrl)}${n.events}`}log(...e){this.options.debug&&console.log(`[ErrorTracker]`,...e)}start(){this.started||typeof window>`u`||(this.started=!0,window.addEventListener(`error`,this.onError),window.addEventListener(`unhandledrejection`,this.onRejection),this.timer=setInterval(()=>void this.flush(),this.options.flushInterval??5e3),this.log(`Started`))}stop(){!this.started||typeof window>`u`||(this.started=!1,window.removeEventListener(`error`,this.onError),window.removeEventListener(`unhandledrejection`,this.onRejection),this.timer&&clearInterval(this.timer),this.timer=null,this.flush(),this.log(`Stopped`))}onPageHidden(e=!1){e||this.flush()}captureError(e){this.enqueue(s(`Error`,e,!0,e.message))}onError=e=>{l(e.filename??``,e.error)||this.enqueue(s(`Error`,e.error,!1,e.message||`Unknown error`))};onRejection=e=>{l(``,e.reason)||this.enqueue(s(`UnhandledRejection`,e.reason,!1,`Unhandled promise rejection`))};enqueue(e){let t=c(e),n=this.queue.get(t);if(n){n.count++;return}this.queue.set(t,{...e,count:1}),this.log(`Captured:`,e),this.queue.size>=(this.options.maxQueueSize??50)&&this.flush()}async flush(){if(this.flushing||this.queue.size===0)return;this.flushing=!0;let t=this.queue;this.queue=new Map;let n=[...t.values()];this.log(`Flushing:`,n);try{let o=globalThis.__SOURCEMAPS_BUILD__?.buildId,s=a(),c=r(this.options.siteKey);if(!await i({url:this.endpoint,data:JSON.stringify({token:this.options.siteKey,...s?{userId:s}:{},sessionId:c.sessionId,windowId:c.windowId,...typeof o==`string`&&o.trim()?{buildId:o}:{},sdkName:this.options.sdkName??`@faststats/web`,sdkVersion:this.options.sdkVersion??`0.4.0`,event:`error`,...e(),properties:{},errors:n}),debug:this.options.debug,debugPrefix:`[ErrorTracker]`}))for(let[e,n]of t){let t=this.queue.get(e);t?t.count+=n.count:this.queue.set(e,n)}}finally{this.flushing=!1}}};export{u as default};
|
package/dist/feature-flags.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{i as e,t}from"./chunks/api-urls-
|
|
1
|
+
import{i as e,t}from"./chunks/api-urls-DYXBPqSP.js";async function n(n,r){if(r.projectToken&&r.projectId)throw Error(`provide either projectToken or projectId, not both`);let i=r.projectToken,a=r.projectId,o=r.identifier,s=r.externalId,c=r.sessionId;if(!i&&!a)throw Error(`feature flag check requires projectToken or projectId`);if(!o&&!s)throw Error(`feature flag check requires serverId or externalId`);let l={key:n,...a?{projectId:a}:{},...o?{identifier:o}:{},...s?{externalId:s}:{},...c?{sessionId:c}:{}},u=r.attributes??{};Object.keys(u).length>0&&(l.attributes=u);let d={"Content-Type":`application/json`};i&&(d.Authorization=`Bearer ${i}`);let f=`${e(r.baseUrl)}${t.flags}`,p=await fetch(f,{method:`POST`,headers:d,body:JSON.stringify(l),credentials:`omit`,signal:r.signal});if(!p.ok){let e=``;try{let t=await p.json();typeof t.error==`string`&&t.error.length>0&&(e=` — ${t.error}`)}catch{}throw Error(`feature flag check failed: HTTP ${p.status}${e}`)}return await p.json()}export{n as fetchFeatureFlagEvaluation};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,9 @@
|
|
|
1
1
|
import { FeatureFlagEvaluation } from "./feature-flags.js";
|
|
2
|
-
import { n as ReplayTrackerOptions } from "./chunks/replay-
|
|
2
|
+
import { n as ReplayTrackerOptions } from "./chunks/replay-BL2Ev5IF.js";
|
|
3
3
|
|
|
4
4
|
//#region src/analytics.d.ts
|
|
5
|
-
interface SamplingOptions {
|
|
6
|
-
percentage?: number;
|
|
7
|
-
}
|
|
8
|
-
interface WebVitalsConfig {
|
|
9
|
-
enabled?: boolean;
|
|
10
|
-
sampling?: SamplingOptions;
|
|
11
|
-
attribution?: boolean;
|
|
12
|
-
}
|
|
13
|
-
interface SessionReplayConfig {
|
|
14
|
-
enabled?: boolean;
|
|
15
|
-
sampling?: SamplingOptions;
|
|
16
|
-
}
|
|
17
5
|
type ConsentMode = "pending" | "granted" | "denied";
|
|
18
|
-
|
|
19
|
-
mode?: ConsentMode;
|
|
20
|
-
cookielessWhilePending?: boolean;
|
|
21
|
-
}
|
|
6
|
+
type PendingBehavior = "anonymous" | "disabled";
|
|
22
7
|
interface IdentifyOptions {
|
|
23
8
|
name?: string;
|
|
24
9
|
phone?: string;
|
|
@@ -32,13 +17,27 @@ interface WebAnalyticsOptions {
|
|
|
32
17
|
debug?: boolean;
|
|
33
18
|
autoTrack?: boolean;
|
|
34
19
|
trackHash?: boolean;
|
|
35
|
-
trackErrors?: boolean;
|
|
36
|
-
trackWebVitals?: boolean;
|
|
37
|
-
trackReplay?: boolean;
|
|
38
20
|
cookieless?: boolean;
|
|
39
|
-
consent?:
|
|
40
|
-
|
|
41
|
-
|
|
21
|
+
consent?: {
|
|
22
|
+
mode?: ConsentMode;
|
|
23
|
+
pendingBehavior?: PendingBehavior;
|
|
24
|
+
};
|
|
25
|
+
webVitals?: {
|
|
26
|
+
enabled?: boolean;
|
|
27
|
+
sampling?: {
|
|
28
|
+
percentage?: number;
|
|
29
|
+
};
|
|
30
|
+
attribution?: boolean;
|
|
31
|
+
};
|
|
32
|
+
sessionReplays?: {
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
sampling?: {
|
|
35
|
+
percentage?: number;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
errorTracking?: {
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
};
|
|
42
41
|
replayOptions?: Partial<ReplayTrackerOptions>;
|
|
43
42
|
/** @internal */
|
|
44
43
|
sdkName?: string;
|
|
@@ -56,41 +55,34 @@ declare function reportError(error: Error): void;
|
|
|
56
55
|
declare function isTrackingDisabled(): boolean;
|
|
57
56
|
declare class WebAnalytics {
|
|
58
57
|
private readonly options;
|
|
59
|
-
private readonly webEndpoint;
|
|
60
58
|
private readonly baseUrl;
|
|
61
|
-
private readonly featureFlagsBaseUrl;
|
|
62
59
|
private readonly debug;
|
|
63
60
|
private started;
|
|
64
61
|
private destroyed;
|
|
65
|
-
private
|
|
62
|
+
private epoch;
|
|
66
63
|
private navTimer;
|
|
67
64
|
private heartbeatTimer;
|
|
68
|
-
private
|
|
69
|
-
private
|
|
70
|
-
private
|
|
71
|
-
private pageUrl;
|
|
72
|
-
private pageHash;
|
|
73
|
-
private hasLeftCurrentPage;
|
|
74
|
-
private scrollUpdate;
|
|
65
|
+
private pageKey;
|
|
66
|
+
private entry;
|
|
67
|
+
private left;
|
|
75
68
|
private consentMode;
|
|
76
|
-
private readonly
|
|
77
|
-
private readonly
|
|
69
|
+
private readonly pendingBehavior;
|
|
70
|
+
private readonly cleanup;
|
|
78
71
|
private readonly childTrackers;
|
|
79
|
-
private readonly pendingReportedErrors;
|
|
80
|
-
private errorTracker;
|
|
81
|
-
private readonly handleVisibilityChange;
|
|
82
|
-
private readonly handlePageHide;
|
|
83
|
-
private readonly handlePageShow;
|
|
84
72
|
constructor(options: WebAnalyticsOptions);
|
|
85
73
|
private log;
|
|
86
|
-
private
|
|
87
|
-
private
|
|
88
|
-
private
|
|
89
|
-
private
|
|
90
|
-
private
|
|
91
|
-
private
|
|
92
|
-
private
|
|
93
|
-
private
|
|
74
|
+
private ensureActive;
|
|
75
|
+
private canTrack;
|
|
76
|
+
private cookieless;
|
|
77
|
+
private touch;
|
|
78
|
+
private bind;
|
|
79
|
+
private loadChild;
|
|
80
|
+
private loadOptionalTrackers;
|
|
81
|
+
private beginTracking;
|
|
82
|
+
private readonly onLinkClick;
|
|
83
|
+
private readonly onVisibilityChange;
|
|
84
|
+
private readonly onPageHide;
|
|
85
|
+
private readonly onPageShow;
|
|
94
86
|
start(): Promise<void>;
|
|
95
87
|
destroy(): void;
|
|
96
88
|
pageview(extra?: Record<string, unknown>): void;
|
|
@@ -107,15 +99,10 @@ declare class WebAnalytics {
|
|
|
107
99
|
signal?: AbortSignal;
|
|
108
100
|
}): Promise<FeatureFlagEvaluation>;
|
|
109
101
|
reportError(error: Error): void;
|
|
110
|
-
private isCookielessMode;
|
|
111
102
|
private send;
|
|
112
|
-
private enterPage;
|
|
113
103
|
private leavePage;
|
|
114
|
-
private trackScroll;
|
|
115
104
|
private startHeartbeat;
|
|
116
|
-
private stopHeartbeat;
|
|
117
105
|
private navigate;
|
|
118
|
-
private links;
|
|
119
106
|
}
|
|
120
107
|
//#endregion
|
|
121
|
-
export { type ConsentMode, type FeatureFlagEvaluation, type IdentifyOptions, WebAnalytics, type WebAnalyticsOptions, getInstance, identify, isTrackingDisabled, logout, optIn, optOut, reportError, setConsentMode, trackEvent };
|
|
108
|
+
export { type ConsentMode, type FeatureFlagEvaluation, type IdentifyOptions, type PendingBehavior, WebAnalytics, type WebAnalyticsOptions, getInstance, identify, isTrackingDisabled, logout, optIn, optOut, reportError, setConsentMode, trackEvent };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{i as e,n as t,r as n,t as r}from"./chunks/api-urls-nKmJnyjD.js";import{d as i,f as a,i as o,n as s,o as c,p as l,t as u}from"./chunks/send-data-D6WdryL7.js";import{n as d,t as f}from"./chunks/identifiers-wylBcW5K.js";const p=new Set([`browser`,`browser_version`,`device`,`os`,`os_version`,`referrer`,`utm_source`,`utm_medium`,`utm_campaign`,`utm_term`,`utm_content`,`title`,`page`,`url`]);function m(e){let t={},n={};for(let[r,i]of Object.entries(e))p.has(r)?t[r]=i:n[r]=i;return{dimensions:t,properties:n}}function h(e,t,n){if(!(e.sessionReplays?.enabled??e.trackReplay))return null;let r=e.replayOptions??{};return{siteKey:e.siteKey,baseUrl:t,debug:n,...r,samplingPercentage:u(r.samplingPercentage??e.sessionReplays?.sampling?.percentage)}}const g={instance:null,pendingConsentMode:void 0};function _(){return typeof window>`u`||E()?null:g.instance}function v(){return g.instance}function y(e,t){_()?.track(e,t??{})}function b(e,t,n){return _()?.identify(e,t,n??{})??Promise.resolve(!1)}function x(e=!0){_()?.logout(e)}function S(e){if(g.instance){g.instance.setConsentMode(e);return}g.pendingConsentMode=e}function C(){S(`granted`)}function w(){S(`denied`)}function T(e){_()?.reportError(e)}function E(){try{let e=globalThis.localStorage;if(!e)return!1;let t=e.getItem(`disable-faststats`);return t===`true`||t===`1`}catch{return!1}}function D(e){for(;e;){if(e instanceof HTMLAnchorElement&&e.href)return e;e=e.parentNode}return null}function O(){let e={};if(!location.search)return e;let t=new URLSearchParams(location.search);for(let n of[`utm_source`,`utm_medium`,`utm_campaign`,`utm_term`,`utm_content`]){let r=t.get(n);r&&(e[n]=r)}return e}var k=class{webEndpoint;baseUrl;featureFlagsBaseUrl;debug;started=!1;destroyed=!1;pageKey=``;navTimer=null;heartbeatTimer=null;scrollDepth=0;pageEntryTime=0;pagePath=``;pageUrl=``;pageHash=``;hasLeftCurrentPage=!1;scrollUpdate=null;consentMode;cookielessWhilePending;cleanupCallbacks=[];childTrackers=[];pendingReportedErrors=[];errorTracker=null;handleVisibilityChange=()=>{if(l(this.options.siteKey,this.isCookielessMode()),document.visibilityState===`hidden`){this.notifyChildren(`onPageHidden`),this.leavePage(),this.stopHeartbeat();return}this.enterPage(),this.startHeartbeat()};handlePageHide=e=>{this.notifyChildren(`onPageHidden`,e.persisted),e.persisted||this.leavePage()};handlePageShow=e=>{this.notifyChildren(`onPageShow`,e.persisted)};constructor(t){this.options=t,this.baseUrl=n(t.baseUrl),this.featureFlagsBaseUrl=e(t.featureFlagsBaseUrl),this.webEndpoint=`${this.baseUrl}${r.events}`,this.debug=t.debug??!1,this.consentMode=t.consent?.mode??`granted`,this.cookielessWhilePending=t.consent?.cookielessWhilePending??!0,g.pendingConsentMode!==void 0&&(this.consentMode=g.pendingConsentMode,g.pendingConsentMode=void 0),(t.autoTrack??!0)&&this.init()}log(e){this.debug&&console.log(`[Analytics] ${e}`)}init(){if(!(typeof window>`u`)){if(E()){this.log(`disabled`);return}setTimeout(()=>void this.start(),0)}}isActive(){return this.started&&!this.destroyed&&g.instance===this}ensureStarted(){return typeof window>`u`||this.destroyed||E()?!1:(this.started||this.start(),this.isActive())}addListener(e,t,n,r){e.addEventListener(t,n,r),this.cleanupCallbacks.push(()=>e.removeEventListener(t,n,r))}patchHistory(){for(let e of[`pushState`,`replaceState`]){let t=history[e].bind(history);history[e]=(e,n,r)=>{t(e,n,r),this.navigate()},this.cleanupCallbacks.push(()=>{history[e]=t})}}notifyChildren(e,t=!1){for(let n of this.childTrackers)n[e]?.(t)}async startChildTracker(e,t){try{let n=await t();return!this.isActive()||(await Promise.resolve(n.start()),!this.isActive())?(n.stop?.(),null):(this.childTrackers.push(n),this.log(`${e} loaded`),n)}catch(t){return this.log(`failed to initialize ${e} tracker: ${String(t)}`),null}}startOptionalTrackers(){let e=this.options,t=h(e,this.baseUrl,this.debug);t&&this.startChildTracker(`replay`,async()=>{let{default:e}=await import(`./replay.js`);return new e({...t,cookieless:this.isCookielessMode()})}),e.trackErrors&&this.startChildTracker(`error`,async()=>{let{default:t}=await import(`./error.js`);return new t({siteKey:e.siteKey,baseUrl:this.baseUrl,debug:this.debug,sdkName:e.sdkName,sdkVersion:e.sdkVersion})}).then(e=>{if(e){this.errorTracker=e;for(let t of this.pendingReportedErrors.splice(0))e.captureError(t)}}),(e.trackWebVitals??e.webVitals?.enabled)&&this.startChildTracker(`web-vitals`,async()=>{let{default:t}=await import(`./web-vitals.js`);return new t({siteKey:e.siteKey,baseUrl:this.baseUrl,debug:this.debug,samplingPercentage:u(e.webVitals?.sampling?.percentage),attribution:e.webVitals?.attribution??!1})})}async start(){if(!(this.started||this.destroyed||typeof window>`u`)){if(g.instance&&g.instance!==this){this.log(`already started by another instance`);return}if(E()){this.log(`disabled`);return}this.started=!0,g.instance=this,a(this.isCookielessMode()),o(),this.startOptionalTrackers(),this.enterPage(),this.pageview({trigger:`load`}),this.links(),this.trackScroll(),this.startHeartbeat(),this.addListener(document,`visibilitychange`,this.handleVisibilityChange),this.addListener(window,`pagehide`,this.handlePageHide),this.addListener(window,`pageshow`,this.handlePageShow),this.addListener(window,`popstate`,()=>this.navigate()),this.options.trackHash&&this.addListener(window,`hashchange`,()=>this.navigate()),this.patchHistory()}}destroy(){if(!this.destroyed){this.started&&typeof window<`u`&&this.leavePage(),this.pendingReportedErrors.length=0,this.navTimer&&clearTimeout(this.navTimer),this.stopHeartbeat();for(let e of this.cleanupCallbacks.splice(0))e();for(let e of this.childTrackers.splice(0))e.stop?.();this.errorTracker=null,g.instance===this&&(g.instance=null),this.started=!1,this.destroyed=!0}}pageview(e={}){if(!this.ensureStarted())return;let t=`${location.pathname}|${this.options.trackHash??!1?location.hash:``}`;t!==this.pageKey&&(this.pageKey=t,this.send(`pageview`,e))}track(e,t={}){this.ensureStarted()&&this.send(e,t)}identify(e,t,n={}){if(!this.ensureStarted()||this.isCookielessMode())return Promise.resolve(!1);let i=e.trim(),a=t.trim();return!i||!a?Promise.resolve(!1):s({url:`${this.baseUrl}${r.identify}`,data:JSON.stringify({token:this.options.siteKey,identifier:this.getAnonymousId(),externalId:i,email:a,name:n.name?.trim()||void 0,phone:n.phone?.trim()||void 0,avatarUrl:n.avatarUrl?.trim()||void 0,traits:n.traits??{}}),contentType:`text/plain`,debug:this.debug,debugPrefix:`[Analytics] identify`,useBeacon:!1})}logout(e=!0){this.ensureStarted()&&(e&&d(this.isCookielessMode()),i(this.options.siteKey))}setConsentMode(e){this.consentMode=e;let t=this.isCookielessMode();a(t);for(let e of this.childTrackers)e.setCookielessMode?.(t)}getConsentMode(){return this.consentMode}getAnonymousId(){return f(this.isCookielessMode())}getSessionId(){return o()}getWindowId(){return c(this.options.siteKey,this.isCookielessMode()).windowId}async checkFeatureFlag(e,t,n){if(typeof window>`u`||E())return{value:`false`};let r=n?.externalId?.trim(),i=this.getAnonymousId();if(!r&&!i)return{value:`false`};let{fetchFeatureFlagEvaluation:a}=await import(`./feature-flags.js`);return a(e,{baseUrl:this.featureFlagsBaseUrl,projectToken:this.options.siteKey,...i?{identifier:i}:{},...r?{externalId:r}:{},sessionId:this.getSessionId(),attributes:t,signal:n?.signal})}reportError(e){if(!(!this.ensureStarted()||!(this.options.trackErrors??!1))){if(this.errorTracker){this.errorTracker.captureError(e);return}this.pendingReportedErrors.length>=50&&this.pendingReportedErrors.shift(),this.pendingReportedErrors.push(e)}}isCookielessMode(){return this.options.cookieless||this.consentMode===`denied`?!0:this.consentMode===`pending`?this.cookielessWhilePending:!1}send(e,n={}){if(typeof window>`u`||this.destroyed||E())return;let r=this.isCookielessMode(),i=f(r),a=c(this.options.siteKey,r),{dimensions:o,properties:l}=m(n);this.log(e),s({url:this.webEndpoint,data:JSON.stringify({token:this.options.siteKey,...i?{userId:i}:{},sessionId:a.sessionId,event:e,...t(),...O(),...o,properties:l}),contentType:`text/plain`,debug:this.debug,debugPrefix:`[Analytics] ${e}`})}enterPage(){this.pageEntryTime=Date.now(),this.pagePath=location.pathname,this.pageUrl=location.href,this.pageHash=location.hash,this.scrollDepth=0,this.hasLeftCurrentPage=!1}leavePage(){if(this.destroyed||this.hasLeftCurrentPage)return;this.hasLeftCurrentPage=!0;let e=Date.now(),t=c(this.options.siteKey,this.isCookielessMode());this.send(`page_leave`,{page:this.pagePath,url:this.pageUrl,time_on_page:e-this.pageEntryTime,scroll_depth:this.scrollDepth,session_duration:e-t.sessionStart})}trackScroll(){let e=()=>{let e=document.documentElement,t=document.body,n=window.innerHeight,r=Math.max(e.scrollHeight,t.scrollHeight);if(r<=n){this.scrollDepth=100;return}let i=Math.min(100,Math.round(((window.scrollY||e.scrollTop)+n)/r*100));i>this.scrollDepth&&(this.scrollDepth=i)};this.scrollUpdate=e,e(),this.addListener(window,`scroll`,e,{passive:!0})}startHeartbeat(){this.stopHeartbeat(),this.heartbeatTimer=setInterval(()=>{if(document.visibilityState===`hidden`){this.stopHeartbeat();return}l(this.options.siteKey,this.isCookielessMode())},300*1e3)}stopHeartbeat(){this.heartbeatTimer&&=(clearInterval(this.heartbeatTimer),null)}navigate(){!this.started||this.destroyed||(this.navTimer&&clearTimeout(this.navTimer),this.navTimer=setTimeout(()=>{this.navTimer=null;let e=location.pathname!==this.pagePath,t=(this.options.trackHash??!1)&&location.hash!==this.pageHash;if(!(!e&&!t)){for(let e of this.childTrackers)e.trackPageChange?.(location.href);this.leavePage(),this.enterPage(),this.scrollUpdate?.(),this.pageview({trigger:`navigation`})}},300))}links(){let e=e=>{let t=D(e.target);t&&t.host!==location.host&&this.track(`outbound_link`,{outbound_link:t.href})};this.addListener(document,`click`,e),this.addListener(document,`auxclick`,e)}};export{k as WebAnalytics,v as getInstance,b as identify,E as isTrackingDisabled,x as logout,C as optIn,w as optOut,T as reportError,S as setConsentMode,y as trackEvent};
|
|
1
|
+
import{i as e,n as t,r as n,t as r}from"./chunks/api-urls-DYXBPqSP.js";import{c as i,i as a,l as o,n as s,t as c,u as l}from"./chunks/send-data-BXrEi1iz.js";import{n as u,t as d}from"./chunks/identifiers-CWMNnPWe.js";let f=null,p;function m(){return f}function h(e,t){typeof window>`u`||S()||f?.track(e,t??{})}function g(e,t,n){return typeof window>`u`||S()?Promise.resolve(!1):f?.identify(e,t,n??{})??Promise.resolve(!1)}function _(e=!0){typeof window>`u`||S()||f?.logout(e)}function v(e){f?f.setConsentMode(e):p=e}function y(){v(`granted`)}function b(){v(`denied`)}function x(e){typeof window>`u`||S()||f?.reportError(e)}function S(){try{let e=globalThis.localStorage?.getItem(`disable-faststats`);return e===`true`||e===`1`}catch{return!1}}var C=class{baseUrl;debug;started=!1;destroyed=!1;epoch=0;navTimer=null;heartbeatTimer=null;pageKey=``;entry={path:``,url:``,hash:``};left=!1;consentMode;pendingBehavior;cleanup=[];childTrackers=[];constructor(e){if(this.options=e,this.baseUrl=n(e.baseUrl),this.debug=e.debug??!1,this.consentMode=p??e.consent?.mode??`granted`,p=void 0,this.pendingBehavior=e.consent?.pendingBehavior??`anonymous`,e.autoTrack??!0){if(typeof window>`u`)return;if(S()){this.log(`disabled`);return}setTimeout(()=>void this.start(),0)}}log(e){this.debug&&console.log(`[Analytics] ${e}`)}ensureActive(){return typeof window>`u`||this.destroyed||S()?!1:(this.started||this.start(),this.started&&!this.destroyed&&f===this)}canTrack(){return!(this.consentMode===`pending`&&this.pendingBehavior===`disabled`)}cookieless(){return!!this.options.cookieless||this.consentMode===`denied`||this.consentMode===`pending`&&this.pendingBehavior===`anonymous`}touch(){this.canTrack()&&l(this.options.siteKey,this.cookieless())}bind(e,t,n,r){e.addEventListener(t,n,r),this.cleanup.push(()=>e.removeEventListener(t,n,r))}loadChild(e,t){let n=this.epoch;t().then(async t=>{if(this.epoch!==n||!this.started||(await t.start(),this.epoch!==n||!this.started))return t.stop?.();this.childTrackers.push(t),this.log(`${e} loaded`)}).catch(t=>this.log(`failed to initialize ${e} tracker: ${String(t)}`))}loadOptionalTrackers(){let e=this.options,t={siteKey:e.siteKey,baseUrl:this.baseUrl,debug:this.debug};if(e.sessionReplays?.enabled){let n=e.replayOptions??{};this.loadChild(`replay`,async()=>{let{default:r}=await import(`./replay.js`);return new r({...t,...n,samplingPercentage:c(n.samplingPercentage??e.sessionReplays?.sampling?.percentage),cookieless:this.cookieless()})})}e.errorTracking?.enabled&&this.loadChild(`error`,async()=>{let{default:n}=await import(`./error.js`);return new n({...t,sdkName:e.sdkName,sdkVersion:e.sdkVersion})}),e.webVitals?.enabled&&this.loadChild(`web-vitals`,async()=>{let{default:n}=await import(`./web-vitals.js`);return new n({...t,samplingPercentage:c(e.webVitals?.sampling?.percentage),attribution:e.webVitals?.attribution??!1})})}beginTracking(e){this.touch(),this.loadOptionalTrackers(),this.entry={path:location.pathname,url:location.href,hash:location.hash},this.left=!1,this.pageKey=``,this.pageview({trigger:e})}onLinkClick=e=>{let t=e.target;for(;t;){if(t instanceof HTMLAnchorElement&&t.href&&t.host!==location.host){this.track(`outbound_link`,{outbound_link:t.href});return}t=t.parentNode}};onVisibilityChange=()=>{if(this.touch(),document.visibilityState===`hidden`){for(let e of this.childTrackers)e.onPageHidden?.();this.heartbeatTimer&&clearInterval(this.heartbeatTimer),this.heartbeatTimer=null;return}this.startHeartbeat()};onPageHide=e=>{let t=e.persisted;for(let e of this.childTrackers)e.onPageHidden?.(t);t||this.leavePage()};onPageShow=e=>{for(let t of this.childTrackers)t.onPageShow?.(e.persisted)};async start(){if(this.started||this.destroyed||typeof window>`u`)return;if(f&&f!==this){this.log(`already started by another instance`);return}if(S()){this.log(`disabled`);return}this.started=!0,f=this,o(this.cookieless()),this.canTrack()&&this.beginTracking(`load`),this.startHeartbeat();let e=()=>this.navigate(),t=[[document,`click`,this.onLinkClick],[document,`auxclick`,this.onLinkClick],[document,`visibilitychange`,this.onVisibilityChange],[window,`pagehide`,this.onPageHide],[window,`pageshow`,this.onPageShow],[window,`popstate`,e]];(this.options.trackHash??!1)&&t.push([window,`hashchange`,e]);for(let[e,n,r]of t)this.bind(e,n,r);for(let e of[`pushState`,`replaceState`]){let t=history[e].bind(history);history[e]=(e,n,r)=>{t(e,n,r),this.navigate()},this.cleanup.push(()=>{history[e]=t})}}destroy(){if(!this.destroyed){this.epoch++,this.started&&typeof window<`u`&&this.leavePage(),this.navTimer&&clearTimeout(this.navTimer),this.heartbeatTimer&&clearInterval(this.heartbeatTimer),this.heartbeatTimer=null;for(let e of this.cleanup.splice(0))e();for(let e of this.childTrackers.splice(0))e.stop?.();f===this&&(f=null),this.started=!1,this.destroyed=!0}}pageview(e={}){if(!this.ensureActive())return;let t=this.options.trackHash??!1,n=`${location.pathname}|${t?location.hash:``}`;n!==this.pageKey&&(this.pageKey=n,this.send(`pageview`,e))}track(e,t={}){if(!this.ensureActive())return;let n=e.trim();n&&this.send(n,t)}identify(e,t,n={}){if(!this.ensureActive()||!this.canTrack()||this.cookieless())return Promise.resolve(!1);let i=e.trim(),a=t.trim();return!i||!a?Promise.resolve(!1):s({url:`${this.baseUrl}${r.identify}`,data:JSON.stringify({token:this.options.siteKey,identifier:this.getAnonymousId(),externalId:i,email:a,name:n.name?.trim()||void 0,phone:n.phone?.trim()||void 0,avatarUrl:n.avatarUrl?.trim()||void 0,traits:n.traits??{}}),contentType:`text/plain`,debug:this.debug,debugPrefix:`[Analytics] identify`,useBeacon:!1})}logout(e=!0){this.ensureActive()&&(e&&u(this.cookieless()),i(this.options.siteKey))}setConsentMode(e){let t=!this.canTrack();this.consentMode=e;let n=this.cookieless();o(n);for(let e of this.childTrackers)e.setCookielessMode?.(n);t&&this.canTrack()&&this.started&&this.beginTracking(`consent`)}getConsentMode(){return this.consentMode}getAnonymousId(){return this.canTrack()?d(this.cookieless()):``}getSessionId(){return a(this.options.siteKey,this.cookieless()).sessionId}getWindowId(){return a(this.options.siteKey,this.cookieless()).windowId}async checkFeatureFlag(t,n,r){if(typeof window>`u`||S()||!this.canTrack())return{value:`false`};let i=r?.externalId?.trim(),a=this.getAnonymousId();if(!i&&!a)return{value:`false`};let{fetchFeatureFlagEvaluation:o}=await import(`./feature-flags.js`);return o(t,{baseUrl:e(this.options.featureFlagsBaseUrl),projectToken:this.options.siteKey,...a?{identifier:a}:{},...i?{externalId:i}:{},sessionId:this.getSessionId(),attributes:n,signal:r?.signal})}reportError(e){!this.ensureActive()||!this.options.errorTracking?.enabled||!this.canTrack()||this.childTrackers.find(e=>`captureError`in e)?.captureError(e)}send(e,n={},i={}){if(typeof window>`u`||this.destroyed||S()||!this.canTrack())return;let o=this.cookieless(),c=d(o),l=a(this.options.siteKey,o);this.log(e),s({url:`${this.baseUrl}${r.events}`,data:JSON.stringify({token:this.options.siteKey,...c?{userId:c}:{},sessionId:l.sessionId,windowId:l.windowId,event:e,...t(),...i,properties:n}),contentType:`text/plain`,debug:this.debug,debugPrefix:`[Analytics] ${e}`})}leavePage(){if(this.destroyed||this.left)return;this.left=!0;let e=a(this.options.siteKey,this.cookieless());this.send(`page_leave`,{session_duration:Date.now()-e.sessionStart},{page:this.entry.path,url:this.entry.url})}startHeartbeat(){this.heartbeatTimer&&clearInterval(this.heartbeatTimer),this.heartbeatTimer=setInterval(()=>{if(document.visibilityState===`hidden`){this.heartbeatTimer&&clearInterval(this.heartbeatTimer),this.heartbeatTimer=null;return}this.touch()},300*1e3)}navigate(){!this.started||this.destroyed||(this.navTimer&&clearTimeout(this.navTimer),this.navTimer=setTimeout(()=>{this.navTimer=null;let e=this.options.trackHash??!1;if(!(location.pathname===this.entry.path&&(!e||location.hash===this.entry.hash))){for(let e of this.childTrackers)e.trackPageChange?.(location.href);this.leavePage(),this.entry={path:location.pathname,url:location.href,hash:location.hash},this.left=!1,this.pageview({trigger:`navigation`})}},300))}};export{C as WebAnalytics,m as getInstance,g as identify,S as isTrackingDisabled,_ as logout,y as optIn,b as optOut,x as reportError,v as setConsentMode,h as trackEvent};
|
package/dist/replay.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as ReplayTrackerOptions, t as ReplayTracker } from "./chunks/replay-
|
|
1
|
+
import { n as ReplayTrackerOptions, t as ReplayTracker } from "./chunks/replay-BL2Ev5IF.js";
|
|
2
2
|
export { ReplayTrackerOptions, ReplayTracker as default };
|
package/dist/replay.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{r as e,t}from"./chunks/api-urls-
|
|
1
|
+
import{r as e,t}from"./chunks/api-urls-DYXBPqSP.js";import{a as n,i as r,n as i,r as a,s as o,t as s,u as c}from"./chunks/send-data-BXrEi1iz.js";import{t as l}from"./chunks/identifiers-CWMNnPWe.js";import{record as u}from"@rrweb/record";const d={mousemove:50,mouseInteraction:!0,scroll:150,media:800,input:`last`};function f(e){return`faststats_replay_sampled_${e}`}function p(e,t,i){let a=s(t);if(a>=100)return!0;if(a<=0)return!1;let o=r(e,i).sessionId,c=f(e),l=`${o}:${a}`;try{let e=sessionStorage?.getItem(c);if(e===l)return!0;if(e?.startsWith(`${o}:`))return!1}catch{}let u=n(o)%100<a;try{sessionStorage?.setItem(c,u?l:`${o}:out`)}catch{}return u}var m=class{endpoint;compressionSupported=typeof window<`u`&&`CompressionStream`in window;flushInterval;maxEvents;maxPendingBatches;maxQueueSizeBytes;minReplayLengthMs;sampled;events=[];pending=[];pendingSizeBytes=0;viewId=a();chunkStartedAt=0;started=!1;startGeneration=0;startTime=0;sequence=0;intervalId=null;flushTask=null;retryTask=null;minLengthFlushTask=null;stopRecording;sending=!1;flushQueued=!1;flushQueuedLowLatency=!1;unsubscribeRotation;cookieless;constructor(n){this.options=n,this.endpoint=`${e(n.baseUrl)}${t.replay}`,this.cookieless=n.cookieless??!1,this.flushInterval=n.flushInterval??1e4,this.maxEvents=n.maxEvents??500,this.maxPendingBatches=n.maxPendingBatches??30,this.maxQueueSizeBytes=n.maxQueueSizeBytes??2097152,this.minReplayLengthMs=n.minReplayLengthMs??3e3,this.sampled=p(n.siteKey,n.samplingPercentage??100,this.cookieless)}log(...e){this.options.debug&&console.log(`[Replay]`,...e)}sessionContext(){return r(this.options.siteKey,this.cookieless)}clearFlushTimers(){this.flushTask&&clearTimeout(this.flushTask),this.retryTask&&clearTimeout(this.retryTask),this.minLengthFlushTask&&clearTimeout(this.minLengthFlushTask),this.flushTask=null,this.retryTask=null,this.minLengthFlushTask=null}clearQueues(){this.events.length=0,this.pending.length=0,this.pendingSizeBytes=0}setCookielessMode(e){this.cookieless!==e&&(this.cookieless=e,e&&(this.clearQueues(),this.clearFlushTimers()),this.startTime=this.sessionContext().sessionStart,this.chunkStartedAt=Date.now())}start(){if(this.started||typeof window>`u`||!this.sampled)return;this.started=!0,this.viewId=a(),this.startTime=this.sessionContext().sessionStart,this.chunkStartedAt=Date.now(),this.unsubscribeRotation=o(e=>{this.onSessionRotated(e)});let e=++this.startGeneration;this.beginRecording(e),this.intervalId=setInterval(this.requestFlush,this.flushInterval),window.addEventListener(`beforeunload`,this.onUnload),this.log(`Recording started`)}trackPageChange(e){this.started&&(this.viewId=a(),this.chunkStartedAt=Date.now(),this.onEvent({type:5,timestamp:Date.now(),data:{tag:`faststats:view`,payload:{href:e??window.location.href,viewId:this.viewId}}},!1))}onPageHidden(e=!1){e||this.flush(!0,!0)}onPageShow(e=!1){!e||!this.started||this.pending.length>0&&this.flush(!1)}async beginRecording(e){let t=this.options.recordConsole===!1?null:await import(`@rrweb/rrweb-plugin-console-record`);if(e!==this.startGeneration)return;let n=[];t&&n.push(t.getRecordConsolePlugin());let r=u({emit:this.onEvent,sampling:this.options.sampling??d,slimDOMOptions:this.options.slimDOMOptions??{script:!0,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0},maskAllInputs:this.options.maskAllInputs??!0,maskInputOptions:this.options.maskInputOptions??{password:!0,email:!0,tel:!0},blockClass:this.options.blockClass,blockSelector:this.options.blockSelector,maskTextClass:this.options.maskTextClass,maskTextSelector:this.options.maskTextSelector,checkoutEveryNms:this.options.checkoutEveryNms??6e4,checkoutEveryNth:this.options.checkoutEveryNth,plugins:n});if(e!==this.startGeneration){r?.();return}this.stopRecording=r}stop(){this.started&&(this.started=!1,this.startGeneration++,this.unsubscribeRotation?.(),this.unsubscribeRotation=void 0,this.stopRecording?.(),this.stopRecording=void 0,this.intervalId&&clearInterval(this.intervalId),this.clearFlushTimers(),this.intervalId=null,window.removeEventListener(`beforeunload`,this.onUnload),this.hasReachedMinLength()||(this.events.length=0,this.log(`Session too short (${Date.now()-this.startTime}ms), discarding events`)),this.flush(!0,!0),this.log(`Recording stopped`))}onSessionRotated(e){this.started&&(this.enqueueEventsIfReady(e,!0),this.pending.length>0&&queueMicrotask(()=>void this.flush(!1)))}onEvent=(e,t)=>{if(this.events.push(e),t||this.events.length>=this.maxEvents||e.type===2&&this.hasReachedMinLength()){this.requestFlush();return}this.scheduleMinLengthFlush()};onUnload=()=>{this.flush(!0,!0)};hasReachedMinLength(){return this.minReplayLengthMs<=0||Date.now()-this.startTime>=this.minReplayLengthMs}requestFlush=()=>{this.flushTask||this.events.length===0||(this.minLengthFlushTask&&=(clearTimeout(this.minLengthFlushTask),null),this.flushTask=setTimeout(()=>{this.flushTask=null,this.flush(!1)},0))};scheduleMinLengthFlush(){if(this.minLengthFlushTask||this.events.length===0||this.hasReachedMinLength())return;let e=Math.max(0,this.minReplayLengthMs-(Date.now()-this.startTime));this.minLengthFlushTask=setTimeout(()=>{this.minLengthFlushTask=null,this.requestFlush()},Math.min(e,2147483647))}enqueueEventsIfReady(e,t){if(this.events.length!==0){if(!t&&!this.hasReachedMinLength()){this.scheduleMinLengthFlush();return}this.enqueueBatch(this.createBatch(this.events.splice(0),e,t))}}createBatch(e,t,n){let r=l(this.cookieless),i=this.sequence++,a=this.chunkStartedAt,o=Date.now();return this.chunkStartedAt=o,{token:this.options.siteKey,sessionId:t.sessionId,windowId:t.windowId,viewId:this.viewId,sessionStart:t.sessionStart,chunkStartedAt:a,chunkEndedAt:o,...r?{identifier:r}:{},batchId:`${t.sessionId}-${i}-${a}`,sequence:i,timestamp:o,url:window.location.href,...n?{isFinal:!0}:{},events:e}}batchSizeBytes(e){return new TextEncoder().encode(JSON.stringify(e)).byteLength}dropOldestPending(e){let t=this.pending.shift();t&&(this.pendingSizeBytes=Math.max(0,this.pendingSizeBytes-this.batchSizeBytes(t)),this.log(`${e}, dropping batch ${t.sequence}`))}enqueueBatch(e){let t=this.batchSizeBytes(e);if(t>this.maxQueueSizeBytes){this.log(`Replay batch ${e.sequence} is ${t}B, exceeding ${this.maxQueueSizeBytes}B queue limit; dropping`);return}for(;(this.pending.length>=this.maxPendingBatches||this.pendingSizeBytes+t>this.maxQueueSizeBytes)&&this.pending.length>0;)this.dropOldestPending(`Pending queue limit reached`);this.pending.push(e),this.pendingSizeBytes+=t}async flush(e,t){let n=c(this.options.siteKey,this.cookieless);if(n&&this.enqueueEventsIfReady(n.prev,!0),this.enqueueEventsIfReady(this.sessionContext(),t),this.pending.length!==0){if(this.sending){this.flushQueued=!0,this.flushQueuedLowLatency||=e;return}this.sending=!0;try{for(;this.pending.length>0;){let t=this.pending[0];if(!t)break;if(!await this.sendBatch(t,e)){this.log(`Failed to send replay batch ${t.sequence}, retrying`),this.scheduleRetry();break}this.dropOldestPending(`Sent replay batch`),e=!1}}finally{if(this.sending=!1,this.flushQueued){let e=this.flushQueuedLowLatency;this.flushQueued=!1,this.flushQueuedLowLatency=!1,this.flush(e)}}}}scheduleRetry(){this.retryTask||=setTimeout(()=>{this.retryTask=null,this.flush(!1)},1e3)}async sendBatch(e,t){let n=JSON.stringify(e),r={debug:!1,useBeacon:t,keepalive:t};if(!t&&(this.options.compress??!0)&&this.compressionSupported)try{let e=new Blob([n]).stream().pipeThrough(new CompressionStream(`gzip`)),t=new Uint8Array(await new Response(e).arrayBuffer());return this.log(`Compressed ${n.length}B -> ${t.byteLength}B (${Math.round(t.byteLength/n.length*100)}%)`),i({url:`${this.endpoint}?encoding=gzip`,data:t,contentType:`application/octet-stream`,...r})}catch{this.log(`Compression failed, using uncompressed`)}return i({url:this.endpoint,data:n,contentType:`application/json`,...r})}};export{m as default};
|
package/dist/web-vitals.d.ts
CHANGED
package/dist/web-vitals.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{r as e,t}from"./chunks/api-urls-
|
|
1
|
+
import{r as e,t}from"./chunks/api-urls-DYXBPqSP.js";import{a as n,i as r,n as i,t as a}from"./chunks/send-data-BXrEi1iz.js";var o=class{endpoint;sampled;metrics=new Map;started=!1;flushing=!1;url=``;constructor(i){this.options=i,this.endpoint=`${e(i.baseUrl)}${t.vitals}`;let o=a(i.samplingPercentage);this.sampled=o>=100||o>0&&n(r(i.siteKey).sessionId)%100<o}async start(){if(this.started||typeof window>`u`)return;this.started=!0,this.url=window.location.href;let e=await(this.options.attribution?import(`web-vitals/attribution`):import(`web-vitals`));if(!this.started)return;let t={reportAllChanges:!0};e.onCLS(this.onMetric,t),e.onINP(this.onMetric,t),e.onLCP(this.onMetric,t),e.onFCP(this.onMetric),e.onTTFB(this.onMetric)}stop(){this.started&&(this.started=!1,this.flush())}onPageHidden(e=!1){e||this.flush()}trackPageChange(e){if(!this.started)return;let t=e??window.location.href;t!==this.url&&(this.flush(),this.url=t)}onMetric=e=>{if(!this.started||!this.sampled)return;let t=e.name,{id:n,rating:r,delta:i,navigationType:a}=e,o=`attribution`in e&&e.attribution?e.attribution:void 0;this.metrics.set(t,{metric:t,value:e.value,attributes:{id:n,rating:r,delta:i,navigationType:a,...o}})};flush(){if(!this.metrics.size||this.flushing)return;this.flushing=!0;let e=this.metrics;this.metrics=new Map;let t=r(this.options.siteKey);i({url:this.endpoint,data:JSON.stringify({token:this.options.siteKey,sessionId:t.sessionId,windowId:t.windowId,vitals:[...e.values()],metadata:{url:this.url}}),debug:this.options.debug,debugPrefix:`[WebVitals]`}).then(t=>{if(!t)for(let[t,n]of e)this.metrics.has(t)||this.metrics.set(t,n);this.flushing=!1})}};export{o as default};
|
package/package.json
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
|
-
"version": "0.
|
|
45
|
+
"version": "0.4.0",
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsdown && bun run check-size",
|
|
48
48
|
"dev": "bun run build",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"check-size": "node scripts/check-bundle-size.mjs"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@
|
|
54
|
+
"@rrweb/types": "^2.0.1",
|
|
55
55
|
"@types/bun": "latest",
|
|
56
56
|
"tsdown": "^0.21.4",
|
|
57
57
|
"typescript": "^5.9.3"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function e(e){return e.replace(/\/+$/,``)}function t(t,n){return t===void 0||t===``?n:e(t)||n}function n(e){return t(e,`https://metrics.faststats.dev`)}function r(e){return t(e,`https://flags.faststats.dev`)}const i={events:`/v1/web`,identify:`/v1/identify`,replay:`/v1/replay`,vitals:`/v1/vitals`,flags:`/v1/check`};function a(){return{page:location.pathname,url:location.href,referrer:document.referrer||null,title:document.title||``}}export{r as i,a as n,n as r,i as t};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e=`faststats_session`;let t=!1,n=null;const r=new Set;function i(){try{return globalThis.localStorage}catch{return null}}function a(){try{return globalThis.sessionStorage}catch{return null}}function o(){return typeof crypto<`u`&&`randomUUID`in crypto?crypto.randomUUID():`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}function s(e){let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n),t|=0;return Math.abs(t)}function c(e){if(!e)return null;try{let t=JSON.parse(e);if(typeof t.id==`string`&&Number.isFinite(t.start)&&Number.isFinite(t.activity))return t}catch{}return null}function l(){let t=i();if(!t)return n;let r=c(t.getItem(e));if(r)return r;let a=t.getItem(`faststats_session_id`),o=t.getItem(`faststats_session_activity`),s=o===null?NaN:Number(o);if(!a||!Number.isFinite(s))return n;let l=t.getItem(`faststats_session_start`),d=l===null?s:Number(l),f={id:a,start:Number.isFinite(d)?d:s,activity:s};return u(f),t.removeItem(`faststats_session_id`),t.removeItem(`faststats_session_activity`),t.removeItem(`faststats_session_start`),f}function u(t){let r=i();if(!r){n=t;return}try{r.setItem(e,JSON.stringify(t))}catch{n=t}}function d(){n=null;try{i()?.removeItem(e)}catch{}}function f(e,t){return t-e.activity>=18e5||t-e.start>=864e5}function p(e,t){for(let n of r)n(e,t)}function m(e,t){let r=Date.now();if(e)return n?t&&(n.activity=r):n={id:o(),start:r,activity:r},{session:n,rotated:!1,previous:null};let i=l();if(i&&!f(i,r))return t&&(i.activity=r,u(i)),{session:i,rotated:!1,previous:null};let a=i,s={id:o(),start:r,activity:r};return u(s),{session:s,rotated:a!==null,previous:a}}function h(e,t,n){if(t)return n;let r=`faststats_window_id_${e}`,i=a();if(!i)return n;try{let e=i.getItem(r);if(e)return e;let t=o();return i.setItem(r,t),t}catch{return n}}function g(e,t,n,r){let i=r??h(e,n,t.id);return{sessionId:t.id,windowId:i,sessionStart:t.start}}function _(e){t=e,e&&(n=null)}function v(){return t}function y(e,n){let r=n??t,{session:i}=m(r,!1);return h(e,r,i.id)}function b(e,n){let r=n??t,{session:i}=m(r,!1);return g(e,i,r)}function x(e){return m(e??t,!0).session.id}function S(e){return m(e??t,!1).session.start}function C(e,n){let r=n??t,{session:i,rotated:a,previous:o}=m(r,!0);if(!a||!o)return null;let s=h(e,r,i.id),c={prev:g(e,o,r,s),next:g(e,i,r,s)};return p(c.prev,c.next),c}function w(e){let r=t,i=b(e,r);if(d(),r){let e=Date.now();n={id:o(),start:e,activity:e}}else try{a()?.removeItem(`faststats_window_id_${e}`)}catch{}let s=b(e,r);return i.sessionId!==s.sessionId&&p(i,s),s}function T(e){return r.add(e),()=>r.delete(e)}function E(e){return typeof e!=`number`||!Number.isFinite(e)?100:Math.max(0,Math.min(100,e))}async function D(e){let{url:t,data:n,contentType:r=`application/json`,headers:i={},debug:a=!1,debugPrefix:o=`[Analytics]`,useBeacon:s=!0,keepalive:c=!0,signal:l}=e;if(l?.aborted)return!1;if(s&&typeof globalThis.navigator?.sendBeacon==`function`)try{let e=n instanceof Blob?n:typeof Blob<`u`?new Blob([n],{type:r}):n;if(globalThis.navigator.sendBeacon(t,e))return a&&console.log(`${o} Sent via beacon`),!0}catch{}if(typeof globalThis.fetch!=`function`)return a&&console.warn(`${o} Failed to send`),!1;try{let e=await fetch(t,{method:`POST`,body:n,headers:{"Content-Type":r,...i},keepalive:c,credentials:`omit`,signal:l}),s=e.ok;if(a){let t=s?`${o} Sent via fetch`:`${o} Failed: ${e.status}`;console[s?`log`:`warn`](t)}return s}catch{return a&&console.warn(`${o} Failed to send`),!1}}export{y as a,s as c,w as d,_ as f,x as i,v as l,D as n,b as o,C as p,o as r,S as s,E as t,T as u};
|