@scoperat/tracker 0.1.1 → 0.3.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.d.ts CHANGED
@@ -1,4 +1,201 @@
1
- import scoperat from "./client.js";
2
- export default scoperat;
3
- export type { EventSource, FlushResult, IdentifyTraits, InitOptions, TrackEvent, } from "./types.js";
4
- //# sourceMappingURL=index.d.ts.map
1
+ type EventSource = "browser" | "server" | "worker";
2
+ interface TrackEvent {
3
+ event: string;
4
+ properties?: Record<string, unknown>;
5
+ context?: Record<string, unknown>;
6
+ source?: EventSource;
7
+ occurred_at?: Date | string;
8
+ org_id?: string;
9
+ user_id?: string;
10
+ anonymous_id?: string;
11
+ session_id?: string;
12
+ }
13
+ interface IdentifyTraits {
14
+ orgId?: string;
15
+ orgName?: string;
16
+ email?: string;
17
+ name?: string;
18
+ [key: string]: unknown;
19
+ }
20
+ interface IdentifyOptions {
21
+ /**
22
+ * HMAC-SHA256(identityVerificationSecret, userId) in lowercase
23
+ * hex, computed by the host site's backend. Required for the
24
+ * support-ticket widget to authenticate the end-user — without
25
+ * it, ticket reads/writes fall back to the anonymous session
26
+ * channel (bootstrapped via `POST /ticket/session`).
27
+ *
28
+ * Retrieve the per-project `identityVerificationSecret` from the
29
+ * ScopeRat dashboard (Analytics → Project → Install). Never ship
30
+ * the secret to the browser — compute the hash server-side.
31
+ */
32
+ userHash?: string;
33
+ }
34
+ interface InitOptions {
35
+ endpoint?: string;
36
+ flushAt?: number;
37
+ flushInterval?: number;
38
+ timeout?: number;
39
+ onError?: (error: Error, events: TrackEvent[]) => void;
40
+ /** Session inactivity timeout in ms. Defaults to 30 minutes. */
41
+ sessionTimeout?: number;
42
+ /** Automatically capture uncaught errors and unhandled rejections. Defaults to false. */
43
+ captureErrors?: boolean;
44
+ /** Environment name for feature flag evaluation (e.g. "production", "development"). */
45
+ environment?: string;
46
+ /** Polling interval for feature flag updates in ms. Defaults to 60 seconds. */
47
+ flagPollInterval?: number;
48
+ }
49
+ interface FlushResult {
50
+ success: boolean;
51
+ ingested: number;
52
+ }
53
+ interface EvaluatedFlag {
54
+ key: string;
55
+ value: unknown;
56
+ type: "boolean" | "string" | "number" | "json";
57
+ source: "default" | "override" | "targeted" | "rollout";
58
+ }
59
+
60
+ interface FlagClientConfig {
61
+ endpoint: string;
62
+ writeKey: string;
63
+ timeout?: number;
64
+ environment?: string;
65
+ pollInterval?: number;
66
+ getIdentity: () => {
67
+ userId?: string;
68
+ anonymousId?: string;
69
+ orgId?: string;
70
+ };
71
+ /** Called when a flag is first evaluated (for impression tracking). */
72
+ onEvaluation?: (key: string, flag: EvaluatedFlag) => void;
73
+ }
74
+ declare class FlagClient {
75
+ private endpoint;
76
+ private writeKey;
77
+ private timeout;
78
+ private environment;
79
+ private getIdentity;
80
+ private onEvaluation;
81
+ private cache;
82
+ private ready;
83
+ private readyCallbacks;
84
+ private changeListeners;
85
+ private evalTracked;
86
+ private timer;
87
+ constructor(config: FlagClientConfig);
88
+ getFlag<T = unknown>(key: string, defaultValue: T): T;
89
+ onFlagsReady(callback: () => void): void;
90
+ onFlagChange(listener: () => void): () => void;
91
+ refresh(): Promise<void>;
92
+ destroy(): void;
93
+ private fetch;
94
+ }
95
+
96
+ /** Why a session ended / began, attached as the event's `reason` property. */
97
+ type SessionReason = "initial" | "login" | "logout" | "timeout";
98
+ interface StoredTicketSession {
99
+ anonymousId: string;
100
+ token: string;
101
+ expiresAt: number;
102
+ }
103
+ declare class TrackerImpl {
104
+ private writeKey;
105
+ private endpoint;
106
+ private flushAt;
107
+ private flushInterval;
108
+ private timeout;
109
+ private onError?;
110
+ private buffer;
111
+ private timer;
112
+ private visibilityHandler;
113
+ private userId;
114
+ private orgId;
115
+ private traits;
116
+ private userHash;
117
+ private anonymousId;
118
+ private sessionId;
119
+ private sessionTimeout;
120
+ private ticketSessionFetch;
121
+ private _flagClient;
122
+ init(writeKey: string, options?: InitOptions): void;
123
+ /** The underlying flag evaluation client. Use for direct SDK integration. */
124
+ get flags(): FlagClient;
125
+ identify(userId: string, traits?: IdentifyTraits, options?: IdentifyOptions): void;
126
+ reset(): void;
127
+ /**
128
+ * Begin a new analytics session, ending the current one first if it
129
+ * exists. Emits `$session_end` (for the old session) and
130
+ * `$session_start` (for the new). Called automatically on login via
131
+ * {@link identify}; also exposed for explicit control.
132
+ */
133
+ startSession(reason?: SessionReason): string | undefined;
134
+ /**
135
+ * End the current analytics session (if any), emitting `$session_end`,
136
+ * and clear the stored session. The next tracked event will lazily
137
+ * start a fresh session. Called automatically on logout via
138
+ * {@link reset}; also exposed for explicit control.
139
+ *
140
+ * @param immediate - flush buffered events synchronously (via
141
+ * `sendBeacon`) so the end event isn't lost if the page unloads.
142
+ */
143
+ endSession(reason?: SessionReason, immediate?: boolean): void;
144
+ private readStoredUserId;
145
+ private writeStoredUserId;
146
+ private clearStoredUserId;
147
+ private clearTicketSession;
148
+ private readStoredTicketSession;
149
+ /**
150
+ * @internal Used by the widget API helpers. Returns a cached or
151
+ * newly-minted anonymous session token, or `null` if the server
152
+ * has anonymous sessions disabled. Coalesces concurrent callers
153
+ * onto a single in-flight network request so mount-time parallel
154
+ * API calls don't each trigger a `POST /ticket/session`.
155
+ */
156
+ _ensureTicketSession(): Promise<StoredTicketSession | null>;
157
+ /**
158
+ * Return the id of the current session, rotating it when the previous
159
+ * session has expired through inactivity. On rotation it emits a
160
+ * `$session_end` for the timed-out session (backdated to the last
161
+ * recorded activity) and a `$session_start` for the new one. Called on
162
+ * every tracked event so sessions advance with real user activity.
163
+ */
164
+ private ensureSession;
165
+ private readStoredSession;
166
+ private readLastActivity;
167
+ private persistSession;
168
+ /**
169
+ * Enqueue a session lifecycle event already stamped with its session id
170
+ * (so it never re-enters {@link ensureSession} and risks recursion).
171
+ */
172
+ private emitSessionEvent;
173
+ private clearSession;
174
+ getSessionId(): string | undefined;
175
+ /** @internal Used by the widget to read tracker config. */
176
+ _getEndpoint(): string;
177
+ /** @internal Used by the widget to read the write key. */
178
+ _getWriteKey(): string | null;
179
+ /** @internal Used by the widget to read the current user identity. */
180
+ _getIdentity(): {
181
+ userId?: string;
182
+ anonymousId?: string;
183
+ email?: string;
184
+ name?: string;
185
+ userHash?: string;
186
+ };
187
+ captureException(error: Error, context?: Record<string, unknown>): void;
188
+ addBreadcrumb(category: string, message: string, data?: Record<string, unknown>): void;
189
+ track(event: string, properties?: Record<string, unknown>): void;
190
+ track(event: TrackEvent): void;
191
+ flush(): Promise<FlushResult>;
192
+ private flushSync;
193
+ getFlag<T = unknown>(key: string, defaultValue: T): T;
194
+ onFlagsReady(callback: () => void): void;
195
+ onFlagChange(listener: () => void): () => void;
196
+ refreshFlags(): Promise<void>;
197
+ shutdown(): Promise<void>;
198
+ }
199
+ declare const scoperat: TrackerImpl;
200
+
201
+ export { type EvaluatedFlag, type EventSource, FlagClient, type FlushResult, type IdentifyOptions, type IdentifyTraits, type InitOptions, type TrackEvent, scoperat as default };