@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/server.js CHANGED
@@ -1,9 +1,9 @@
1
- var h = Object.defineProperty;
2
- var f = (a, r, e) => r in a ? h(a, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[r] = e;
3
- var o = (a, r, e) => f(a, typeof r != "symbol" ? r + "" : r, e);
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 x } from "./base.provider-AfFL5W_P.js";
6
- class v {
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(r) {
38
- o(this, "providerConfigs", []);
39
- o(this, "config");
40
- o(this, "initialized", !1);
41
- this.config = r, this.providerConfigs = this.normalizeProviders(r.providers);
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(r) {
47
- const e = [
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 r.map((i) => {
56
- if ("initialize" in i && "track" in i)
55
+ return e.map((n) => {
56
+ if ("initialize" in n && "track" in n)
57
57
  return {
58
- provider: i,
59
- enabledMethods: new Set(e)
58
+ provider: n,
59
+ enabledMethods: new Set(t)
60
60
  };
61
- const t = i;
62
- t.methods && t.exclude && console.warn(
63
- `[Analytics] Provider ${t.provider.name} has both 'methods' and 'exclude' specified. Using 'methods' and ignoring 'exclude'.`
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 n;
66
- return t.methods ? n = new Set(t.methods) : t.exclude ? n = new Set(
67
- e.filter(
71
+ let a;
72
+ r.methods ? a = new Set(r.methods) : r.exclude ? a = new Set(
73
+ t.filter(
68
74
  (l) => {
69
- var d;
70
- return !((d = t.exclude) != null && d.includes(l));
75
+ var v;
76
+ return !((v = r.exclude) != null && v.includes(l));
71
77
  }
72
78
  )
73
- ) : n = new Set(e), {
74
- provider: t.provider,
75
- enabledMethods: n
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(r, e) {
83
- return r.enabledMethods.has(e);
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 r of this.providerConfigs)
124
- r.provider.initialize();
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(r, e) {
175
- for (const i of this.providerConfigs)
176
- this.shouldCallMethod(i, "identify") && i.provider.identify(r, e);
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(r, e, i) {
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 t = {
306
- action: r,
307
- category: this.getCategoryFromEventName(r),
308
- properties: e,
325
+ const r = {
326
+ action: e,
327
+ category: this.getCategoryFromEventName(e),
328
+ properties: t,
309
329
  timestamp: Date.now(),
310
- userId: i == null ? void 0 : i.userId,
311
- sessionId: i == null ? void 0 : i.sessionId
312
- }, n = {
330
+ userId: n == null ? void 0 : n.userId,
331
+ sessionId: n == null ? void 0 : n.sessionId
332
+ }, a = {
313
333
  ...this.config.defaultContext,
314
- ...i == null ? void 0 : i.context,
315
- user: (i == null ? void 0 : i.user) || ((d = i == null ? void 0 : i.context) == null ? void 0 : d.user)
316
- }, l = this.providerConfigs.filter((s) => this.shouldCallMethod(s, "track")).map(async (s) => {
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(t, n);
319
- } catch (c) {
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
- c
344
+ l
323
345
  );
324
346
  }
325
347
  });
326
- await Promise.all(l);
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(r, e) {
410
+ pageView(e, t) {
389
411
  if (!this.initialized) return;
390
- const i = {
412
+ const n = {
391
413
  ...this.config.defaultContext,
392
- ...e == null ? void 0 : e.context
414
+ ...t == null ? void 0 : t.context
393
415
  };
394
- for (const t of this.providerConfigs)
395
- this.shouldCallMethod(t, "pageView") && t.provider.pageView(r, i);
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(r, e) {
478
+ pageLeave(e, t) {
457
479
  if (!this.initialized) return;
458
- const i = {
480
+ const n = {
459
481
  ...this.config.defaultContext,
460
- ...e == null ? void 0 : e.context
482
+ ...t == null ? void 0 : t.context
461
483
  };
462
- for (const t of this.providerConfigs)
463
- this.shouldCallMethod(t, "pageLeave") && t.provider.pageLeave && t.provider.pageLeave(r, i);
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 r = this.providerConfigs.map((e) => "shutdown" in e.provider && typeof e.provider.shutdown == "function" ? e.provider.shutdown() : Promise.resolve());
551
- await Promise.all(r);
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(r) {
554
- const e = r.split("_");
555
- return e.length > 1 && e[0] ? e[0] : "engagement";
575
+ getCategoryFromEventName(e) {
576
+ const t = e.split("_");
577
+ return t.length > 1 && t[0] ? t[0] : "engagement";
556
578
  }
557
579
  }
558
- function g(a) {
559
- const r = {
560
- providers: a.providers || [],
561
- debug: a.debug,
562
- enabled: a.enabled
563
- }, e = new v(r);
564
- return e.initialize(), e;
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
- x as BaseAnalyticsProvider,
589
+ y as BaseAnalyticsProvider,
568
590
  w as PostHogServerProvider,
569
- v as ServerAnalytics,
570
- g as createServerAnalytics
591
+ f as ServerAnalytics,
592
+ x as createServerAnalytics
571
593
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacksee/analytics",
3
- "version": "0.11.1",
3
+ "version": "0.13.0",
4
4
  "description": "A highly typed, provider-agnostic analytics library for TypeScript applications",
5
5
  "type": "module",
6
6
  "exports": {