@powerit/cms-tracker 1.0.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.
@@ -0,0 +1,486 @@
1
+ /**
2
+ * PowerIT CMS Tracker TypeScript Definitions
3
+ */
4
+
5
+ // ============================================================================
6
+ // Logger Interface
7
+ // ============================================================================
8
+
9
+ /**
10
+ * Logger interface for tracker
11
+ */
12
+ export interface TrackerLogger {
13
+ debug: (...args: unknown[]) => void;
14
+ info: (...args: unknown[]) => void;
15
+ warn: (...args: unknown[]) => void;
16
+ error: (...args: unknown[]) => void;
17
+ }
18
+
19
+ // ============================================================================
20
+ // Privacy Configuration
21
+ // ============================================================================
22
+
23
+ /**
24
+ * Input masking options
25
+ */
26
+ export interface MaskInputOptions {
27
+ /** Mask password inputs (default: true) */
28
+ password?: boolean;
29
+ /** Mask email inputs (default: true) */
30
+ email?: boolean;
31
+ /** Mask telephone inputs (default: true) */
32
+ tel?: boolean;
33
+ /** Mask credit card inputs (default: true) */
34
+ creditCard?: boolean;
35
+ /** Mask regular text inputs (default: false) */
36
+ text?: boolean;
37
+ /** Mask textareas (default: false) */
38
+ textarea?: boolean;
39
+ /** Mask select elements (default: false) */
40
+ select?: boolean;
41
+ }
42
+
43
+ /**
44
+ * Privacy configuration for session recording
45
+ */
46
+ export interface TrackerPrivacyConfig {
47
+ /** Respect browser's Do Not Track setting (default: true) */
48
+ respectDoNotTrack?: boolean;
49
+
50
+ /** CSS class for blocked elements - replaced with placeholder (default: 'rr-block') */
51
+ blockClass?: string;
52
+
53
+ /** CSS selector for blocked elements */
54
+ blockSelector?: string;
55
+
56
+ /** CSS class for ignored elements - no input recording (default: 'rr-ignore') */
57
+ ignoreClass?: string;
58
+
59
+ /** CSS selector for ignored elements */
60
+ ignoreSelector?: string;
61
+
62
+ /** CSS class for masked text (default: 'rr-mask') */
63
+ maskTextClass?: string;
64
+
65
+ /** CSS selector for masked text */
66
+ maskTextSelector?: string;
67
+
68
+ /** Mask all input values (default: true for privacy) */
69
+ maskAllInputs?: boolean;
70
+
71
+ /** Specific input types to mask */
72
+ maskInputOptions?: MaskInputOptions;
73
+
74
+ /** Custom input masking function */
75
+ maskInputFn?: (text: string, element: HTMLElement) => string;
76
+
77
+ /** Custom text masking function */
78
+ maskTextFn?: (text: string, element: HTMLElement) => string;
79
+
80
+ /** CSS attributes to ignore */
81
+ ignoreCSSAttributes?: string[];
82
+
83
+ /** Block recording on specific pages (string prefix or regex patterns) */
84
+ blockedPages?: (string | RegExp)[];
85
+
86
+ /** Only record on specific pages (string prefix or regex patterns) */
87
+ allowedPages?: (string | RegExp)[];
88
+ }
89
+
90
+ // ============================================================================
91
+ // Sampling Configuration
92
+ // ============================================================================
93
+
94
+ /**
95
+ * Sampling configuration to reduce data volume
96
+ */
97
+ export interface TrackerSamplingConfig {
98
+ /** Enable sampling (default: false) */
99
+ enabled?: boolean;
100
+
101
+ /** Percentage of sessions to record (0-100, default: 100) */
102
+ sessionRate?: number;
103
+
104
+ /** Mouse movement sampling (events per second, default: 50) */
105
+ mousemove?: number | boolean;
106
+
107
+ /** Mouse interaction sampling (default: true) */
108
+ mouseInteraction?: boolean;
109
+
110
+ /** Scroll event sampling (events per second, default: 10) */
111
+ scroll?: number | boolean;
112
+
113
+ /** Media interaction sampling (default: true) */
114
+ media?: boolean;
115
+
116
+ /** Input change sampling: 'last' captures only final value (default: 'last') */
117
+ input?: 'last' | boolean;
118
+ }
119
+
120
+ // ============================================================================
121
+ // Recording Configuration
122
+ // ============================================================================
123
+
124
+ /**
125
+ * DOM recording options
126
+ */
127
+ export interface TrackerRecordingConfig {
128
+ /** Record canvas elements (default: false) */
129
+ recordCanvas?: boolean;
130
+
131
+ /** Record cross-origin iframes (default: false) */
132
+ recordCrossOriginIframes?: boolean;
133
+
134
+ /** Inline stylesheets in events (default: true) */
135
+ inlineStylesheet?: boolean;
136
+
137
+ /** Inline images as data URLs (default: false) */
138
+ inlineImages?: boolean;
139
+
140
+ /** Collect fonts for replay (default: false) */
141
+ collectFonts?: boolean;
142
+
143
+ /** Slim DOM options to reduce event size */
144
+ slimDOMOptions?: {
145
+ script?: boolean;
146
+ comment?: boolean;
147
+ headFavicon?: boolean;
148
+ headWhitespace?: boolean;
149
+ headMetaDescKeywords?: boolean;
150
+ headMetaSocial?: boolean;
151
+ headMetaRobots?: boolean;
152
+ headMetaHttpEquiv?: boolean;
153
+ headMetaAuthorship?: boolean;
154
+ headMetaVerification?: boolean;
155
+ };
156
+ }
157
+
158
+ // ============================================================================
159
+ // Main Configuration
160
+ // ============================================================================
161
+
162
+ /**
163
+ * Session tracker configuration
164
+ */
165
+ export interface TrackerConfig {
166
+ /** Transport type: 'http' or 'grpc' (default: 'http') */
167
+ transport?: 'http' | 'grpc';
168
+
169
+ /** Custom transport URL (defaults to client baseUrl) */
170
+ transportUrl?: string;
171
+
172
+ /** Privacy settings */
173
+ privacy?: TrackerPrivacyConfig;
174
+
175
+ /** Sampling settings */
176
+ sampling?: TrackerSamplingConfig;
177
+
178
+ /** Recording settings */
179
+ recording?: TrackerRecordingConfig;
180
+
181
+ /** Batch size for sending events (default: 50) */
182
+ batchSize?: number;
183
+
184
+ /** Flush interval in ms (default: 5000) */
185
+ flushInterval?: number;
186
+
187
+ /** Maximum events to queue before forced flush (default: 500) */
188
+ maxQueueSize?: number;
189
+
190
+ /** Compress events before sending (default: true) */
191
+ compress?: boolean;
192
+
193
+ /** Checkout every N events for snapshot (default: 200) */
194
+ checkoutEveryNth?: number;
195
+
196
+ /** Checkout every N ms (default: 300000 = 5 min) */
197
+ checkoutEveryNms?: number;
198
+
199
+ /** Session timeout in ms (default: 1800000 = 30 min) */
200
+ sessionTimeout?: number;
201
+
202
+ /** Storage key prefix (default: 'powerit_tracker') */
203
+ storageKey?: string;
204
+
205
+ /**
206
+ * Enable logging
207
+ * - `true`: Use default console logger
208
+ * - `TrackerLogger`: Use custom logger
209
+ * - `false` or `undefined`: Disable logging (default)
210
+ */
211
+ logger?: boolean | TrackerLogger;
212
+
213
+ /** Error handler callback */
214
+ onError?: (error: Error) => void;
215
+ }
216
+
217
+ // ============================================================================
218
+ // Session Types
219
+ // ============================================================================
220
+
221
+ /**
222
+ * Session information
223
+ */
224
+ export interface TrackerSession {
225
+ /** Unique session ID */
226
+ sessionId: string;
227
+
228
+ /** Visitor ID (persistent across sessions) */
229
+ visitorId: string;
230
+
231
+ /** Session start timestamp */
232
+ startedAt: number;
233
+
234
+ /** Last activity timestamp */
235
+ lastActivityAt: number;
236
+
237
+ /** Page URL where session started */
238
+ entryPage: string;
239
+
240
+ /** Full entry URL */
241
+ entryUrl: string;
242
+
243
+ /** Total events recorded */
244
+ eventCount: number;
245
+
246
+ /** Whether session is linked to analytics plugin */
247
+ linkedToAnalytics: boolean;
248
+
249
+ /** Analytics session ID if linked */
250
+ analyticsSessionId: string | null;
251
+ }
252
+
253
+ // ============================================================================
254
+ // Compression Types
255
+ // ============================================================================
256
+
257
+ /**
258
+ * Compressed events container
259
+ */
260
+ export interface CompressedEvents {
261
+ /** Compression type: 'gzip' or 'none' */
262
+ type: 'gzip' | 'none';
263
+
264
+ /** Compressed or raw data */
265
+ data: string;
266
+
267
+ /** Original size in bytes */
268
+ originalSize: number;
269
+
270
+ /** Compressed size in bytes */
271
+ compressedSize: number;
272
+ }
273
+
274
+ /**
275
+ * Compression statistics
276
+ */
277
+ export interface CompressionStats {
278
+ /** Original size in bytes */
279
+ originalSize: number;
280
+
281
+ /** Compressed size in bytes */
282
+ compressedSize: number;
283
+
284
+ /** Compression ratio */
285
+ ratio: string;
286
+
287
+ /** Savings percentage */
288
+ savings: string;
289
+
290
+ /** Compression type used */
291
+ type: 'gzip' | 'none';
292
+ }
293
+
294
+ // ============================================================================
295
+ // Session Manager Class
296
+ // ============================================================================
297
+
298
+ /**
299
+ * Session Manager
300
+ * Manages visitor and session IDs with persistence
301
+ */
302
+ export declare class SessionManager {
303
+ constructor(options?: {
304
+ storageKey?: string;
305
+ sessionTimeout?: number;
306
+ });
307
+
308
+ /** Get or create a session */
309
+ getOrCreateSession(): TrackerSession;
310
+
311
+ /** Get current session */
312
+ getSession(): TrackerSession | null;
313
+
314
+ /** Get visitor ID */
315
+ getVisitorId(): string | null;
316
+
317
+ /** Get session ID */
318
+ getSessionId(): string | null;
319
+
320
+ /** Link to analytics session */
321
+ linkToAnalytics(analyticsTracker: unknown): void;
322
+
323
+ /** Update activity timestamp */
324
+ touch(): void;
325
+
326
+ /** Increment event count */
327
+ incrementEventCount(count?: number): void;
328
+
329
+ /** End the current session */
330
+ endSession(): void;
331
+
332
+ /** Check if session is valid */
333
+ isSessionValid(): boolean;
334
+ }
335
+
336
+ // ============================================================================
337
+ // Session Recorder Class
338
+ // ============================================================================
339
+
340
+ /**
341
+ * Session Recorder
342
+ * Records user sessions using rrweb
343
+ */
344
+ export declare class SessionRecorder {
345
+ constructor(client: unknown, config?: TrackerConfig);
346
+
347
+ /**
348
+ * Initialize the recorder
349
+ * Sets up transport, session, and event handlers
350
+ */
351
+ init(): Promise<SessionRecorder>;
352
+
353
+ /** Start recording */
354
+ start(): void;
355
+
356
+ /** Pause recording (keeps session active) */
357
+ pause(): void;
358
+
359
+ /** Resume recording */
360
+ resume(): void;
361
+
362
+ /** Stop recording and flush events */
363
+ stop(): Promise<void>;
364
+
365
+ /** Check if currently recording */
366
+ isRecording(): boolean;
367
+
368
+ /** Check if tracking is enabled */
369
+ isEnabled(): boolean;
370
+
371
+ /** Get current session info */
372
+ getSession(): TrackerSession | null;
373
+
374
+ /** Get visitor ID */
375
+ getVisitorId(): string | null;
376
+
377
+ /** Get session ID */
378
+ getSessionId(): string | null;
379
+
380
+ /** Flush pending events */
381
+ flush(): Promise<void>;
382
+
383
+ /**
384
+ * Add a custom event to the recording
385
+ * @param tag - Event tag/name
386
+ * @param payload - Event payload
387
+ */
388
+ addCustomEvent(tag: string, payload?: Record<string, unknown>): void;
389
+
390
+ /**
391
+ * Identify a user
392
+ * @param userId - User identifier
393
+ * @param traits - User traits/properties
394
+ */
395
+ identify(userId: string, traits?: Record<string, unknown>): void;
396
+
397
+ /** Take a full snapshot manually */
398
+ takeSnapshot(): void;
399
+
400
+ /**
401
+ * Update privacy settings at runtime
402
+ * @param privacy - Privacy configuration updates
403
+ */
404
+ updatePrivacy(privacy: Partial<TrackerPrivacyConfig>): void;
405
+
406
+ /** Enable recording */
407
+ enable(): void;
408
+
409
+ /** Disable recording */
410
+ disable(): void;
411
+
412
+ /** Destroy the recorder and cleanup */
413
+ destroy(): void;
414
+ }
415
+
416
+ // ============================================================================
417
+ // Privacy Utilities
418
+ // ============================================================================
419
+
420
+ /** Default privacy configuration */
421
+ export declare const DEFAULT_PRIVACY_CONFIG: TrackerPrivacyConfig;
422
+
423
+ /** Sensitive CSS selectors that are auto-blocked */
424
+ export declare const SENSITIVE_SELECTORS: string[];
425
+
426
+ /** Check if Do Not Track is enabled */
427
+ export declare function isDoNotTrackEnabled(): boolean;
428
+
429
+ /** Check if current page should be recorded */
430
+ export declare function shouldRecordPage(
431
+ config: TrackerPrivacyConfig,
432
+ currentPath: string
433
+ ): boolean;
434
+
435
+ /** Build rrweb-compatible privacy config */
436
+ export declare function buildRrwebPrivacyConfig(
437
+ config: TrackerPrivacyConfig
438
+ ): Record<string, unknown>;
439
+
440
+ /** Merge user privacy config with defaults */
441
+ export declare function mergePrivacyConfig(
442
+ userConfig?: Partial<TrackerPrivacyConfig>
443
+ ): TrackerPrivacyConfig;
444
+
445
+ // ============================================================================
446
+ // Compression Utilities
447
+ // ============================================================================
448
+
449
+ /** Compress events using browser's CompressionStream */
450
+ export declare function compressEvents(events: unknown[]): Promise<CompressedEvents>;
451
+
452
+ /** Decompress events */
453
+ export declare function decompressEvents(compressed: CompressedEvents): Promise<unknown[]>;
454
+
455
+ /** Estimate compression ratio for events */
456
+ export declare function estimateCompression(events: unknown[]): Promise<CompressionStats>;
457
+
458
+ // ============================================================================
459
+ // Factory Functions
460
+ // ============================================================================
461
+
462
+ /** Create a session recorder instance */
463
+ export declare function createRecorder(
464
+ client: unknown,
465
+ config?: TrackerConfig
466
+ ): SessionRecorder;
467
+
468
+ /** Create a session manager instance */
469
+ export declare function createSessionManager(options?: {
470
+ storageKey?: string;
471
+ sessionTimeout?: number;
472
+ }): SessionManager;
473
+
474
+ // ============================================================================
475
+ // Plugin
476
+ // ============================================================================
477
+
478
+ /**
479
+ * Tracker plugin for @powerit/cms-client
480
+ */
481
+ export declare const trackerPlugin: {
482
+ name: 'tracker';
483
+ install(client: unknown, options?: TrackerConfig): () => void;
484
+ };
485
+
486
+ export default trackerPlugin;