vident-rum 0.8.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.
Files changed (46) hide show
  1. package/README.md +424 -0
  2. package/dist/client.d.ts +67 -0
  3. package/dist/client.d.ts.map +1 -0
  4. package/dist/client.js +207 -0
  5. package/dist/client.js.map +1 -0
  6. package/dist/errors.d.ts +8 -0
  7. package/dist/errors.d.ts.map +1 -0
  8. package/dist/errors.js +67 -0
  9. package/dist/errors.js.map +1 -0
  10. package/dist/events.d.ts +78 -0
  11. package/dist/events.d.ts.map +1 -0
  12. package/dist/events.js +11 -0
  13. package/dist/events.js.map +1 -0
  14. package/dist/index.d.ts +5 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +3 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/instrumentation/fetch.d.ts +16 -0
  19. package/dist/instrumentation/fetch.d.ts.map +1 -0
  20. package/dist/instrumentation/fetch.js +109 -0
  21. package/dist/instrumentation/fetch.js.map +1 -0
  22. package/dist/instrumentation/xhr.d.ts +16 -0
  23. package/dist/instrumentation/xhr.d.ts.map +1 -0
  24. package/dist/instrumentation/xhr.js +129 -0
  25. package/dist/instrumentation/xhr.js.map +1 -0
  26. package/dist/replay.d.ts +66 -0
  27. package/dist/replay.d.ts.map +1 -0
  28. package/dist/replay.js +289 -0
  29. package/dist/replay.js.map +1 -0
  30. package/dist/session.d.ts +11 -0
  31. package/dist/session.d.ts.map +1 -0
  32. package/dist/session.js +176 -0
  33. package/dist/session.js.map +1 -0
  34. package/dist/trace-context.d.ts +37 -0
  35. package/dist/trace-context.d.ts.map +1 -0
  36. package/dist/trace-context.js +113 -0
  37. package/dist/trace-context.js.map +1 -0
  38. package/dist/transport.d.ts +15 -0
  39. package/dist/transport.d.ts.map +1 -0
  40. package/dist/transport.js +101 -0
  41. package/dist/transport.js.map +1 -0
  42. package/dist/vitals.d.ts +12 -0
  43. package/dist/vitals.d.ts.map +1 -0
  44. package/dist/vitals.js +48 -0
  45. package/dist/vitals.js.map +1 -0
  46. package/package.json +34 -0
