@stacksee/analytics 0.11.1 → 0.13.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/adapters/client/browser-analytics.d.ts +4 -0
- package/dist/adapters/server/server-analytics.d.ts +4 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +122 -100
- package/dist/core/events/types.d.ts +73 -2
- package/dist/providers/emitkit/server.d.ts +28 -1
- package/dist/providers/server.js +130 -108
- package/dist/server.d.ts +2 -2
- package/dist/server.js +96 -74
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var u = Object.defineProperty;
|
|
2
|
+
var h = (i, e, t) => e in i ? u(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
|
|
3
|
+
var c = (i, e, t) => h(i, typeof e != "symbol" ? e + "" : e, t);
|
|
4
4
|
import { P as w } from "./server-DjEk1fUD.js";
|
|
5
|
-
import { B as
|
|
6
|
-
class
|
|
5
|
+
import { B as y } from "./base.provider-AfFL5W_P.js";
|
|
6
|
+
class f {
|
|
7
7
|
/**
|
|
8
8
|
* Creates a new ServerAnalytics instance for server-side event tracking.
|
|
9
9
|
*
|
|
@@ -34,17 +34,17 @@ class v {
|
|
|
34
34
|
* analytics.initialize();
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
constructor(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.config =
|
|
37
|
+
constructor(e) {
|
|
38
|
+
c(this, "providerConfigs", []);
|
|
39
|
+
c(this, "config");
|
|
40
|
+
c(this, "initialized", !1);
|
|
41
|
+
this.config = e, this.providerConfigs = this.normalizeProviders(e.providers);
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* Normalizes provider configurations into a consistent internal format
|
|
45
45
|
*/
|
|
46
|
-
normalizeProviders(
|
|
47
|
-
const
|
|
46
|
+
normalizeProviders(e) {
|
|
47
|
+
const t = [
|
|
48
48
|
"initialize",
|
|
49
49
|
"identify",
|
|
50
50
|
"track",
|
|
@@ -52,35 +52,55 @@ class v {
|
|
|
52
52
|
"pageLeave",
|
|
53
53
|
"reset"
|
|
54
54
|
];
|
|
55
|
-
return
|
|
56
|
-
if ("initialize" in
|
|
55
|
+
return e.map((n) => {
|
|
56
|
+
if ("initialize" in n && "track" in n)
|
|
57
57
|
return {
|
|
58
|
-
provider:
|
|
59
|
-
enabledMethods: new Set(
|
|
58
|
+
provider: n,
|
|
59
|
+
enabledMethods: new Set(t)
|
|
60
60
|
};
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
`[Analytics] Provider ${
|
|
61
|
+
const r = n;
|
|
62
|
+
r.methods && r.exclude && console.warn(
|
|
63
|
+
`[Analytics] Provider ${r.provider.name} has both 'methods' and 'exclude' specified. Using 'methods' and ignoring 'exclude'.`
|
|
64
|
+
), r.events && r.excludeEvents && console.warn(
|
|
65
|
+
`[Analytics] Provider ${r.provider.name} has both 'events' and 'excludeEvents' specified. Using 'events' and ignoring 'excludeEvents'.`
|
|
66
|
+
), r.events && r.eventPatterns && console.warn(
|
|
67
|
+
`[Analytics] Provider ${r.provider.name} has both 'events' and 'eventPatterns' specified. Using 'events' and ignoring 'eventPatterns'.`
|
|
68
|
+
), r.excludeEvents && r.eventPatterns && console.warn(
|
|
69
|
+
`[Analytics] Provider ${r.provider.name} has both 'excludeEvents' and 'eventPatterns' specified. Using 'eventPatterns' and ignoring 'excludeEvents'.`
|
|
64
70
|
);
|
|
65
|
-
let
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
let a;
|
|
72
|
+
r.methods ? a = new Set(r.methods) : r.exclude ? a = new Set(
|
|
73
|
+
t.filter(
|
|
68
74
|
(l) => {
|
|
69
|
-
var
|
|
70
|
-
return !((
|
|
75
|
+
var v;
|
|
76
|
+
return !((v = r.exclude) != null && v.includes(l));
|
|
71
77
|
}
|
|
72
78
|
)
|
|
73
|
-
) :
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
) : a = new Set(t);
|
|
80
|
+
let o, d, s;
|
|
81
|
+
return r.events && r.events.length > 0 ? o = new Set(r.events) : r.eventPatterns && r.eventPatterns.length > 0 ? s = r.eventPatterns.map((l) => {
|
|
82
|
+
const v = l.replace(/\*/g, ".*");
|
|
83
|
+
return new RegExp(`^${v}$`);
|
|
84
|
+
}) : r.excludeEvents && r.excludeEvents.length > 0 && (d = new Set(r.excludeEvents)), {
|
|
85
|
+
provider: r.provider,
|
|
86
|
+
enabledMethods: a,
|
|
87
|
+
enabledEvents: o,
|
|
88
|
+
excludedEvents: d,
|
|
89
|
+
eventPatterns: s
|
|
76
90
|
};
|
|
77
91
|
});
|
|
78
92
|
}
|
|
79
93
|
/**
|
|
80
94
|
* Checks if a method should be called on a provider based on routing configuration
|
|
81
95
|
*/
|
|
82
|
-
shouldCallMethod(
|
|
83
|
-
return
|
|
96
|
+
shouldCallMethod(e, t) {
|
|
97
|
+
return e.enabledMethods.has(t);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Checks if an event should be tracked on a provider based on event filtering configuration
|
|
101
|
+
*/
|
|
102
|
+
shouldTrackEvent(e, t) {
|
|
103
|
+
return !e.enabledEvents && !e.excludedEvents && !e.eventPatterns ? !0 : e.enabledEvents ? e.enabledEvents.has(t) : e.eventPatterns ? e.eventPatterns.some((n) => n.test(t)) : e.excludedEvents ? !e.excludedEvents.has(t) : !0;
|
|
84
104
|
}
|
|
85
105
|
/**
|
|
86
106
|
* Initializes all analytics providers.
|
|
@@ -120,8 +140,8 @@ class v {
|
|
|
120
140
|
*/
|
|
121
141
|
initialize() {
|
|
122
142
|
if (!this.initialized) {
|
|
123
|
-
for (const
|
|
124
|
-
|
|
143
|
+
for (const e of this.providerConfigs)
|
|
144
|
+
e.provider.initialize();
|
|
125
145
|
this.initialized = !0;
|
|
126
146
|
}
|
|
127
147
|
}
|
|
@@ -171,9 +191,9 @@ class v {
|
|
|
171
191
|
* }
|
|
172
192
|
* ```
|
|
173
193
|
*/
|
|
174
|
-
identify(
|
|
175
|
-
for (const
|
|
176
|
-
this.shouldCallMethod(
|
|
194
|
+
identify(e, t) {
|
|
195
|
+
for (const n of this.providerConfigs)
|
|
196
|
+
this.shouldCallMethod(n, "identify") && n.provider.identify(e, t);
|
|
177
197
|
}
|
|
178
198
|
/**
|
|
179
199
|
* Tracks a custom event with properties and optional context.
|
|
@@ -296,34 +316,36 @@ class v {
|
|
|
296
316
|
* }
|
|
297
317
|
* ```
|
|
298
318
|
*/
|
|
299
|
-
async track(
|
|
319
|
+
async track(e, t, n) {
|
|
300
320
|
var d;
|
|
301
321
|
if (!this.initialized) {
|
|
302
322
|
console.warn("[Analytics] Not initialized. Call initialize() first.");
|
|
303
323
|
return;
|
|
304
324
|
}
|
|
305
|
-
const
|
|
306
|
-
action:
|
|
307
|
-
category: this.getCategoryFromEventName(
|
|
308
|
-
properties:
|
|
325
|
+
const r = {
|
|
326
|
+
action: e,
|
|
327
|
+
category: this.getCategoryFromEventName(e),
|
|
328
|
+
properties: t,
|
|
309
329
|
timestamp: Date.now(),
|
|
310
|
-
userId:
|
|
311
|
-
sessionId:
|
|
312
|
-
},
|
|
330
|
+
userId: n == null ? void 0 : n.userId,
|
|
331
|
+
sessionId: n == null ? void 0 : n.sessionId
|
|
332
|
+
}, a = {
|
|
313
333
|
...this.config.defaultContext,
|
|
314
|
-
...
|
|
315
|
-
user: (
|
|
316
|
-
},
|
|
334
|
+
...n == null ? void 0 : n.context,
|
|
335
|
+
user: (n == null ? void 0 : n.user) || ((d = n == null ? void 0 : n.context) == null ? void 0 : d.user)
|
|
336
|
+
}, o = this.providerConfigs.filter(
|
|
337
|
+
(s) => this.shouldCallMethod(s, "track") && this.shouldTrackEvent(s, e)
|
|
338
|
+
).map(async (s) => {
|
|
317
339
|
try {
|
|
318
|
-
await s.provider.track(
|
|
319
|
-
} catch (
|
|
340
|
+
await s.provider.track(r, a);
|
|
341
|
+
} catch (l) {
|
|
320
342
|
console.error(
|
|
321
343
|
`[Analytics] Provider ${s.provider.name} failed to track event:`,
|
|
322
|
-
|
|
344
|
+
l
|
|
323
345
|
);
|
|
324
346
|
}
|
|
325
347
|
});
|
|
326
|
-
await Promise.all(
|
|
348
|
+
await Promise.all(o);
|
|
327
349
|
}
|
|
328
350
|
/**
|
|
329
351
|
* Tracks a page view event from the server side.
|
|
@@ -385,14 +407,14 @@ class v {
|
|
|
385
407
|
* }
|
|
386
408
|
* ```
|
|
387
409
|
*/
|
|
388
|
-
pageView(
|
|
410
|
+
pageView(e, t) {
|
|
389
411
|
if (!this.initialized) return;
|
|
390
|
-
const
|
|
412
|
+
const n = {
|
|
391
413
|
...this.config.defaultContext,
|
|
392
|
-
...
|
|
414
|
+
...t == null ? void 0 : t.context
|
|
393
415
|
};
|
|
394
|
-
for (const
|
|
395
|
-
this.shouldCallMethod(
|
|
416
|
+
for (const r of this.providerConfigs)
|
|
417
|
+
this.shouldCallMethod(r, "pageView") && r.provider.pageView(e, n);
|
|
396
418
|
}
|
|
397
419
|
/**
|
|
398
420
|
* Tracks when a user leaves a page from the server side.
|
|
@@ -453,14 +475,14 @@ class v {
|
|
|
453
475
|
* }
|
|
454
476
|
* ```
|
|
455
477
|
*/
|
|
456
|
-
pageLeave(
|
|
478
|
+
pageLeave(e, t) {
|
|
457
479
|
if (!this.initialized) return;
|
|
458
|
-
const
|
|
480
|
+
const n = {
|
|
459
481
|
...this.config.defaultContext,
|
|
460
|
-
...
|
|
482
|
+
...t == null ? void 0 : t.context
|
|
461
483
|
};
|
|
462
|
-
for (const
|
|
463
|
-
this.shouldCallMethod(
|
|
484
|
+
for (const r of this.providerConfigs)
|
|
485
|
+
this.shouldCallMethod(r, "pageLeave") && r.provider.pageLeave && r.provider.pageLeave(e, n);
|
|
464
486
|
}
|
|
465
487
|
/**
|
|
466
488
|
* Shuts down all analytics providers and flushes pending events.
|
|
@@ -547,25 +569,25 @@ class v {
|
|
|
547
569
|
* ```
|
|
548
570
|
*/
|
|
549
571
|
async shutdown() {
|
|
550
|
-
const
|
|
551
|
-
await Promise.all(
|
|
572
|
+
const e = this.providerConfigs.map((t) => "shutdown" in t.provider && typeof t.provider.shutdown == "function" ? t.provider.shutdown() : Promise.resolve());
|
|
573
|
+
await Promise.all(e);
|
|
552
574
|
}
|
|
553
|
-
getCategoryFromEventName(
|
|
554
|
-
const
|
|
555
|
-
return
|
|
575
|
+
getCategoryFromEventName(e) {
|
|
576
|
+
const t = e.split("_");
|
|
577
|
+
return t.length > 1 && t[0] ? t[0] : "engagement";
|
|
556
578
|
}
|
|
557
579
|
}
|
|
558
|
-
function
|
|
559
|
-
const
|
|
560
|
-
providers:
|
|
561
|
-
debug:
|
|
562
|
-
enabled:
|
|
563
|
-
},
|
|
564
|
-
return
|
|
580
|
+
function x(i) {
|
|
581
|
+
const e = {
|
|
582
|
+
providers: i.providers || [],
|
|
583
|
+
debug: i.debug,
|
|
584
|
+
enabled: i.enabled
|
|
585
|
+
}, t = new f(e);
|
|
586
|
+
return t.initialize(), t;
|
|
565
587
|
}
|
|
566
588
|
export {
|
|
567
|
-
|
|
589
|
+
y as BaseAnalyticsProvider,
|
|
568
590
|
w as PostHogServerProvider,
|
|
569
|
-
|
|
570
|
-
|
|
591
|
+
f as ServerAnalytics,
|
|
592
|
+
x as createServerAnalytics
|
|
571
593
|
};
|