@plasius/analytics 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.
- package/LICENSE +203 -0
- package/README.md +128 -0
- package/dist/index.cjs +457 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +94 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +426 -0
- package/dist/index.js.map +1 -0
- package/package.json +86 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AnalyticsProvider: () => AnalyticsProvider,
|
|
24
|
+
createLocalSpaceAnalyticsClient: () => createLocalSpaceAnalyticsClient,
|
|
25
|
+
createNoopLocalSpaceAnalyticsClient: () => createNoopLocalSpaceAnalyticsClient,
|
|
26
|
+
useAnalytics: () => useAnalytics,
|
|
27
|
+
useComponentInteractionTracker: () => useComponentInteractionTracker
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/core/client.ts
|
|
32
|
+
var DEFAULT_FLUSH_INTERVAL_MS = 5e3;
|
|
33
|
+
var DEFAULT_BATCH_SIZE = 20;
|
|
34
|
+
var DEFAULT_MAX_QUEUE_SIZE = 500;
|
|
35
|
+
var DEFAULT_STORAGE_KEY = "plasius.analytics.local-space.queue";
|
|
36
|
+
var JSON_CONTENT_TYPE_HEADER = "application/json";
|
|
37
|
+
function createId(prefix) {
|
|
38
|
+
return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
39
|
+
}
|
|
40
|
+
async function defaultTransport({
|
|
41
|
+
endpoint,
|
|
42
|
+
body,
|
|
43
|
+
headers,
|
|
44
|
+
keepalive
|
|
45
|
+
}) {
|
|
46
|
+
if (typeof fetch !== "function") {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const response = await fetch(endpoint, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers,
|
|
52
|
+
body,
|
|
53
|
+
keepalive
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new Error(`Analytics transport failed with status ${response.status}.`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function toPositiveInteger(value, fallback) {
|
|
60
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
61
|
+
return fallback;
|
|
62
|
+
}
|
|
63
|
+
const rounded = Math.floor(value);
|
|
64
|
+
return rounded > 0 ? rounded : fallback;
|
|
65
|
+
}
|
|
66
|
+
function resolveConfig(config, previous) {
|
|
67
|
+
const source = (config.source ?? previous?.source ?? "").trim();
|
|
68
|
+
if (!source) {
|
|
69
|
+
throw new Error("Local space analytics requires a non-empty source.");
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
source,
|
|
73
|
+
endpoint: config.endpoint?.trim(),
|
|
74
|
+
enabled: config.enabled ?? previous?.enabled ?? true,
|
|
75
|
+
defaultContext: {
|
|
76
|
+
...previous?.defaultContext ?? {},
|
|
77
|
+
...config.defaultContext ?? {}
|
|
78
|
+
},
|
|
79
|
+
headers: {
|
|
80
|
+
"content-type": JSON_CONTENT_TYPE_HEADER,
|
|
81
|
+
...previous?.headers ?? {},
|
|
82
|
+
...config.headers ?? {}
|
|
83
|
+
},
|
|
84
|
+
flushIntervalMs: toPositiveInteger(
|
|
85
|
+
config.flushIntervalMs ?? previous?.flushIntervalMs,
|
|
86
|
+
DEFAULT_FLUSH_INTERVAL_MS
|
|
87
|
+
),
|
|
88
|
+
batchSize: toPositiveInteger(config.batchSize ?? previous?.batchSize, DEFAULT_BATCH_SIZE),
|
|
89
|
+
maxQueueSize: toPositiveInteger(
|
|
90
|
+
config.maxQueueSize ?? previous?.maxQueueSize,
|
|
91
|
+
DEFAULT_MAX_QUEUE_SIZE
|
|
92
|
+
),
|
|
93
|
+
storageKey: (config.storageKey ?? previous?.storageKey ?? DEFAULT_STORAGE_KEY).trim(),
|
|
94
|
+
transport: config.transport ?? previous?.transport ?? defaultTransport,
|
|
95
|
+
onError: config.onError ?? previous?.onError
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function isBrowserEnvironment() {
|
|
99
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
100
|
+
}
|
|
101
|
+
function readQueue(storageKey) {
|
|
102
|
+
if (!isBrowserEnvironment() || typeof window.localStorage === "undefined") {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const rawValue = window.localStorage.getItem(storageKey);
|
|
107
|
+
if (!rawValue) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
const parsed = JSON.parse(rawValue);
|
|
111
|
+
if (!Array.isArray(parsed)) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
return parsed.filter(isStoredRecordCandidate);
|
|
115
|
+
} catch {
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function isStoredRecordCandidate(value) {
|
|
120
|
+
if (!value || typeof value !== "object") {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
const candidate = value;
|
|
124
|
+
return typeof candidate.id === "string" && typeof candidate.component === "string" && typeof candidate.action === "string" && typeof candidate.source === "string" && typeof candidate.sessionId === "string" && typeof candidate.timestamp === "number";
|
|
125
|
+
}
|
|
126
|
+
function writeQueue(storageKey, queue) {
|
|
127
|
+
if (!isBrowserEnvironment() || typeof window.localStorage === "undefined") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
if (queue.length === 0) {
|
|
132
|
+
window.localStorage.removeItem(storageKey);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
window.localStorage.setItem(storageKey, JSON.stringify(queue));
|
|
136
|
+
} catch {
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function createPayload(source, events) {
|
|
140
|
+
return JSON.stringify({
|
|
141
|
+
source,
|
|
142
|
+
sentAt: Date.now(),
|
|
143
|
+
events
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function createLocalSpaceAnalyticsClient(config) {
|
|
147
|
+
let resolvedConfig = resolveConfig(config);
|
|
148
|
+
const sessionId = createId("session");
|
|
149
|
+
let queue = readQueue(resolvedConfig.storageKey).slice(-resolvedConfig.maxQueueSize);
|
|
150
|
+
let flushTimer = null;
|
|
151
|
+
let isFlushing = false;
|
|
152
|
+
let flushQueuedAfterCurrent = false;
|
|
153
|
+
let isDestroyed = false;
|
|
154
|
+
const persistQueue = () => writeQueue(resolvedConfig.storageKey, queue);
|
|
155
|
+
const trimQueue = () => {
|
|
156
|
+
if (queue.length <= resolvedConfig.maxQueueSize) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
queue = queue.slice(queue.length - resolvedConfig.maxQueueSize);
|
|
160
|
+
};
|
|
161
|
+
const flushWithBeacon = () => {
|
|
162
|
+
if (!resolvedConfig.endpoint || queue.length === 0) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (typeof navigator === "undefined" || typeof navigator.sendBeacon !== "function") {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
const events = queue.slice(0, resolvedConfig.batchSize);
|
|
169
|
+
try {
|
|
170
|
+
const body = createPayload(resolvedConfig.source, events);
|
|
171
|
+
const blob = new Blob([body], { type: JSON_CONTENT_TYPE_HEADER });
|
|
172
|
+
const sent = navigator.sendBeacon(resolvedConfig.endpoint, blob);
|
|
173
|
+
if (sent) {
|
|
174
|
+
queue = queue.slice(events.length);
|
|
175
|
+
persistQueue();
|
|
176
|
+
}
|
|
177
|
+
return sent;
|
|
178
|
+
} catch {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const flush = async () => {
|
|
183
|
+
if (isDestroyed || !resolvedConfig.enabled || !resolvedConfig.endpoint) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (queue.length === 0) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (isFlushing) {
|
|
190
|
+
flushQueuedAfterCurrent = true;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const events = queue.slice(0, resolvedConfig.batchSize);
|
|
194
|
+
const body = createPayload(resolvedConfig.source, events);
|
|
195
|
+
isFlushing = true;
|
|
196
|
+
try {
|
|
197
|
+
await resolvedConfig.transport({
|
|
198
|
+
endpoint: resolvedConfig.endpoint,
|
|
199
|
+
body,
|
|
200
|
+
headers: resolvedConfig.headers,
|
|
201
|
+
keepalive: true
|
|
202
|
+
});
|
|
203
|
+
queue = queue.slice(events.length);
|
|
204
|
+
persistQueue();
|
|
205
|
+
} catch (error) {
|
|
206
|
+
resolvedConfig.onError?.(error);
|
|
207
|
+
} finally {
|
|
208
|
+
isFlushing = false;
|
|
209
|
+
if (flushQueuedAfterCurrent) {
|
|
210
|
+
flushQueuedAfterCurrent = false;
|
|
211
|
+
if (queue.length > 0) {
|
|
212
|
+
void flush();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
const startFlushTimer = () => {
|
|
218
|
+
if (flushTimer) {
|
|
219
|
+
clearInterval(flushTimer);
|
|
220
|
+
flushTimer = null;
|
|
221
|
+
}
|
|
222
|
+
if (!resolvedConfig.enabled) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
flushTimer = setInterval(() => {
|
|
226
|
+
void flush();
|
|
227
|
+
}, resolvedConfig.flushIntervalMs);
|
|
228
|
+
};
|
|
229
|
+
const handleVisibilityChange = () => {
|
|
230
|
+
if (typeof document === "undefined") {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (document.visibilityState !== "hidden") {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (!flushWithBeacon()) {
|
|
237
|
+
void flush();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const handlePageHide = () => {
|
|
241
|
+
if (!flushWithBeacon()) {
|
|
242
|
+
void flush();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
if (isBrowserEnvironment()) {
|
|
246
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
247
|
+
window.addEventListener("pagehide", handlePageHide);
|
|
248
|
+
}
|
|
249
|
+
startFlushTimer();
|
|
250
|
+
return {
|
|
251
|
+
get source() {
|
|
252
|
+
return resolvedConfig.source;
|
|
253
|
+
},
|
|
254
|
+
track(event) {
|
|
255
|
+
if (isDestroyed || !resolvedConfig.enabled) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const record = {
|
|
259
|
+
id: createId("event"),
|
|
260
|
+
component: event.component,
|
|
261
|
+
action: event.action,
|
|
262
|
+
label: event.label,
|
|
263
|
+
href: event.href,
|
|
264
|
+
variant: event.variant,
|
|
265
|
+
source: resolvedConfig.source,
|
|
266
|
+
sessionId,
|
|
267
|
+
timestamp: event.timestamp ?? Date.now(),
|
|
268
|
+
context: {
|
|
269
|
+
...resolvedConfig.defaultContext,
|
|
270
|
+
...event.context ?? {}
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
queue.push(record);
|
|
274
|
+
trimQueue();
|
|
275
|
+
persistQueue();
|
|
276
|
+
if (queue.length >= resolvedConfig.batchSize) {
|
|
277
|
+
void flush();
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
flush,
|
|
281
|
+
updateConfig(nextConfig) {
|
|
282
|
+
if (isDestroyed) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const previousStorageKey = resolvedConfig.storageKey;
|
|
286
|
+
resolvedConfig = resolveConfig(
|
|
287
|
+
{
|
|
288
|
+
...resolvedConfig,
|
|
289
|
+
...nextConfig,
|
|
290
|
+
source: nextConfig.source ?? resolvedConfig.source
|
|
291
|
+
},
|
|
292
|
+
resolvedConfig
|
|
293
|
+
);
|
|
294
|
+
if (previousStorageKey !== resolvedConfig.storageKey) {
|
|
295
|
+
writeQueue(previousStorageKey, []);
|
|
296
|
+
persistQueue();
|
|
297
|
+
}
|
|
298
|
+
startFlushTimer();
|
|
299
|
+
if (resolvedConfig.endpoint && queue.length > 0) {
|
|
300
|
+
void flush();
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
getConfig() {
|
|
304
|
+
return resolvedConfig;
|
|
305
|
+
},
|
|
306
|
+
destroy() {
|
|
307
|
+
if (isDestroyed) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
isDestroyed = true;
|
|
311
|
+
if (flushTimer) {
|
|
312
|
+
clearInterval(flushTimer);
|
|
313
|
+
flushTimer = null;
|
|
314
|
+
}
|
|
315
|
+
if (isBrowserEnvironment()) {
|
|
316
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
317
|
+
window.removeEventListener("pagehide", handlePageHide);
|
|
318
|
+
}
|
|
319
|
+
void flush();
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function createNoopLocalSpaceAnalyticsClient(source = "noop") {
|
|
324
|
+
const noopConfig = {
|
|
325
|
+
source,
|
|
326
|
+
enabled: false,
|
|
327
|
+
endpoint: void 0,
|
|
328
|
+
defaultContext: {},
|
|
329
|
+
headers: { "content-type": JSON_CONTENT_TYPE_HEADER },
|
|
330
|
+
flushIntervalMs: DEFAULT_FLUSH_INTERVAL_MS,
|
|
331
|
+
batchSize: DEFAULT_BATCH_SIZE,
|
|
332
|
+
maxQueueSize: DEFAULT_MAX_QUEUE_SIZE,
|
|
333
|
+
storageKey: DEFAULT_STORAGE_KEY,
|
|
334
|
+
transport: async () => void 0,
|
|
335
|
+
onError: void 0
|
|
336
|
+
};
|
|
337
|
+
return {
|
|
338
|
+
source,
|
|
339
|
+
track: () => void 0,
|
|
340
|
+
flush: async () => void 0,
|
|
341
|
+
updateConfig: () => void 0,
|
|
342
|
+
getConfig: () => noopConfig,
|
|
343
|
+
destroy: () => void 0
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// src/react/provider.tsx
|
|
348
|
+
var import_react = require("react");
|
|
349
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
350
|
+
var AnalyticsContext = (0, import_react.createContext)(null);
|
|
351
|
+
function AnalyticsProvider({
|
|
352
|
+
children,
|
|
353
|
+
source = "react-app",
|
|
354
|
+
endpoint,
|
|
355
|
+
enabled = true,
|
|
356
|
+
defaultContext,
|
|
357
|
+
headers,
|
|
358
|
+
flushIntervalMs,
|
|
359
|
+
batchSize,
|
|
360
|
+
maxQueueSize,
|
|
361
|
+
storageKey,
|
|
362
|
+
transport,
|
|
363
|
+
onError,
|
|
364
|
+
client
|
|
365
|
+
}) {
|
|
366
|
+
const managedClient = (0, import_react.useMemo)(() => {
|
|
367
|
+
if (client) {
|
|
368
|
+
return client;
|
|
369
|
+
}
|
|
370
|
+
if (!enabled) {
|
|
371
|
+
return createNoopLocalSpaceAnalyticsClient(source);
|
|
372
|
+
}
|
|
373
|
+
return createLocalSpaceAnalyticsClient({
|
|
374
|
+
source,
|
|
375
|
+
endpoint,
|
|
376
|
+
enabled,
|
|
377
|
+
defaultContext,
|
|
378
|
+
headers,
|
|
379
|
+
flushIntervalMs,
|
|
380
|
+
batchSize,
|
|
381
|
+
maxQueueSize,
|
|
382
|
+
storageKey,
|
|
383
|
+
transport,
|
|
384
|
+
onError
|
|
385
|
+
});
|
|
386
|
+
}, [
|
|
387
|
+
client,
|
|
388
|
+
source,
|
|
389
|
+
endpoint,
|
|
390
|
+
enabled,
|
|
391
|
+
defaultContext,
|
|
392
|
+
headers,
|
|
393
|
+
flushIntervalMs,
|
|
394
|
+
batchSize,
|
|
395
|
+
maxQueueSize,
|
|
396
|
+
storageKey,
|
|
397
|
+
transport,
|
|
398
|
+
onError
|
|
399
|
+
]);
|
|
400
|
+
(0, import_react.useEffect)(() => {
|
|
401
|
+
return () => {
|
|
402
|
+
if (!client) {
|
|
403
|
+
managedClient.destroy();
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}, [client, managedClient]);
|
|
407
|
+
const value = (0, import_react.useMemo)(
|
|
408
|
+
() => ({
|
|
409
|
+
client: managedClient,
|
|
410
|
+
trackInteraction: (event) => {
|
|
411
|
+
managedClient.track(event);
|
|
412
|
+
},
|
|
413
|
+
flush: () => managedClient.flush()
|
|
414
|
+
}),
|
|
415
|
+
[managedClient]
|
|
416
|
+
);
|
|
417
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AnalyticsContext.Provider, { value, children });
|
|
418
|
+
}
|
|
419
|
+
function useAnalytics() {
|
|
420
|
+
const context = (0, import_react.useContext)(AnalyticsContext);
|
|
421
|
+
if (!context) {
|
|
422
|
+
throw new Error("useAnalytics must be used within AnalyticsProvider.");
|
|
423
|
+
}
|
|
424
|
+
return context;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/react/hooks.ts
|
|
428
|
+
var import_react2 = require("react");
|
|
429
|
+
function useComponentInteractionTracker(component, baseContext = {}) {
|
|
430
|
+
const { trackInteraction } = useAnalytics();
|
|
431
|
+
return (0, import_react2.useCallback)(
|
|
432
|
+
(action, details) => {
|
|
433
|
+
trackInteraction({
|
|
434
|
+
component,
|
|
435
|
+
action,
|
|
436
|
+
label: details?.label,
|
|
437
|
+
href: details?.href,
|
|
438
|
+
variant: details?.variant,
|
|
439
|
+
timestamp: details?.timestamp,
|
|
440
|
+
context: {
|
|
441
|
+
...baseContext,
|
|
442
|
+
...details?.context ?? {}
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
},
|
|
446
|
+
[baseContext, component, trackInteraction]
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
450
|
+
0 && (module.exports = {
|
|
451
|
+
AnalyticsProvider,
|
|
452
|
+
createLocalSpaceAnalyticsClient,
|
|
453
|
+
createNoopLocalSpaceAnalyticsClient,
|
|
454
|
+
useAnalytics,
|
|
455
|
+
useComponentInteractionTracker
|
|
456
|
+
});
|
|
457
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/client.ts","../src/react/provider.tsx","../src/react/hooks.ts"],"sourcesContent":["export * from \"./core/index.js\";\nexport * from \"./react/index.js\";\n","import type {\n AnalyticsTransport,\n LocalSpaceAnalyticsClient,\n LocalSpaceAnalyticsConfig,\n LocalSpaceAnalyticsEvent,\n LocalSpaceAnalyticsRecord,\n ResolvedLocalSpaceAnalyticsConfig,\n} from \"./types.js\";\n\nconst DEFAULT_FLUSH_INTERVAL_MS = 5000;\nconst DEFAULT_BATCH_SIZE = 20;\nconst DEFAULT_MAX_QUEUE_SIZE = 500;\nconst DEFAULT_STORAGE_KEY = \"plasius.analytics.local-space.queue\";\n\nconst JSON_CONTENT_TYPE_HEADER = \"application/json\";\n\nfunction createId(prefix: string): string {\n return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;\n}\n\nasync function defaultTransport({\n endpoint,\n body,\n headers,\n keepalive,\n}: {\n endpoint: string;\n body: string;\n headers: Record<string, string>;\n keepalive: boolean;\n}): Promise<void> {\n if (typeof fetch !== \"function\") {\n return;\n }\n\n const response = await fetch(endpoint, {\n method: \"POST\",\n headers,\n body,\n keepalive,\n });\n\n if (!response.ok) {\n throw new Error(`Analytics transport failed with status ${response.status}.`);\n }\n}\n\nfunction toPositiveInteger(value: number | undefined, fallback: number): number {\n if (typeof value !== \"number\" || !Number.isFinite(value)) {\n return fallback;\n }\n\n const rounded = Math.floor(value);\n return rounded > 0 ? rounded : fallback;\n}\n\nfunction resolveConfig(\n config: LocalSpaceAnalyticsConfig,\n previous?: ResolvedLocalSpaceAnalyticsConfig\n): ResolvedLocalSpaceAnalyticsConfig {\n const source = (config.source ?? previous?.source ?? \"\").trim();\n\n if (!source) {\n throw new Error(\"Local space analytics requires a non-empty source.\");\n }\n\n return {\n source,\n endpoint: config.endpoint?.trim(),\n enabled: config.enabled ?? previous?.enabled ?? true,\n defaultContext: {\n ...(previous?.defaultContext ?? {}),\n ...(config.defaultContext ?? {}),\n },\n headers: {\n \"content-type\": JSON_CONTENT_TYPE_HEADER,\n ...(previous?.headers ?? {}),\n ...(config.headers ?? {}),\n },\n flushIntervalMs: toPositiveInteger(\n config.flushIntervalMs ?? previous?.flushIntervalMs,\n DEFAULT_FLUSH_INTERVAL_MS\n ),\n batchSize: toPositiveInteger(config.batchSize ?? previous?.batchSize, DEFAULT_BATCH_SIZE),\n maxQueueSize: toPositiveInteger(\n config.maxQueueSize ?? previous?.maxQueueSize,\n DEFAULT_MAX_QUEUE_SIZE\n ),\n storageKey: (config.storageKey ?? previous?.storageKey ?? DEFAULT_STORAGE_KEY).trim(),\n transport: (config.transport ?? previous?.transport ?? defaultTransport) as AnalyticsTransport,\n onError: config.onError ?? previous?.onError,\n };\n}\n\nfunction isBrowserEnvironment(): boolean {\n return typeof window !== \"undefined\" && typeof document !== \"undefined\";\n}\n\nfunction readQueue(storageKey: string): LocalSpaceAnalyticsRecord[] {\n if (!isBrowserEnvironment() || typeof window.localStorage === \"undefined\") {\n return [];\n }\n\n try {\n const rawValue = window.localStorage.getItem(storageKey);\n if (!rawValue) {\n return [];\n }\n\n const parsed = JSON.parse(rawValue);\n if (!Array.isArray(parsed)) {\n return [];\n }\n\n return parsed.filter(isStoredRecordCandidate);\n } catch {\n return [];\n }\n}\n\nfunction isStoredRecordCandidate(value: unknown): value is LocalSpaceAnalyticsRecord {\n if (!value || typeof value !== \"object\") {\n return false;\n }\n\n const candidate = value as Partial<LocalSpaceAnalyticsRecord>;\n return (\n typeof candidate.id === \"string\" &&\n typeof candidate.component === \"string\" &&\n typeof candidate.action === \"string\" &&\n typeof candidate.source === \"string\" &&\n typeof candidate.sessionId === \"string\" &&\n typeof candidate.timestamp === \"number\"\n );\n}\n\nfunction writeQueue(storageKey: string, queue: LocalSpaceAnalyticsRecord[]): void {\n if (!isBrowserEnvironment() || typeof window.localStorage === \"undefined\") {\n return;\n }\n\n try {\n if (queue.length === 0) {\n window.localStorage.removeItem(storageKey);\n return;\n }\n\n window.localStorage.setItem(storageKey, JSON.stringify(queue));\n } catch {\n // Ignored by design: storage quota errors should never break host app UX.\n }\n}\n\nfunction createPayload(source: string, events: LocalSpaceAnalyticsRecord[]): string {\n return JSON.stringify({\n source,\n sentAt: Date.now(),\n events,\n });\n}\n\nexport function createLocalSpaceAnalyticsClient(\n config: LocalSpaceAnalyticsConfig\n): LocalSpaceAnalyticsClient {\n let resolvedConfig = resolveConfig(config);\n const sessionId = createId(\"session\");\n let queue = readQueue(resolvedConfig.storageKey).slice(-resolvedConfig.maxQueueSize);\n\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let isFlushing = false;\n let flushQueuedAfterCurrent = false;\n let isDestroyed = false;\n\n const persistQueue = () => writeQueue(resolvedConfig.storageKey, queue);\n\n const trimQueue = () => {\n if (queue.length <= resolvedConfig.maxQueueSize) {\n return;\n }\n\n queue = queue.slice(queue.length - resolvedConfig.maxQueueSize);\n };\n\n const flushWithBeacon = (): boolean => {\n if (!resolvedConfig.endpoint || queue.length === 0) {\n return false;\n }\n\n if (\n typeof navigator === \"undefined\" ||\n typeof navigator.sendBeacon !== \"function\"\n ) {\n return false;\n }\n\n const events = queue.slice(0, resolvedConfig.batchSize);\n\n try {\n const body = createPayload(resolvedConfig.source, events);\n const blob = new Blob([body], { type: JSON_CONTENT_TYPE_HEADER });\n const sent = navigator.sendBeacon(resolvedConfig.endpoint, blob);\n\n if (sent) {\n queue = queue.slice(events.length);\n persistQueue();\n }\n\n return sent;\n } catch {\n return false;\n }\n };\n\n const flush = async (): Promise<void> => {\n if (isDestroyed || !resolvedConfig.enabled || !resolvedConfig.endpoint) {\n return;\n }\n\n if (queue.length === 0) {\n return;\n }\n\n if (isFlushing) {\n flushQueuedAfterCurrent = true;\n return;\n }\n\n const events = queue.slice(0, resolvedConfig.batchSize);\n const body = createPayload(resolvedConfig.source, events);\n\n isFlushing = true;\n\n try {\n await resolvedConfig.transport({\n endpoint: resolvedConfig.endpoint,\n body,\n headers: resolvedConfig.headers,\n keepalive: true,\n });\n\n queue = queue.slice(events.length);\n persistQueue();\n } catch (error) {\n resolvedConfig.onError?.(error);\n } finally {\n isFlushing = false;\n\n if (flushQueuedAfterCurrent) {\n flushQueuedAfterCurrent = false;\n if (queue.length > 0) {\n void flush();\n }\n }\n }\n };\n\n const startFlushTimer = () => {\n if (flushTimer) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n\n if (!resolvedConfig.enabled) {\n return;\n }\n\n flushTimer = setInterval(() => {\n void flush();\n }, resolvedConfig.flushIntervalMs);\n };\n\n const handleVisibilityChange = () => {\n if (typeof document === \"undefined\") {\n return;\n }\n\n if (document.visibilityState !== \"hidden\") {\n return;\n }\n\n if (!flushWithBeacon()) {\n void flush();\n }\n };\n\n const handlePageHide = () => {\n if (!flushWithBeacon()) {\n void flush();\n }\n };\n\n if (isBrowserEnvironment()) {\n document.addEventListener(\"visibilitychange\", handleVisibilityChange);\n window.addEventListener(\"pagehide\", handlePageHide);\n }\n\n startFlushTimer();\n\n return {\n get source() {\n return resolvedConfig.source;\n },\n\n track(event: LocalSpaceAnalyticsEvent) {\n if (isDestroyed || !resolvedConfig.enabled) {\n return;\n }\n\n const record: LocalSpaceAnalyticsRecord = {\n id: createId(\"event\"),\n component: event.component,\n action: event.action,\n label: event.label,\n href: event.href,\n variant: event.variant,\n source: resolvedConfig.source,\n sessionId,\n timestamp: event.timestamp ?? Date.now(),\n context: {\n ...resolvedConfig.defaultContext,\n ...(event.context ?? {}),\n },\n };\n\n queue.push(record);\n trimQueue();\n persistQueue();\n\n if (queue.length >= resolvedConfig.batchSize) {\n void flush();\n }\n },\n\n flush,\n\n updateConfig(nextConfig: Partial<LocalSpaceAnalyticsConfig>) {\n if (isDestroyed) {\n return;\n }\n\n const previousStorageKey = resolvedConfig.storageKey;\n resolvedConfig = resolveConfig(\n {\n ...resolvedConfig,\n ...nextConfig,\n source: nextConfig.source ?? resolvedConfig.source,\n },\n resolvedConfig\n );\n\n if (previousStorageKey !== resolvedConfig.storageKey) {\n writeQueue(previousStorageKey, []);\n persistQueue();\n }\n\n startFlushTimer();\n\n if (resolvedConfig.endpoint && queue.length > 0) {\n void flush();\n }\n },\n\n getConfig() {\n return resolvedConfig;\n },\n\n destroy() {\n if (isDestroyed) {\n return;\n }\n\n isDestroyed = true;\n\n if (flushTimer) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n\n if (isBrowserEnvironment()) {\n document.removeEventListener(\"visibilitychange\", handleVisibilityChange);\n window.removeEventListener(\"pagehide\", handlePageHide);\n }\n\n void flush();\n },\n };\n}\n\nexport function createNoopLocalSpaceAnalyticsClient(\n source = \"noop\"\n): LocalSpaceAnalyticsClient {\n const noopConfig: ResolvedLocalSpaceAnalyticsConfig = {\n source,\n enabled: false,\n endpoint: undefined,\n defaultContext: {},\n headers: { \"content-type\": JSON_CONTENT_TYPE_HEADER },\n flushIntervalMs: DEFAULT_FLUSH_INTERVAL_MS,\n batchSize: DEFAULT_BATCH_SIZE,\n maxQueueSize: DEFAULT_MAX_QUEUE_SIZE,\n storageKey: DEFAULT_STORAGE_KEY,\n transport: async () => undefined,\n onError: undefined,\n };\n\n return {\n source,\n track: () => undefined,\n flush: async () => undefined,\n updateConfig: () => undefined,\n getConfig: () => noopConfig,\n destroy: () => undefined,\n };\n}\n","import { createContext, useContext, useEffect, useMemo, type ReactNode } from \"react\";\nimport {\n createLocalSpaceAnalyticsClient,\n createNoopLocalSpaceAnalyticsClient,\n} from \"../core/client.js\";\nimport type {\n LocalSpaceAnalyticsClient,\n LocalSpaceAnalyticsConfig,\n LocalSpaceAnalyticsEvent,\n} from \"../core/types.js\";\n\nexport interface AnalyticsProviderProps {\n children: ReactNode;\n source?: string;\n endpoint?: string;\n enabled?: boolean;\n defaultContext?: LocalSpaceAnalyticsConfig[\"defaultContext\"];\n headers?: LocalSpaceAnalyticsConfig[\"headers\"];\n flushIntervalMs?: number;\n batchSize?: number;\n maxQueueSize?: number;\n storageKey?: string;\n transport?: LocalSpaceAnalyticsConfig[\"transport\"];\n onError?: LocalSpaceAnalyticsConfig[\"onError\"];\n client?: LocalSpaceAnalyticsClient;\n}\n\nexport interface AnalyticsContextValue {\n client: LocalSpaceAnalyticsClient;\n trackInteraction: (event: LocalSpaceAnalyticsEvent) => void;\n flush: () => Promise<void>;\n}\n\nconst AnalyticsContext = createContext<AnalyticsContextValue | null>(null);\n\nexport function AnalyticsProvider({\n children,\n source = \"react-app\",\n endpoint,\n enabled = true,\n defaultContext,\n headers,\n flushIntervalMs,\n batchSize,\n maxQueueSize,\n storageKey,\n transport,\n onError,\n client,\n}: AnalyticsProviderProps) {\n const managedClient = useMemo(() => {\n if (client) {\n return client;\n }\n\n if (!enabled) {\n return createNoopLocalSpaceAnalyticsClient(source);\n }\n\n return createLocalSpaceAnalyticsClient({\n source,\n endpoint,\n enabled,\n defaultContext,\n headers,\n flushIntervalMs,\n batchSize,\n maxQueueSize,\n storageKey,\n transport,\n onError,\n });\n }, [\n client,\n source,\n endpoint,\n enabled,\n defaultContext,\n headers,\n flushIntervalMs,\n batchSize,\n maxQueueSize,\n storageKey,\n transport,\n onError,\n ]);\n\n useEffect(() => {\n return () => {\n if (!client) {\n managedClient.destroy();\n }\n };\n }, [client, managedClient]);\n\n const value = useMemo<AnalyticsContextValue>(\n () => ({\n client: managedClient,\n trackInteraction: (event: LocalSpaceAnalyticsEvent) => {\n managedClient.track(event);\n },\n flush: () => managedClient.flush(),\n }),\n [managedClient]\n );\n\n return <AnalyticsContext.Provider value={value}>{children}</AnalyticsContext.Provider>;\n}\n\nexport function useAnalytics(): AnalyticsContextValue {\n const context = useContext(AnalyticsContext);\n\n if (!context) {\n throw new Error(\"useAnalytics must be used within AnalyticsProvider.\");\n }\n\n return context;\n}\n","import { useCallback } from \"react\";\nimport type { AnalyticsContext, LocalSpaceAnalyticsEvent } from \"../core/types.js\";\nimport { useAnalytics } from \"./provider.js\";\n\nexport interface TrackInteractionDetails\n extends Omit<LocalSpaceAnalyticsEvent, \"component\" | \"action\" | \"context\"> {\n context?: AnalyticsContext;\n}\n\nexport function useComponentInteractionTracker(\n component: string,\n baseContext: AnalyticsContext = {}\n): (action: string, details?: TrackInteractionDetails) => void {\n const { trackInteraction } = useAnalytics();\n\n return useCallback(\n (action: string, details?: TrackInteractionDetails) => {\n trackInteraction({\n component,\n action,\n label: details?.label,\n href: details?.href,\n variant: details?.variant,\n timestamp: details?.timestamp,\n context: {\n ...baseContext,\n ...(details?.context ?? {}),\n },\n });\n },\n [baseContext, component, trackInteraction]\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSA,IAAM,4BAA4B;AAClC,IAAM,qBAAqB;AAC3B,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
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type AnalyticsContext = Record<string, unknown>;
|
|
5
|
+
interface LocalSpaceAnalyticsEvent {
|
|
6
|
+
component: string;
|
|
7
|
+
action: string;
|
|
8
|
+
label?: string;
|
|
9
|
+
href?: string;
|
|
10
|
+
variant?: string;
|
|
11
|
+
context?: AnalyticsContext;
|
|
12
|
+
timestamp?: number;
|
|
13
|
+
}
|
|
14
|
+
interface LocalSpaceAnalyticsRecord extends LocalSpaceAnalyticsEvent {
|
|
15
|
+
id: string;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
source: string;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
context: AnalyticsContext;
|
|
20
|
+
}
|
|
21
|
+
interface AnalyticsTransportRequest {
|
|
22
|
+
endpoint: string;
|
|
23
|
+
body: string;
|
|
24
|
+
headers: Record<string, string>;
|
|
25
|
+
keepalive: boolean;
|
|
26
|
+
}
|
|
27
|
+
type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
|
|
28
|
+
interface LocalSpaceAnalyticsConfig {
|
|
29
|
+
endpoint?: string;
|
|
30
|
+
source: string;
|
|
31
|
+
enabled?: boolean;
|
|
32
|
+
defaultContext?: AnalyticsContext;
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
flushIntervalMs?: number;
|
|
35
|
+
batchSize?: number;
|
|
36
|
+
maxQueueSize?: number;
|
|
37
|
+
storageKey?: string;
|
|
38
|
+
transport?: AnalyticsTransport;
|
|
39
|
+
onError?: (error: unknown) => void;
|
|
40
|
+
}
|
|
41
|
+
interface ResolvedLocalSpaceAnalyticsConfig {
|
|
42
|
+
endpoint?: string;
|
|
43
|
+
source: string;
|
|
44
|
+
enabled: boolean;
|
|
45
|
+
defaultContext: AnalyticsContext;
|
|
46
|
+
headers: Record<string, string>;
|
|
47
|
+
flushIntervalMs: number;
|
|
48
|
+
batchSize: number;
|
|
49
|
+
maxQueueSize: number;
|
|
50
|
+
storageKey: string;
|
|
51
|
+
transport: AnalyticsTransport;
|
|
52
|
+
onError?: (error: unknown) => void;
|
|
53
|
+
}
|
|
54
|
+
interface LocalSpaceAnalyticsClient {
|
|
55
|
+
readonly source: string;
|
|
56
|
+
track: (event: LocalSpaceAnalyticsEvent) => void;
|
|
57
|
+
flush: () => Promise<void>;
|
|
58
|
+
updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
|
|
59
|
+
getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
|
|
60
|
+
destroy: () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
|
|
64
|
+
declare function createNoopLocalSpaceAnalyticsClient(source?: string): LocalSpaceAnalyticsClient;
|
|
65
|
+
|
|
66
|
+
interface AnalyticsProviderProps {
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
source?: string;
|
|
69
|
+
endpoint?: string;
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
defaultContext?: LocalSpaceAnalyticsConfig["defaultContext"];
|
|
72
|
+
headers?: LocalSpaceAnalyticsConfig["headers"];
|
|
73
|
+
flushIntervalMs?: number;
|
|
74
|
+
batchSize?: number;
|
|
75
|
+
maxQueueSize?: number;
|
|
76
|
+
storageKey?: string;
|
|
77
|
+
transport?: LocalSpaceAnalyticsConfig["transport"];
|
|
78
|
+
onError?: LocalSpaceAnalyticsConfig["onError"];
|
|
79
|
+
client?: LocalSpaceAnalyticsClient;
|
|
80
|
+
}
|
|
81
|
+
interface AnalyticsContextValue {
|
|
82
|
+
client: LocalSpaceAnalyticsClient;
|
|
83
|
+
trackInteraction: (event: LocalSpaceAnalyticsEvent) => void;
|
|
84
|
+
flush: () => Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
declare function AnalyticsProvider({ children, source, endpoint, enabled, defaultContext, headers, flushIntervalMs, batchSize, maxQueueSize, storageKey, transport, onError, client, }: AnalyticsProviderProps): react_jsx_runtime.JSX.Element;
|
|
87
|
+
declare function useAnalytics(): AnalyticsContextValue;
|
|
88
|
+
|
|
89
|
+
interface TrackInteractionDetails extends Omit<LocalSpaceAnalyticsEvent, "component" | "action" | "context"> {
|
|
90
|
+
context?: AnalyticsContext;
|
|
91
|
+
}
|
|
92
|
+
declare function useComponentInteractionTracker(component: string, baseContext?: AnalyticsContext): (action: string, details?: TrackInteractionDetails) => void;
|
|
93
|
+
|
|
94
|
+
export { type AnalyticsContext, type AnalyticsContextValue, AnalyticsProvider, type AnalyticsProviderProps, type AnalyticsTransport, type AnalyticsTransportRequest, type LocalSpaceAnalyticsClient, type LocalSpaceAnalyticsConfig, type LocalSpaceAnalyticsEvent, type LocalSpaceAnalyticsRecord, type ResolvedLocalSpaceAnalyticsConfig, type TrackInteractionDetails, createLocalSpaceAnalyticsClient, createNoopLocalSpaceAnalyticsClient, useAnalytics, useComponentInteractionTracker };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type AnalyticsContext = Record<string, unknown>;
|
|
5
|
+
interface LocalSpaceAnalyticsEvent {
|
|
6
|
+
component: string;
|
|
7
|
+
action: string;
|
|
8
|
+
label?: string;
|
|
9
|
+
href?: string;
|
|
10
|
+
variant?: string;
|
|
11
|
+
context?: AnalyticsContext;
|
|
12
|
+
timestamp?: number;
|
|
13
|
+
}
|
|
14
|
+
interface LocalSpaceAnalyticsRecord extends LocalSpaceAnalyticsEvent {
|
|
15
|
+
id: string;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
source: string;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
context: AnalyticsContext;
|
|
20
|
+
}
|
|
21
|
+
interface AnalyticsTransportRequest {
|
|
22
|
+
endpoint: string;
|
|
23
|
+
body: string;
|
|
24
|
+
headers: Record<string, string>;
|
|
25
|
+
keepalive: boolean;
|
|
26
|
+
}
|
|
27
|
+
type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
|
|
28
|
+
interface LocalSpaceAnalyticsConfig {
|
|
29
|
+
endpoint?: string;
|
|
30
|
+
source: string;
|
|
31
|
+
enabled?: boolean;
|
|
32
|
+
defaultContext?: AnalyticsContext;
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
flushIntervalMs?: number;
|
|
35
|
+
batchSize?: number;
|
|
36
|
+
maxQueueSize?: number;
|
|
37
|
+
storageKey?: string;
|
|
38
|
+
transport?: AnalyticsTransport;
|
|
39
|
+
onError?: (error: unknown) => void;
|
|
40
|
+
}
|
|
41
|
+
interface ResolvedLocalSpaceAnalyticsConfig {
|
|
42
|
+
endpoint?: string;
|
|
43
|
+
source: string;
|
|
44
|
+
enabled: boolean;
|
|
45
|
+
defaultContext: AnalyticsContext;
|
|
46
|
+
headers: Record<string, string>;
|
|
47
|
+
flushIntervalMs: number;
|
|
48
|
+
batchSize: number;
|
|
49
|
+
maxQueueSize: number;
|
|
50
|
+
storageKey: string;
|
|
51
|
+
transport: AnalyticsTransport;
|
|
52
|
+
onError?: (error: unknown) => void;
|
|
53
|
+
}
|
|
54
|
+
interface LocalSpaceAnalyticsClient {
|
|
55
|
+
readonly source: string;
|
|
56
|
+
track: (event: LocalSpaceAnalyticsEvent) => void;
|
|
57
|
+
flush: () => Promise<void>;
|
|
58
|
+
updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
|
|
59
|
+
getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
|
|
60
|
+
destroy: () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
|
|
64
|
+
declare function createNoopLocalSpaceAnalyticsClient(source?: string): LocalSpaceAnalyticsClient;
|
|
65
|
+
|
|
66
|
+
interface AnalyticsProviderProps {
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
source?: string;
|
|
69
|
+
endpoint?: string;
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
defaultContext?: LocalSpaceAnalyticsConfig["defaultContext"];
|
|
72
|
+
headers?: LocalSpaceAnalyticsConfig["headers"];
|
|
73
|
+
flushIntervalMs?: number;
|
|
74
|
+
batchSize?: number;
|
|
75
|
+
maxQueueSize?: number;
|
|
76
|
+
storageKey?: string;
|
|
77
|
+
transport?: LocalSpaceAnalyticsConfig["transport"];
|
|
78
|
+
onError?: LocalSpaceAnalyticsConfig["onError"];
|
|
79
|
+
client?: LocalSpaceAnalyticsClient;
|
|
80
|
+
}
|
|
81
|
+
interface AnalyticsContextValue {
|
|
82
|
+
client: LocalSpaceAnalyticsClient;
|
|
83
|
+
trackInteraction: (event: LocalSpaceAnalyticsEvent) => void;
|
|
84
|
+
flush: () => Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
declare function AnalyticsProvider({ children, source, endpoint, enabled, defaultContext, headers, flushIntervalMs, batchSize, maxQueueSize, storageKey, transport, onError, client, }: AnalyticsProviderProps): react_jsx_runtime.JSX.Element;
|
|
87
|
+
declare function useAnalytics(): AnalyticsContextValue;
|
|
88
|
+
|
|
89
|
+
interface TrackInteractionDetails extends Omit<LocalSpaceAnalyticsEvent, "component" | "action" | "context"> {
|
|
90
|
+
context?: AnalyticsContext;
|
|
91
|
+
}
|
|
92
|
+
declare function useComponentInteractionTracker(component: string, baseContext?: AnalyticsContext): (action: string, details?: TrackInteractionDetails) => void;
|
|
93
|
+
|
|
94
|
+
export { type AnalyticsContext, type AnalyticsContextValue, AnalyticsProvider, type AnalyticsProviderProps, type AnalyticsTransport, type AnalyticsTransportRequest, type LocalSpaceAnalyticsClient, type LocalSpaceAnalyticsConfig, type LocalSpaceAnalyticsEvent, type LocalSpaceAnalyticsRecord, type ResolvedLocalSpaceAnalyticsConfig, type TrackInteractionDetails, createLocalSpaceAnalyticsClient, createNoopLocalSpaceAnalyticsClient, useAnalytics, useComponentInteractionTracker };
|