package/README.md ADDED
@@ -0,0 +1,424 @@
1
+ # @spanwise/rum
2
+
3
+ Real User Monitoring SDK for browser applications. Captures page views, clicks, errors, Web Vitals, and session replays.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @spanwise/rum
9
+ # or
10
+ pnpm add @spanwise/rum
11
+ # or
12
+ yarn add @spanwise/rum
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { createSpanwiseBrowser } from "@spanwise/rum"
19
+
20
+ const spanwise = createSpanwiseBrowser({
21
+ apiKey: "sw_...",
22
+ serviceName: "my-app",
23
+ })
24
+
25
+ // That's it! Page views, clicks, errors, and vitals are tracked automatically.
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ ```typescript
31
+ const spanwise = createSpanwiseBrowser({
32
+ // Required
33
+ apiKey: "sw_...",
34
+
35
+ // Optional
36
+ serviceName: "my-app", // Service name for filtering
37
+ sampleRate: 1.0, // Sample 100% of sessions (default: 1.0)
38
+ trackPageViews: true, // Track page views (default: true)
39
+ trackClicks: true, // Track clicks (default: true)
40
+ trackVitals: true, // Track Web Vitals (default: true)
41
+ trackErrors: true, // Track errors (default: true)
42
+ trackResources: true, // Track fetch/XHR requests (default: true)
43
+ sessionStorage: "cookie", // "cookie" or "sessionStorage" (default: "cookie")
44
+
45
+ // Distributed Tracing
46
+ tracing: {
47
+ enabled: true, // Inject traceparent headers (default: true)
48
+ propagateToOrigins: [ // Origins to propagate trace context to
49
+ "https://api.example.com",
50
+ /\.example\.com$/,
51
+ ],
52
+ },
53
+
54
+ // Session Replay (opt-in)
55
+ replay: {
56
+ enabled: true, // Enable replay recording (default: false)
57
+ sampleRate: 0.1, // Record 10% of sessions (default: 0.1)
58
+ onErrorSampleRate: 1.0, // Record 100% of sessions with errors (default: 1.0)
59
+ privacyMode: "strict", // Privacy mode (default: "strict")
60
+ },
61
+ })
62
+ ```
63
+
64
+ ## API
65
+
66
+ ### `spanwise.trackEvent(name, properties?)`
67
+
68
+ Track a custom event.
69
+
70
+ ```typescript
71
+ spanwise.trackEvent("purchase", {
72
+ productId: "123",
73
+ amount: 99.99,
74
+ })
75
+ ```
76
+
77
+ ### `spanwise.trackPageView(url?)`
78
+
79
+ Manually track a page view. Called automatically on navigation.
80
+
81
+ ```typescript
82
+ spanwise.trackPageView("/checkout")
83
+ ```
84
+
85
+ ### `spanwise.trackError(error, options?)`
86
+
87
+ Manually track an error.
88
+
89
+ ```typescript
90
+ try {
91
+ await riskyOperation()
92
+ } catch (error) {
93
+ spanwise.trackError(error, {
94
+ requestId: "req_123",
95
+ traceId: "trace_abc",
96
+ })
97
+ }
98
+ ```
99
+
100
+ ### `spanwise.setUser(id, traits?)`
101
+
102
+ Identify the current user.
103
+
104
+ ```typescript
105
+ spanwise.setUser("user_123", {
106
+ email: "user@example.com",
107
+ plan: "pro",
108
+ })
109
+ ```
110
+
111
+ ### `spanwise.getSessionId()`
112
+
113
+ Get the current session ID.
114
+
115
+ ```typescript
116
+ const sessionId = spanwise.getSessionId()
117
+ ```
118
+
119
+ ### `spanwise.forceReplayUpload()`
120
+
121
+ Force replay upload for specific users (bypasses sampling).
122
+
123
+ ```typescript
124
+ // Force upload for VIP users regardless of sample rate
125
+ if (user.isVIP) {
126
+ spanwise.forceReplayUpload()
127
+ }
128
+ ```
129
+
130
+ ### `spanwise.stopReplay()`
131
+
132
+ Stop replay recording entirely.
133
+
134
+ ### `spanwise.isReplayUploading()`
135
+
136
+ Check if replay is currently uploading to server (vs buffering in memory).
137
+
138
+ ---
139
+
140
+ ## Session Replay
141
+
142
+ Session Replay records user interactions as a video-like playback. It captures DOM mutations, mouse movements, clicks, and scrolls.
143
+
144
+ ### Enabling Replay
145
+
146
+ ```typescript
147
+ const spanwise = createSpanwiseBrowser({
148
+ apiKey: "sw_...",
149
+ replay: {
150
+ enabled: true,
151
+ },
152
+ })
153
+ ```
154
+
155
+ ### How It Works: Rolling Buffer
156
+
157
+ Session Replay uses a **rolling buffer architecture** (similar to Sentry):
158
+
159
+ 1. **Always recording** - When enabled, the SDK always records to a 60-second memory buffer
160
+ 2. **Sampled sessions** (default 10%) - Upload immediately to server
161
+ 3. **Non-sampled sessions** (90%) - Keep buffer in memory, zero network cost
162
+ 4. **On error** - Flush the 60-second buffer and start uploading
163
+
164
+ This means you **always capture what happened before an error**, not just after.
165
+
166
+ ### Sampling
167
+
168
+ ```typescript
169
+ replay: {
170
+ enabled: true,
171
+ sampleRate: 0.1, // 10% upload immediately
172
+ onErrorSampleRate: 1.0, // 100% upload on error (includes 60s buffer)
173
+ }
174
+ ```
175
+
176
+ | Scenario | What's Captured |
177
+ |----------|-----------------|
178
+ | Sampled (10%) | Full session from start |
179
+ | Not sampled + error | 60 seconds before error + everything after |
180
+ | Not sampled + no error | Nothing sent (zero network cost) |
181
+
182
+ ---
183
+
184
+ ## Privacy & Data Protection
185
+
186
+ Session Replay is designed with privacy as the default. **No configuration is required for most applications** - sensitive data is automatically protected.
187
+
188
+ ### Privacy Modes
189
+
190
+ | Mode | Text | Inputs | Use Case |
191
+ |------|------|--------|----------|
192
+ | `"strict"` (default) | Masked | Masked | Most applications |
193
+ | `"balanced"` | Visible | Masked | Marketing sites, blogs |
194
+ | `"permissive"` | Visible | Visible | Internal tools only |
195
+
196
+ ```typescript
197
+ replay: {
198
+ enabled: true,
199
+ privacyMode: "strict", // Default - masks everything
200
+ }
201
+ ```
202
+
203
+ ### What Each Mode Does
204
+
205
+ #### Strict Mode (Default)
206
+
207
+ All text content is replaced with `****`. Form inputs show `•••••`. You see the page structure and user interactions, but no actual content.
208
+
209
+ ```
210
+ ┌─────────────────────────────┐
211
+ │ **** ******** │ ← Masked heading
212
+ │ │
213
+ │ ******** **** ** ******* │ ← Masked paragraph
214
+ │ ******* ** *** **** │
215
+ │ │
216
+ │ Email: [•••••••••••••] │ ← Masked input
217
+ │ Password: [••••••••] │
218
+ │ │
219
+ │ [**********] │ ← Masked button
220
+ └─────────────────────────────┘
221
+ ```
222
+
223
+ #### Balanced Mode
224
+
225
+ Text is visible, but all form inputs are masked. Good for content-heavy sites.
226
+
227
+ ```
228
+ ┌─────────────────────────────┐
229
+ │ Welcome Back │ ← Visible heading
230
+ │ │
231
+ │ Please sign in to continue │ ← Visible text
232
+ │ │
233
+ │ Email: [•••••••••••••] │ ← Masked input
234
+ │ Password: [••••••••] │
235
+ │ │
236
+ │ [Sign In] │ ← Visible button
237
+ └─────────────────────────────┘
238
+ ```
239
+
240
+ #### Permissive Mode
241
+
242
+ Everything is recorded as-is. **Only use for internal tools or with explicit user consent.**
243
+
244
+ ### Fine-Grained Control with HTML Attributes
245
+
246
+ Regardless of privacy mode, you can control specific elements:
247
+
248
+ #### Block Elements Completely
249
+
250
+ Use `data-sw-block` to completely hide an element. It will appear as a gray placeholder in replays.
251
+
252
+ ```html
253
+ <!-- Credit card form - never record -->
254
+ <div data-sw-block>
255
+ <input type="text" placeholder="Card number" />
256
+ <input type="text" placeholder="CVV" />
257
+ </div>
258
+
259
+ <!-- Sensitive user data -->
260
+ <div data-sw-block class="user-profile">
261
+ <p>SSN: 123-45-6789</p>
262
+ </div>
263
+ ```
264
+
265
+ #### Mask Specific Elements
266
+
267
+ Use `data-sw-mask` to mask text while preserving structure (useful in balanced/permissive modes).
268
+
269
+ ```html
270
+ <!-- Mask email in a visible section -->
271
+ <p>Contact: <span data-sw-mask>user@example.com</span></p>
272
+ ```
273
+
274
+ #### Unmask Elements in Strict Mode
275
+
276
+ Use `data-sw-unmask` to show content even when using strict mode.
277
+
278
+ ```html
279
+ <!-- Show navigation labels in strict mode -->
280
+ <nav data-sw-unmask>
281
+ <a href="/">Home</a>
282
+ <a href="/products">Products</a>
283
+ <a href="/about">About</a>
284
+ </nav>
285
+
286
+ <!-- Show static content -->
287
+ <footer data-sw-unmask>
288
+ <p>© 2024 My Company</p>
289
+ </footer>
290
+ ```
291
+
292
+ #### rrweb Classes (Alternative)
293
+
294
+ The standard rrweb classes also work:
295
+
296
+ ```html
297
+ <div class="rr-block">Hidden content</div>
298
+ <div class="rr-mask">Masked content</div>
299
+ ```
300
+
301
+ ### Best Practices
302
+
303
+ 1. **Start with strict mode** - It's the safest default. Only relax if needed.
304
+
305
+ 2. **Block sensitive sections entirely** - For areas with PII (profile pages, account settings), use `data-sw-block`.
306
+
307
+ 3. **Review before production** - Test your app with replay enabled and verify no sensitive data appears.
308
+
309
+ 4. **Consider user consent** - For GDPR compliance, inform users that sessions may be recorded.
310
+
311
+ 5. **Use unmask sparingly** - Only unmask truly static, non-sensitive content like navigation labels.
312
+
313
+ ### What's Automatically Protected
314
+
315
+ Even without configuration:
316
+
317
+ - All `<input>` values are masked (except in permissive mode)
318
+ - Password fields are always masked
319
+ - Credit card patterns are detected and masked
320
+ - `<script>` content is never recorded
321
+ - `<head>` metadata is stripped
322
+
323
+ ### Sensitive Data Checklist
324
+
325
+ Before enabling replay, ensure these are blocked or masked:
326
+
327
+ - [ ] User profile information (name, email, phone)
328
+ - [ ] Payment forms and credit card inputs
329
+ - [ ] Social security numbers, government IDs
330
+ - [ ] Medical or health information
331
+ - [ ] Authentication tokens displayed on screen
332
+ - [ ] Private messages or chat content
333
+ - [ ] Financial account numbers
334
+ - [ ] Any PII in confirmation pages
335
+
336
+ ---
337
+
338
+ ## Distributed Tracing
339
+
340
+ The SDK automatically injects `traceparent` headers into fetch/XHR requests, connecting frontend sessions to backend traces.
341
+
342
+ ```typescript
343
+ const spanwise = createSpanwiseBrowser({
344
+ apiKey: "sw_...",
345
+ tracing: {
346
+ enabled: true,
347
+ propagateToOrigins: [
348
+ "https://api.myapp.com",
349
+ /\.myapp\.com$/,
350
+ ],
351
+ },
352
+ })
353
+ ```
354
+
355
+ Your backend will receive headers like:
356
+
357
+ ```
358
+ traceparent: 00-{traceId}-{spanId}-01
359
+ ```
360
+
361
+ ---
362
+
363
+ ## Browser Support
364
+
365
+ - Chrome 64+
366
+ - Firefox 67+
367
+ - Safari 12+
368
+ - Edge 79+
369
+
370
+ Session Replay requires:
371
+ - MutationObserver
372
+ - WeakMap
373
+ - requestAnimationFrame
374
+
375
+ ---
376
+
377
+ ## Bundle Size
378
+
379
+ The base SDK is ~8KB gzipped. Session Replay adds ~40KB gzipped (rrweb).
380
+
381
+ Replay is dynamically imported only when enabled, so it won't affect your bundle if not used.
382
+
383
+ ---
384
+
385
+ ## TypeScript
386
+
387
+ Full TypeScript support with exported types:
388
+
389
+ ```typescript
390
+ import {
391
+ createSpanwiseBrowser,
392
+ type BrowserConfig,
393
+ type SpanwiseBrowser,
394
+ type ReplayConfig,
395
+ type PrivacyMode,
396
+ type TracingConfig,
397
+ } from "@spanwise/rum"
398
+ ```
399
+
400
+ ---
401
+
402
+ ## Troubleshooting
403
+
404
+ ### Replay not recording
405
+
406
+ 1. Check that `replay.enabled` is `true`
407
+ 2. Verify you're within the sample rate (try `sampleRate: 1.0` for testing)
408
+ 3. Check browser console for errors
409
+
410
+ ### High data volume
411
+
412
+ 1. Reduce `sampleRate` (e.g., `0.05` for 5%)
413
+ 2. Use `"strict"` privacy mode (smaller payloads due to masked content)
414
+
415
+ ### Missing trace correlation
416
+
417
+ 1. Ensure `tracing.propagateToOrigins` includes your API domain
418
+ 2. Check that your backend parses the `traceparent` header
419
+
420
+ ---
421
+
422
+ ## License
423
+
424
+ MIT
@@ -0,0 +1,67 @@
1
+ import { type ReplayConfig } from "./replay.js";
2
+ import { type StorageMode } from "./session.js";
3
+ export type TracingConfig = {
4
+ /** Enable distributed tracing (default: true) */
5
+ enabled?: boolean;
6
+ /** Origins to propagate trace context to (default: same-origin only) */
7
+ propagateToOrigins?: (string | RegExp)[];
8
+ /** Trace fetch requests (default: true) */
9
+ traceFetch?: boolean;
10
+ /** Trace XMLHttpRequest (default: true) */
11
+ traceXHR?: boolean;
12
+ };
13
+ export type BrowserConfig = {
14
+ apiKey: string;
15
+ appName?: string;
16
+ baseUrl?: string;
17
+ sampleRate?: number;
18
+ trackClicks?: boolean;
19
+ trackPageViews?: boolean;
20
+ trackVitals?: boolean;
21
+ trackErrors?: boolean;
22
+ /** Track HTTP resources (fetch/XHR) with timing (default: true) */
23
+ trackResources?: boolean;
24
+ /** Distributed tracing configuration */
25
+ tracing?: TracingConfig;
26
+ /**
27
+ * Session storage mode (default: "cookie")
28
+ * - "cookie": Uses cookies with localStorage fallback. Survives OAuth redirects.
29
+ * Requires cookie consent in GDPR regions.
30
+ * - "sessionStorage": Original behavior. Tab-isolated, cleared on tab close.
31
+ * No cookie consent required.
32
+ */
33
+ sessionStorage?: StorageMode;
34
+ /** Session replay configuration */
35
+ replay?: ReplayConfig;
36
+ };
37
+ export declare function createVidentBrowser(config: BrowserConfig): {
38
+ trackEvent: () => void;
39
+ trackPageView: () => void;
40
+ trackError: () => void;
41
+ setUser: () => void;
42
+ getSessionId: () => null;
43
+ startReplay: () => void;
44
+ stopReplay: () => void;
45
+ /** Force replay upload (e.g., for VIP users) - overrides sampling */
46
+ forceReplayUpload?: undefined;
47
+ /** Check if replay is currently uploading to server */
48
+ isReplayUploading?: undefined;
49
+ } | {
50
+ trackEvent: (name: string, properties?: Record<string, unknown>) => void;
51
+ trackPageView: (url?: string) => void;
52
+ trackError: (error: Error, options?: {
53
+ requestId?: string;
54
+ traceId?: string;
55
+ }) => void;
56
+ setUser: (id: string, traits?: Record<string, unknown>) => void;
57
+ getSessionId: () => string;
58
+ /** Force replay upload (e.g., for VIP users) - overrides sampling */
59
+ forceReplayUpload: () => void | undefined;
60
+ /** Stop replay recording */
61
+ stopReplay: () => void | undefined;
62
+ /** Check if replay is currently uploading to server */
63
+ isReplayUploading: () => boolean;
64
+ startReplay?: undefined;
65
+ };
66
+ export type VidentBrowser = ReturnType<typeof createVidentBrowser>;
67
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,YAAY,EAAwB,MAAM,aAAa,CAAA;AACrE,OAAO,EACN,KAAK,WAAW,EAIhB,MAAM,cAAc,CAAA;AAIrB,MAAM,MAAM,aAAa,GAAG;IAC3B,iDAAiD;IACjD,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,wEAAwE;IACxE,kBAAkB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;IACxC,2CAA2C;IAC3C,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,mEAAmE;IACnE,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,wCAAwC;IACxC,OAAO,CAAC,EAAE,aAAa,CAAA;IACvB;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,WAAW,CAAA;IAC5B,mCAAmC;IACnC,MAAM,CAAC,EAAE,YAAY,CAAA;CACrB,CAAA;AAmBD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,aAAa;;;;;;;;IA2NvD,qEAAqE;;IAIrE,uDAAuD;;;uBAxKjD,MAAM,eACC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAClC,IAAI;0BAhBsB,MAAM,KAAG,IAAI;wBA6BlC,KAAK,YACF;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,KAChD,IAAI;kBAOc,MAAM,WAAW,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,IAAI;wBAWtC,MAAM;IAiInC,qEAAqE;;IAErE,4BAA4B;;IAE5B,uDAAuD;;;EAGxD;AAED,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
package/dist/client.js ADDED
@@ -0,0 +1,207 @@
1
+ import { captureError, setupErrorTracking } from "./errors.js";
2
+ import { createBaseEvent } from "./events.js";
3
+ import { instrumentFetch, updateFetchConfig } from "./instrumentation/fetch.js";
4
+ import { instrumentXHR, updateXHRConfig } from "./instrumentation/xhr.js";
5
+ import { createReplayRecorder } from "./replay.js";
6
+ import { getOrCreateSession, refreshSession, setStorageMode, } from "./session.js";
7
+ import { createTransport } from "./transport.js";
8
+ import { setupVitalsTracking } from "./vitals.js";
9
+ const DEFAULT_BASE_URL = "https://api.vident.dev";
10
+ function getClickTarget(element) {
11
+ if (element.id)
12
+ return `#${element.id}`;
13
+ const tag = element.tagName.toLowerCase();
14
+ const classes = Array.from(element.classList).slice(0, 3).join(".");
15
+ if (classes)
16
+ return `${tag}.${classes}`;
17
+ return tag;
18
+ }
19
+ function getClickText(element) {
20
+ const text = element.textContent?.trim().slice(0, 50);
21
+ return text || undefined;
22
+ }
23
+ export function createVidentBrowser(config) {
24
+ // Apply sampling
25
+ const sampleRate = config.sampleRate ?? 1;
26
+ if (Math.random() > sampleRate) {
27
+ // Return no-op client
28
+ return {
29
+ trackEvent: () => { },
30
+ trackPageView: () => { },
31
+ trackError: () => { },
32
+ setUser: () => { },
33
+ getSessionId: () => null,
34
+ startReplay: () => { },
35
+ stopReplay: () => { },
36
+ };
37
+ }
38
+ const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
39
+ setStorageMode(config.sessionStorage ?? "cookie");
40
+ const { sessionId } = getOrCreateSession();
41
+ let userId;
42
+ const transport = createTransport({
43
+ apiKey: config.apiKey,
44
+ baseUrl,
45
+ appName: config.appName,
46
+ });
47
+ // Initialize replay recorder if configured
48
+ const replayRecorder = config.replay?.enabled
49
+ ? createReplayRecorder(config.replay, {
50
+ apiKey: config.apiKey,
51
+ baseUrl,
52
+ sessionId,
53
+ })
54
+ : null;
55
+ function enqueueEvent(event) {
56
+ refreshSession();
57
+ transport.enqueue(event);
58
+ }
59
+ function trackPageView(url) {
60
+ const event = {
61
+ ...createBaseEvent("page_view", sessionId, userId),
62
+ type: "page_view",
63
+ url: url ?? window.location.href,
64
+ data: {
65
+ referrer: document.referrer || undefined,
66
+ title: document.title,
67
+ },
68
+ };
69
+ enqueueEvent(event);
70
+ }
71
+ function trackEvent(name, properties) {
72
+ const event = {
73
+ ...createBaseEvent("custom", sessionId, userId),
74
+ type: "custom",
75
+ data: {
76
+ name,
77
+ properties,
78
+ },
79
+ };
80
+ enqueueEvent(event);
81
+ }
82
+ function trackError(error, options) {
83
+ const event = captureError(error, sessionId, userId, options);
84
+ enqueueEvent(event);
85
+ // Mark replay for error correlation
86
+ replayRecorder?.markError();
87
+ }
88
+ function setUser(id, traits) {
89
+ userId = id;
90
+ // Update instrumentation configs with new userId
91
+ updateFetchConfig(userId);
92
+ updateXHRConfig(userId);
93
+ // Optionally track user identification as custom event
94
+ if (traits) {
95
+ trackEvent("user_identified", { userId: id, ...traits });
96
+ }
97
+ }
98
+ function getSessionIdValue() {
99
+ return sessionId;
100
+ }
101
+ // Auto-tracking setup
102
+ if (config.trackPageViews !== false) {
103
+ // Track initial page view
104
+ trackPageView();
105
+ // Track SPA navigation
106
+ const originalPushState = history.pushState.bind(history);
107
+ const originalReplaceState = history.replaceState.bind(history);
108
+ history.pushState = (...args) => {
109
+ originalPushState(...args);
110
+ trackPageView();
111
+ };
112
+ history.replaceState = (...args) => {
113
+ originalReplaceState(...args);
114
+ trackPageView();
115
+ };
116
+ window.addEventListener("popstate", () => {
117
+ trackPageView();
118
+ });
119
+ }
120
+ if (config.trackClicks !== false) {
121
+ document.addEventListener("click", (event) => {
122
+ const target = event.target;
123
+ if (!target)
124
+ return;
125
+ // Only track interactive elements
126
+ const interactiveElements = [
127
+ "A",
128
+ "BUTTON",
129
+ "INPUT",
130
+ "SELECT",
131
+ "TEXTAREA",
132
+ ];
133
+ const isInteractive = interactiveElements.includes(target.tagName) ||
134
+ target.closest("a, button") !== null ||
135
+ target.getAttribute("role") === "button";
136
+ if (!isInteractive)
137
+ return;
138
+ const clickEvent = {
139
+ ...createBaseEvent("click", sessionId, userId),
140
+ type: "click",
141
+ data: {
142
+ target: getClickTarget(target),
143
+ text: getClickText(target),
144
+ x: event.clientX,
145
+ y: event.clientY,
146
+ },
147
+ };
148
+ enqueueEvent(clickEvent);
149
+ }, { capture: true });
150
+ }
151
+ if (config.trackVitals !== false) {
152
+ setupVitalsTracking(sessionId, userId, (event) => {
153
+ enqueueEvent(event);
154
+ }, () => {
155
+ transport.flushBeacon();
156
+ });
157
+ }
158
+ if (config.trackErrors !== false) {
159
+ setupErrorTracking(sessionId, userId, (event) => {
160
+ enqueueEvent(event);
161
+ // Mark replay for error correlation (auto-captured errors)
162
+ replayRecorder?.markError();
163
+ });
164
+ }
165
+ // Resource tracking: capture HTTP requests with timing and trace correlation
166
+ // - trackResources: store resource events in RUM (default: true)
167
+ // - tracing.enabled: inject traceparent headers for backend correlation (default: true)
168
+ // If either is enabled, we instrument fetch/XHR
169
+ const shouldInstrument = config.trackResources !== false || config.tracing?.enabled !== false;
170
+ const shouldStoreResources = config.trackResources !== false;
171
+ if (shouldInstrument) {
172
+ const ingestUrl = `${baseUrl}/v1/ingest`;
173
+ const instrumentationConfig = {
174
+ propagateToOrigins: config.tracing?.propagateToOrigins,
175
+ sessionId,
176
+ userId,
177
+ ingestUrl,
178
+ };
179
+ const onResource = shouldStoreResources
180
+ ? (event) => enqueueEvent(event)
181
+ : () => { }; // traceparent injected but events not stored
182
+ // Capture network errors with URL context (only if error tracking enabled)
183
+ const onNetworkError = config.trackErrors !== false
184
+ ? (event) => enqueueEvent(event)
185
+ : undefined;
186
+ if (config.tracing?.traceFetch !== false) {
187
+ instrumentFetch(instrumentationConfig, onResource, onNetworkError);
188
+ }
189
+ if (config.tracing?.traceXHR !== false) {
190
+ instrumentXHR(instrumentationConfig, onResource, onNetworkError);
191
+ }
192
+ }
193
+ return {
194
+ trackEvent,
195
+ trackPageView,
196
+ trackError,
197
+ setUser,
198
+ getSessionId: getSessionIdValue,
199
+ /** Force replay upload (e.g., for VIP users) - overrides sampling */
200
+ forceReplayUpload: () => replayRecorder?.startUploading(),
201
+ /** Stop replay recording */
202
+ stopReplay: () => replayRecorder?.stop(),
203
+ /** Check if replay is currently uploading to server */
204
+ isReplayUploading: () => replayRecorder?.isUploading() ?? false,
205
+ };
206
+ }
207
+ //# sourceMappingURL=client.js.map