@stacksee/analytics 0.9.8 → 0.11.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.
@@ -1,16 +1,16 @@
1
- var q = Object.defineProperty;
2
- var D = (p, u, i) => u in p ? q(p, u, { enumerable: !0, configurable: !0, writable: !0, value: i }) : p[u] = i;
3
- var t = (p, u, i) => D(p, typeof u != "symbol" ? u + "" : u, i);
4
- import { B as V } from "../base.provider-AfFL5W_P.js";
5
- import { P as Q } from "../server-DjEk1fUD.js";
6
- class N extends V {
1
+ var O = Object.defineProperty;
2
+ var q = (g, t, i) => t in g ? O(g, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : g[t] = i;
3
+ var o = (g, t, i) => q(g, typeof t != "symbol" ? t + "" : t, i);
4
+ import { B } from "../base.provider-AfFL5W_P.js";
5
+ import { P as X } from "../server-DjEk1fUD.js";
6
+ class H extends B {
7
7
  constructor(i) {
8
8
  super({ debug: i.debug, enabled: i.enabled });
9
- t(this, "name", "Bento-Server");
10
- t(this, "client");
11
- t(this, "initialized", !1);
12
- t(this, "config");
13
- t(this, "currentUserEmail");
9
+ o(this, "name", "Bento-Server");
10
+ o(this, "client");
11
+ o(this, "initialized", !1);
12
+ o(this, "config");
13
+ o(this, "currentUserEmail");
14
14
  this.config = i;
15
15
  }
16
16
  async initialize() {
@@ -23,7 +23,7 @@ class N extends V {
23
23
  if (!((e = this.config.authentication) != null && e.secretKey) || typeof this.config.authentication.secretKey != "string")
24
24
  throw new Error("Bento requires authentication.secretKey");
25
25
  try {
26
- const { Analytics: r } = await import("../bento-node-sdk.esm-CWEAoj97.js"), { debug: h, enabled: s, ...d } = this.config;
26
+ const { Analytics: r } = await import("../bento-node-sdk.esm-CWEAoj97.js"), { debug: a, enabled: s, ...d } = this.config;
27
27
  this.client = new r(d), this.initialized = !0, this.log("Initialized successfully", {
28
28
  siteUuid: this.config.siteUuid
29
29
  });
@@ -39,29 +39,32 @@ class N extends V {
39
39
  if (!this.isEnabled() || !this.initialized || !this.client) return;
40
40
  const r = (e == null ? void 0 : e.email) || i;
41
41
  if (!r || !r.includes("@")) {
42
- this.log("Skipping identify - invalid or missing email", { userId: i, traits: e });
42
+ this.log("Skipping identify - invalid or missing email", {
43
+ userId: i,
44
+ traits: e
45
+ });
43
46
  return;
44
47
  }
45
48
  this.currentUserEmail = r;
46
- const h = e ? { ...e } : {};
47
- delete h.email, this.client.V1.addSubscriber({
49
+ const a = e ? { ...e } : {};
50
+ a.email = void 0, this.client.V1.addSubscriber({
48
51
  email: r,
49
- fields: h
52
+ fields: a
50
53
  }).catch((s) => {
51
54
  console.error("[Bento-Server] Failed to identify user:", s);
52
55
  }), this.log("Identified user", { userId: i, email: r, traits: e });
53
56
  }
54
57
  async track(i, e) {
55
- var d, g, n, l;
58
+ var d, p, u, l;
56
59
  if (!this.isEnabled() || !this.initialized || !this.client) return;
57
- const r = ((d = e == null ? void 0 : e.user) == null ? void 0 : d.email) || this.currentUserEmail || ((g = e == null ? void 0 : e.user) == null ? void 0 : g.userId) || i.userId;
60
+ const r = ((d = e == null ? void 0 : e.user) == null ? void 0 : d.email) || this.currentUserEmail || ((p = e == null ? void 0 : e.user) == null ? void 0 : p.userId) || i.userId;
58
61
  if (!r || !r.includes("@")) {
59
62
  console.warn(
60
63
  "[Bento-Server] Skipping event - Bento requires an email address. Anonymous events are not currently supported by the Bento Node SDK. For now, use the Bento client provider for anonymous tracking. If you're using a proxy, use the hybrid pattern as described in the docs. For identified users, call identify() with a valid email before tracking events."
61
64
  );
62
65
  return;
63
66
  }
64
- const h = {
67
+ const a = {
65
68
  ...i.properties,
66
69
  category: i.category,
67
70
  timestamp: i.timestamp || Date.now(),
@@ -80,21 +83,21 @@ class N extends V {
80
83
  ...(e == null ? void 0 : e.device) && { device: e.device },
81
84
  ...(e == null ? void 0 : e.utm) && { utm: e.utm },
82
85
  site: this.config.siteUuid,
83
- ...((n = e == null ? void 0 : e.user) == null ? void 0 : n.userId) && { visitor: e.user.userId }
86
+ ...((u = e == null ? void 0 : e.user) == null ? void 0 : u.userId) && { visitor: e.user.userId }
84
87
  }, s = ((l = e == null ? void 0 : e.user) == null ? void 0 : l.traits) || {};
85
88
  try {
86
89
  await this.client.V1.track({
87
90
  email: r,
88
91
  type: `$${i.action}`,
89
- details: h,
92
+ details: a,
90
93
  fields: s
91
94
  }), this.log("Tracked event", { event: i, context: e });
92
- } catch (a) {
93
- console.error("[Bento-Server] Failed to track event:", a);
95
+ } catch (h) {
96
+ console.error("[Bento-Server] Failed to track event:", h);
94
97
  }
95
98
  }
96
99
  pageView(i, e) {
97
- var d, g, n;
100
+ var d, p, u;
98
101
  if (!this.isEnabled() || !this.initialized || !this.client) return;
99
102
  const r = ((d = e == null ? void 0 : e.user) == null ? void 0 : d.email) || this.currentUserEmail;
100
103
  if (!r || !r.includes("@")) {
@@ -103,7 +106,7 @@ class N extends V {
103
106
  );
104
107
  return;
105
108
  }
106
- const h = {
109
+ const a = {
107
110
  ...i,
108
111
  date: (/* @__PURE__ */ new Date()).toISOString(),
109
112
  ...(e == null ? void 0 : e.page) && {
@@ -118,12 +121,12 @@ class N extends V {
118
121
  }
119
122
  },
120
123
  site: this.config.siteUuid,
121
- ...((g = e == null ? void 0 : e.user) == null ? void 0 : g.userId) && { visitor: e.user.userId }
122
- }, s = ((n = e == null ? void 0 : e.user) == null ? void 0 : n.traits) || {};
124
+ ...((p = e == null ? void 0 : e.user) == null ? void 0 : p.userId) && { visitor: e.user.userId }
125
+ }, s = ((u = e == null ? void 0 : e.user) == null ? void 0 : u.traits) || {};
123
126
  this.client.V1.track({
124
127
  email: r,
125
128
  type: "$view",
126
- details: h,
129
+ details: a,
127
130
  fields: s
128
131
  }).catch((l) => {
129
132
  console.error("[Bento-Server] Failed to track page view:", l);
@@ -136,13 +139,13 @@ class N extends V {
136
139
  this.client = void 0, this.initialized = !1, this.log("Shutdown complete");
137
140
  }
138
141
  }
139
- class W extends V {
142
+ class W extends B {
140
143
  constructor(i) {
141
144
  super({ debug: i.debug, enabled: i.enabled });
142
- t(this, "name", "Pirsch-Server");
143
- t(this, "client");
144
- t(this, "initialized", !1);
145
- t(this, "config");
145
+ o(this, "name", "Pirsch-Server");
146
+ o(this, "client");
147
+ o(this, "initialized", !1);
148
+ o(this, "config");
146
149
  this.config = i;
147
150
  }
148
151
  async initialize() {
@@ -157,7 +160,7 @@ class W extends V {
157
160
  "Pirsch requires a clientId when using OAuth authentication (clientSecret doesn't start with 'pa_'). Either provide a clientId or use an access key (starts with 'pa_') as clientSecret."
158
161
  );
159
162
  try {
160
- const { Pirsch: e } = await import("../index-zS7gy63J.js").then((d) => d.i), { debug: r, enabled: h, ...s } = this.config;
163
+ const { Pirsch: e } = await import("../index-zS7gy63J.js").then((d) => d.i), { debug: r, enabled: a, ...s } = this.config;
161
164
  this.client = new e(s), this.initialized = !0, this.log("Initialized successfully", {
162
165
  hostname: this.config.hostname,
163
166
  authMode: i ? "access-key" : "oauth"
@@ -175,7 +178,7 @@ class W extends V {
175
178
  url: "https://identify",
176
179
  ip: "0.0.0.0",
177
180
  user_agent: "analytics-library"
178
- }, h = {
181
+ }, a = {
179
182
  userId: i,
180
183
  ...e && Object.fromEntries(
181
184
  Object.entries(e).filter(
@@ -183,87 +186,113 @@ class W extends V {
183
186
  )
184
187
  )
185
188
  };
186
- this.client.event("user_identified", r, 0, h).catch((s) => {
189
+ this.client.event("user_identified", r, 0, a).catch((s) => {
187
190
  console.error("[Pirsch-Server] Failed to track identify event:", s);
188
191
  }), this.log("Identified user via event", { userId: i, traits: e });
189
192
  }
190
193
  async track(i, e) {
191
- var a, o, f, y, c, w, m, b, S, k, I, _, E, P, z, B, A, F, v, $, K, j, O;
194
+ var h, n, c, v, y, m, w, b, E, I, k, S, _, z, P, U, K, A, f, $, D, j, V;
192
195
  if (!this.isEnabled() || !this.initialized || !this.client) return;
193
- const r = e, h = ((a = r == null ? void 0 : r.device) == null ? void 0 : a.ip) || ((o = r == null ? void 0 : r.server) == null ? void 0 : o.ip), s = ((f = r == null ? void 0 : r.server) == null ? void 0 : f.userAgent) || ((y = r == null ? void 0 : r.device) == null ? void 0 : y.userAgent);
194
- if (!h || !s) {
195
- this.log("Skipping event - missing required IP or user-agent from context", {
196
- hasIp: !!h,
197
- hasUserAgent: !!s,
198
- event: i.action
199
- });
196
+ const r = e, a = ((h = r == null ? void 0 : r.device) == null ? void 0 : h.ip) || ((n = r == null ? void 0 : r.server) == null ? void 0 : n.ip), s = ((c = r == null ? void 0 : r.server) == null ? void 0 : c.userAgent) || ((v = r == null ? void 0 : r.device) == null ? void 0 : v.userAgent);
197
+ if (!a || !s) {
198
+ this.log(
199
+ "Skipping event - missing required IP or user-agent from context",
200
+ {
201
+ hasIp: !!a,
202
+ hasUserAgent: !!s,
203
+ event: i.action
204
+ }
205
+ );
200
206
  return;
201
207
  }
202
- const g = {
203
- url: ((c = e == null ? void 0 : e.page) == null ? void 0 : c.url) || ((w = e == null ? void 0 : e.page) != null && w.protocol && ((m = e == null ? void 0 : e.page) != null && m.host) && ((b = e == null ? void 0 : e.page) != null && b.path) ? `${e.page.protocol}://${e.page.host}${e.page.path}` : (S = e == null ? void 0 : e.page) != null && S.path ? `https://${this.config.hostname}${e.page.path}` : "https://event"),
204
- ip: h,
208
+ const p = {
209
+ url: ((y = e == null ? void 0 : e.page) == null ? void 0 : y.url) || ((m = e == null ? void 0 : e.page) != null && m.protocol && ((w = e == null ? void 0 : e.page) != null && w.host) && ((b = e == null ? void 0 : e.page) != null && b.path) ? `${e.page.protocol}://${e.page.host}${e.page.path}` : (E = e == null ? void 0 : e.page) != null && E.path ? `https://${this.config.hostname}${e.page.path}` : "https://event"),
210
+ ip: a,
205
211
  user_agent: s,
206
- ...((k = e == null ? void 0 : e.page) == null ? void 0 : k.title) && { title: e.page.title },
207
- ...((I = e == null ? void 0 : e.page) == null ? void 0 : I.referrer) && { referrer: e.page.referrer },
208
- ...((E = (_ = e == null ? void 0 : e.device) == null ? void 0 : _.screen) == null ? void 0 : E.width) && { screen_width: e.device.screen.width },
209
- ...((z = (P = e == null ? void 0 : e.device) == null ? void 0 : P.screen) == null ? void 0 : z.height) && { screen_height: e.device.screen.height },
210
- ...((A = (B = e == null ? void 0 : e.device) == null ? void 0 : B.viewport) == null ? void 0 : A.width) && { sec_ch_viewport_width: String(e.device.viewport.width) },
211
- ...((F = e == null ? void 0 : e.device) == null ? void 0 : F.language) && { accept_language: e.device.language },
212
- ...((v = e == null ? void 0 : e.device) == null ? void 0 : v.type) && { sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0" },
212
+ ...((I = e == null ? void 0 : e.page) == null ? void 0 : I.title) && { title: e.page.title },
213
+ ...((k = e == null ? void 0 : e.page) == null ? void 0 : k.referrer) && { referrer: e.page.referrer },
214
+ ...((_ = (S = e == null ? void 0 : e.device) == null ? void 0 : S.screen) == null ? void 0 : _.width) && {
215
+ screen_width: e.device.screen.width
216
+ },
217
+ ...((P = (z = e == null ? void 0 : e.device) == null ? void 0 : z.screen) == null ? void 0 : P.height) && {
218
+ screen_height: e.device.screen.height
219
+ },
220
+ ...((K = (U = e == null ? void 0 : e.device) == null ? void 0 : U.viewport) == null ? void 0 : K.width) && {
221
+ sec_ch_viewport_width: String(e.device.viewport.width)
222
+ },
223
+ ...((A = e == null ? void 0 : e.device) == null ? void 0 : A.language) && {
224
+ accept_language: e.device.language
225
+ },
226
+ ...((f = e == null ? void 0 : e.device) == null ? void 0 : f.type) && {
227
+ sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0"
228
+ },
213
229
  ...(($ = e == null ? void 0 : e.device) == null ? void 0 : $.os) && { sec_ch_ua_platform: e.device.os }
214
230
  }, l = {
215
231
  ...Object.fromEntries(
216
232
  Object.entries(i.properties).filter(
217
- ([, U]) => typeof U == "string" || typeof U == "number" || typeof U == "boolean"
233
+ ([, F]) => typeof F == "string" || typeof F == "number" || typeof F == "boolean"
218
234
  )
219
235
  ),
220
236
  category: i.category,
221
237
  timestamp: String(i.timestamp || Date.now()),
222
238
  ...i.userId && { userId: i.userId },
223
239
  ...i.sessionId && { sessionId: i.sessionId },
224
- ...((K = e == null ? void 0 : e.user) == null ? void 0 : K.email) && { user_email: e.user.email },
240
+ ...((D = e == null ? void 0 : e.user) == null ? void 0 : D.email) && { user_email: e.user.email },
225
241
  ...((j = e == null ? void 0 : e.device) == null ? void 0 : j.timezone) && { timezone: e.device.timezone },
226
- ...((O = e == null ? void 0 : e.device) == null ? void 0 : O.browser) && { browser: e.device.browser }
242
+ ...((V = e == null ? void 0 : e.device) == null ? void 0 : V.browser) && { browser: e.device.browser }
227
243
  };
228
244
  try {
229
- await this.client.event(i.action, g, 0, l), this.log("Tracked event", { event: i, context: e });
230
- } catch (U) {
231
- console.error("[Pirsch-Server] Failed to track event:", U);
245
+ await this.client.event(i.action, p, 0, l), this.log("Tracked event", { event: i, context: e });
246
+ } catch (F) {
247
+ console.error("[Pirsch-Server] Failed to track event:", F);
232
248
  }
233
249
  }
234
250
  pageView(i, e) {
235
- var n, l, a, o, f, y, c, w, m, b, S, k, I, _, E, P, z, B, A, F;
251
+ var u, l, h, n, c, v, y, m, w, b, E, I, k, S, _, z, P, U, K, A;
236
252
  if (!this.isEnabled() || !this.initialized || !this.client) return;
237
- const r = e, h = ((n = r == null ? void 0 : r.device) == null ? void 0 : n.ip) || ((l = r == null ? void 0 : r.server) == null ? void 0 : l.ip), s = ((a = r == null ? void 0 : r.server) == null ? void 0 : a.userAgent) || ((o = r == null ? void 0 : r.device) == null ? void 0 : o.userAgent);
238
- if (!h || !s) {
239
- this.log("Skipping pageView - missing required IP or user-agent from context", {
240
- hasIp: !!h,
241
- hasUserAgent: !!s
242
- });
253
+ const r = e, a = ((u = r == null ? void 0 : r.device) == null ? void 0 : u.ip) || ((l = r == null ? void 0 : r.server) == null ? void 0 : l.ip), s = ((h = r == null ? void 0 : r.server) == null ? void 0 : h.userAgent) || ((n = r == null ? void 0 : r.device) == null ? void 0 : n.userAgent);
254
+ if (!a || !s) {
255
+ this.log(
256
+ "Skipping pageView - missing required IP or user-agent from context",
257
+ {
258
+ hasIp: !!a,
259
+ hasUserAgent: !!s
260
+ }
261
+ );
243
262
  return;
244
263
  }
245
- const g = {
246
- url: ((f = e == null ? void 0 : e.page) == null ? void 0 : f.url) || ((y = e == null ? void 0 : e.page) != null && y.protocol && ((c = e == null ? void 0 : e.page) != null && c.host) && ((w = e == null ? void 0 : e.page) != null && w.path) ? `${e.page.protocol}://${e.page.host}${e.page.path}` : (m = e == null ? void 0 : e.page) != null && m.path ? `https://${this.config.hostname}${e.page.path}` : "https://pageview"),
247
- ip: h,
264
+ const p = {
265
+ url: ((c = e == null ? void 0 : e.page) == null ? void 0 : c.url) || ((v = e == null ? void 0 : e.page) != null && v.protocol && ((y = e == null ? void 0 : e.page) != null && y.host) && ((m = e == null ? void 0 : e.page) != null && m.path) ? `${e.page.protocol}://${e.page.host}${e.page.path}` : (w = e == null ? void 0 : e.page) != null && w.path ? `https://${this.config.hostname}${e.page.path}` : "https://pageview"),
266
+ ip: a,
248
267
  user_agent: s,
249
268
  ...((b = e == null ? void 0 : e.page) == null ? void 0 : b.title) && { title: e.page.title },
250
- ...((S = e == null ? void 0 : e.page) == null ? void 0 : S.referrer) && { referrer: e.page.referrer },
251
- ...((I = (k = e == null ? void 0 : e.device) == null ? void 0 : k.screen) == null ? void 0 : I.width) && { screen_width: e.device.screen.width },
252
- ...((E = (_ = e == null ? void 0 : e.device) == null ? void 0 : _.screen) == null ? void 0 : E.height) && { screen_height: e.device.screen.height },
253
- ...((z = (P = e == null ? void 0 : e.device) == null ? void 0 : P.viewport) == null ? void 0 : z.width) && { sec_ch_viewport_width: String(e.device.viewport.width) },
254
- ...((B = e == null ? void 0 : e.device) == null ? void 0 : B.language) && { accept_language: e.device.language },
255
- ...((A = e == null ? void 0 : e.device) == null ? void 0 : A.type) && { sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0" },
256
- ...((F = e == null ? void 0 : e.device) == null ? void 0 : F.os) && { sec_ch_ua_platform: e.device.os },
269
+ ...((E = e == null ? void 0 : e.page) == null ? void 0 : E.referrer) && { referrer: e.page.referrer },
270
+ ...((k = (I = e == null ? void 0 : e.device) == null ? void 0 : I.screen) == null ? void 0 : k.width) && {
271
+ screen_width: e.device.screen.width
272
+ },
273
+ ...((_ = (S = e == null ? void 0 : e.device) == null ? void 0 : S.screen) == null ? void 0 : _.height) && {
274
+ screen_height: e.device.screen.height
275
+ },
276
+ ...((P = (z = e == null ? void 0 : e.device) == null ? void 0 : z.viewport) == null ? void 0 : P.width) && {
277
+ sec_ch_viewport_width: String(e.device.viewport.width)
278
+ },
279
+ ...((U = e == null ? void 0 : e.device) == null ? void 0 : U.language) && {
280
+ accept_language: e.device.language
281
+ },
282
+ ...((K = e == null ? void 0 : e.device) == null ? void 0 : K.type) && {
283
+ sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0"
284
+ },
285
+ ...((A = e == null ? void 0 : e.device) == null ? void 0 : A.os) && { sec_ch_ua_platform: e.device.os },
257
286
  ...i && {
258
287
  tags: Object.fromEntries(
259
288
  Object.entries(i).filter(
260
- ([, v]) => typeof v == "string" || typeof v == "number" || typeof v == "boolean"
289
+ ([, f]) => typeof f == "string" || typeof f == "number" || typeof f == "boolean"
261
290
  )
262
291
  )
263
292
  }
264
293
  };
265
- this.client.hit(g).catch((v) => {
266
- console.error("[Pirsch-Server] Failed to track page view:", v);
294
+ this.client.hit(p).catch((f) => {
295
+ console.error("[Pirsch-Server] Failed to track page view:", f);
267
296
  }), this.log("Tracked page view", { properties: i, context: e });
268
297
  }
269
298
  async reset() {
@@ -281,80 +310,271 @@ class W extends V {
281
310
  this.client = void 0, this.initialized = !1, this.log("Shutdown complete");
282
311
  }
283
312
  }
284
- async function R(p, u, i) {
285
- var e, r, h, s;
313
+ class G extends B {
314
+ constructor(i) {
315
+ super({ debug: i.debug, enabled: i.enabled });
316
+ o(this, "name", "EmitKit-Server");
317
+ o(this, "client");
318
+ o(this, "initialized", !1);
319
+ o(this, "config");
320
+ o(this, "currentUserId");
321
+ o(this, "currentUserEmail");
322
+ this.config = i;
323
+ }
324
+ async initialize() {
325
+ if (this.isEnabled() && !this.initialized) {
326
+ if (!this.config.apiKey || typeof this.config.apiKey != "string")
327
+ throw new Error("EmitKit requires an apiKey");
328
+ this.config.apiKey.startsWith("emitkit_") || console.warn(
329
+ "[EmitKit-Server] API key should start with 'emitkit_'. Double check your configuration."
330
+ );
331
+ try {
332
+ const { EmitKit: i } = await import("../index-CBs091W0.js");
333
+ this.client = new i(this.config.apiKey), this.initialized = !0, this.log("Initialized successfully");
334
+ } catch (i) {
335
+ throw console.error(
336
+ "[EmitKit-Server] Failed to initialize. Make sure @emitkit/js is installed:",
337
+ i
338
+ ), i;
339
+ }
340
+ }
341
+ }
342
+ identify(i, e) {
343
+ if (!this.isEnabled() || !this.initialized || !this.client) return;
344
+ this.currentUserId = i;
345
+ const r = (e == null ? void 0 : e.email) || i;
346
+ r && r.includes("@") && (this.currentUserEmail = r);
347
+ const a = [];
348
+ i && a.push(i), r && r !== i && a.push(r), e != null && e.username && typeof e.username == "string" && a.push(e.username), this.client.identify({
349
+ user_id: i,
350
+ properties: e || {},
351
+ aliases: a.length > 0 ? a : void 0
352
+ }).then((s) => {
353
+ var d, p, u, l, h;
354
+ this.log("Identified user", {
355
+ userId: i,
356
+ email: r,
357
+ identityId: s.data.id,
358
+ aliasesCreated: ((p = (d = s.data.aliases) == null ? void 0 : d.created) == null ? void 0 : p.length) || 0,
359
+ aliasesFailed: ((l = (u = s.data.aliases) == null ? void 0 : u.failed) == null ? void 0 : l.length) || 0
360
+ }), (h = s.data.aliases) != null && h.failed && s.data.aliases.failed.length > 0 && console.warn(
361
+ "[EmitKit-Server] Some aliases failed to create:",
362
+ s.data.aliases.failed
363
+ );
364
+ }).catch((s) => {
365
+ console.error("[EmitKit-Server] Failed to identify user:", s);
366
+ });
367
+ }
368
+ async track(i, e) {
369
+ var u, l, h;
370
+ if (!this.isEnabled() || !this.initialized || !this.client) return;
371
+ const r = ((u = e == null ? void 0 : e.user) == null ? void 0 : u.email) || ((l = e == null ? void 0 : e.user) == null ? void 0 : l.userId) || i.userId || this.currentUserEmail || this.currentUserId, a = this.formatEventTitle(i.action), s = {
372
+ ...i.properties,
373
+ category: i.category,
374
+ timestamp: i.timestamp || Date.now(),
375
+ ...i.sessionId && { sessionId: i.sessionId },
376
+ ...(e == null ? void 0 : e.page) && {
377
+ page: {
378
+ url: e.page.url,
379
+ host: e.page.host,
380
+ path: e.page.path,
381
+ title: e.page.title,
382
+ protocol: e.page.protocol,
383
+ referrer: e.page.referrer,
384
+ ...e.page.search && { search: e.page.search }
385
+ }
386
+ },
387
+ ...(e == null ? void 0 : e.device) && { device: e.device },
388
+ ...(e == null ? void 0 : e.utm) && { utm: e.utm },
389
+ ...(e == null ? void 0 : e.server) && { server: e.server }
390
+ }, d = [];
391
+ i.category && d.push(i.category), (h = i.properties) != null && h.tags && Array.isArray(i.properties.tags) && i.properties.tags.every((n) => typeof n == "string") && d.push(...i.properties.tags);
392
+ const p = this.config.channelName || "analytics";
393
+ try {
394
+ const n = await this.client.events.create({
395
+ channelName: p,
396
+ title: a,
397
+ description: this.getEventDescription(i, e),
398
+ icon: this.getEventIcon(i.category),
399
+ tags: d.length > 0 ? d : void 0,
400
+ metadata: s,
401
+ userId: r || null,
402
+ notify: this.config.notify ?? !0,
403
+ displayAs: this.config.displayAs || "notification",
404
+ source: "stacksee-analytics"
405
+ });
406
+ this.log("Tracked event", {
407
+ eventId: n.data.id,
408
+ action: i.action,
409
+ userId: r,
410
+ channelName: p
411
+ });
412
+ } catch (n) {
413
+ throw console.error("[EmitKit-Server] Failed to track event:", n), n;
414
+ }
415
+ }
416
+ pageView(i, e) {
417
+ var d, p, u;
418
+ if (!this.isEnabled() || !this.initialized || !this.client) return;
419
+ const r = ((d = e == null ? void 0 : e.user) == null ? void 0 : d.email) || ((p = e == null ? void 0 : e.user) == null ? void 0 : p.userId) || this.currentUserEmail || this.currentUserId, a = {
420
+ ...i,
421
+ date: (/* @__PURE__ */ new Date()).toISOString(),
422
+ ...(e == null ? void 0 : e.page) && {
423
+ page: {
424
+ url: e.page.url,
425
+ host: e.page.host,
426
+ path: e.page.path,
427
+ title: e.page.title,
428
+ protocol: e.page.protocol,
429
+ referrer: e.page.referrer,
430
+ ...e.page.search && { search: e.page.search }
431
+ }
432
+ },
433
+ ...(e == null ? void 0 : e.device) && { device: e.device },
434
+ ...(e == null ? void 0 : e.utm) && { utm: e.utm },
435
+ ...(e == null ? void 0 : e.server) && { server: e.server }
436
+ }, s = this.config.channelName || "analytics";
437
+ this.client.events.create({
438
+ channelName: s,
439
+ title: "Page View",
440
+ description: ((u = e == null ? void 0 : e.page) == null ? void 0 : u.path) || "User viewed a page",
441
+ icon: "👁️",
442
+ tags: ["page_view", "navigation"],
443
+ metadata: a,
444
+ userId: r || null,
445
+ notify: !1,
446
+ // Don't notify for page views by default
447
+ displayAs: "message",
448
+ source: "stacksee-analytics"
449
+ }).then((l) => {
450
+ var h;
451
+ this.log("Tracked page view", {
452
+ eventId: l.data.id,
453
+ path: (h = e == null ? void 0 : e.page) == null ? void 0 : h.path,
454
+ userId: r
455
+ });
456
+ }).catch((l) => {
457
+ console.error("[EmitKit-Server] Failed to track page view:", l);
458
+ });
459
+ }
460
+ async reset() {
461
+ !this.isEnabled() || !this.initialized || !this.client || (this.currentUserId = void 0, this.currentUserEmail = void 0, this.log("Reset user session"));
462
+ }
463
+ async shutdown() {
464
+ this.client = void 0, this.initialized = !1, this.log("Shutdown complete");
465
+ }
466
+ // ============================================================================
467
+ // Helper Methods
468
+ // ============================================================================
469
+ /**
470
+ * Format event action into a human-readable title
471
+ * Converts: "user_signed_up" -> "User Signed Up"
472
+ */
473
+ formatEventTitle(i) {
474
+ return i.split("_").map((e) => e.charAt(0).toUpperCase() + e.slice(1)).join(" ");
475
+ }
476
+ /**
477
+ * Generate a description for the event
478
+ */
479
+ getEventDescription(i, e) {
480
+ var a;
481
+ return (a = i.properties) != null && a.description && typeof i.properties.description == "string" ? i.properties.description : {
482
+ engagement: "User interaction event",
483
+ user: "User lifecycle event",
484
+ navigation: "Navigation event",
485
+ error: "Error or exception occurred",
486
+ performance: "Performance metric",
487
+ conversion: "Conversion event"
488
+ }[i.category] || void 0;
489
+ }
490
+ /**
491
+ * Get an appropriate icon for the event category
492
+ */
493
+ getEventIcon(i) {
494
+ return {
495
+ engagement: "👆",
496
+ user: "👤",
497
+ navigation: "🧭",
498
+ error: "❌",
499
+ performance: "⚡",
500
+ conversion: "💰"
501
+ }[i];
502
+ }
503
+ }
504
+ async function T(g, t, i) {
505
+ var e, r, a, s;
286
506
  try {
287
- const d = await p.json();
507
+ const d = await g.json();
288
508
  if (!d.events || !Array.isArray(d.events))
289
509
  throw new Error("Invalid payload: missing events array");
290
- const g = i != null && i.extractIp ? i.extractIp(p) : T(p), n = p.headers.get("user-agent"), l = i != null && i.enrichContext ? i.enrichContext(p) : {};
291
- for (const a of d.events)
510
+ const p = i != null && i.extractIp ? i.extractIp(g) : N(g), u = g.headers.get("user-agent"), l = i != null && i.enrichContext ? i.enrichContext(g) : {};
511
+ for (const h of d.events)
292
512
  try {
293
- switch (a.type) {
513
+ switch (h.type) {
294
514
  case "track": {
295
- const o = {
296
- ...a.context,
515
+ const n = {
516
+ ...h.context,
297
517
  ...l,
298
518
  server: {
299
- ...(e = a.context) == null ? void 0 : e.server,
519
+ ...(e = h.context) == null ? void 0 : e.server,
300
520
  ...typeof (l == null ? void 0 : l.server) == "object" && l.server !== null ? l.server : {},
301
- ...n ? { userAgent: n } : {}
521
+ ...u ? { userAgent: u } : {}
302
522
  },
303
523
  device: {
304
- ...(r = a.context) == null ? void 0 : r.device,
305
- ...g ? { ip: g } : {}
524
+ ...(r = h.context) == null ? void 0 : r.device,
525
+ ...p ? { ip: p } : {}
306
526
  }
307
527
  };
308
- await u.track(
309
- a.event.action,
528
+ await t.track(
529
+ h.event.action,
310
530
  // biome-ignore lint/suspicious/noExplicitAny: Properties from JSON cannot be type-checked against TEventMap at compile time
311
- a.event.properties,
531
+ h.event.properties,
312
532
  {
313
- userId: a.event.userId,
314
- sessionId: a.event.sessionId,
315
- context: o
533
+ userId: h.event.userId,
534
+ sessionId: h.event.sessionId,
535
+ context: n
316
536
  }
317
537
  );
318
538
  break;
319
539
  }
320
540
  case "identify": {
321
- u.identify(a.userId, a.traits);
541
+ t.identify(h.userId, h.traits);
322
542
  break;
323
543
  }
324
544
  case "pageView": {
325
- const o = {
326
- ...a.context,
545
+ const n = {
546
+ ...h.context,
327
547
  ...l,
328
548
  server: {
329
- ...(h = a.context) == null ? void 0 : h.server,
549
+ ...(a = h.context) == null ? void 0 : a.server,
330
550
  ...typeof (l == null ? void 0 : l.server) == "object" && l.server !== null ? l.server : {},
331
- ...n ? { userAgent: n } : {}
551
+ ...u ? { userAgent: u } : {}
332
552
  },
333
553
  device: {
334
- ...(s = a.context) == null ? void 0 : s.device,
335
- ...g ? { ip: g } : {}
554
+ ...(s = h.context) == null ? void 0 : s.device,
555
+ ...p ? { ip: p } : {}
336
556
  }
337
557
  };
338
- u.pageView(a.properties, {
339
- context: o
558
+ t.pageView(h.properties, {
559
+ context: n
340
560
  });
341
561
  break;
342
562
  }
343
563
  case "reset":
344
564
  break;
345
565
  default:
346
- console.warn("[Proxy] Unknown event type:", a);
566
+ console.warn("[Proxy] Unknown event type:", h);
347
567
  }
348
- } catch (o) {
349
- i != null && i.onError ? i.onError(o) : console.error("[Proxy] Failed to process event:", o);
568
+ } catch (n) {
569
+ i != null && i.onError ? i.onError(n) : console.error("[Proxy] Failed to process event:", n);
350
570
  }
351
571
  } catch (d) {
352
572
  throw i != null && i.onError ? i.onError(d) : console.error("[Proxy] Failed to ingest events:", d), d;
353
573
  }
354
574
  }
355
- function T(p) {
575
+ function N(g) {
356
576
  var i;
357
- const u = [
577
+ const t = [
358
578
  "x-forwarded-for",
359
579
  "x-real-ip",
360
580
  "cf-connecting-ip",
@@ -362,26 +582,27 @@ function T(p) {
362
582
  "x-client-ip",
363
583
  "x-cluster-client-ip"
364
584
  ];
365
- for (const e of u) {
366
- const r = p.headers.get(e);
585
+ for (const e of t) {
586
+ const r = g.headers.get(e);
367
587
  if (r)
368
588
  return (i = r.split(",")[0]) == null ? void 0 : i.trim();
369
589
  }
370
590
  }
371
- function G(p, u) {
591
+ function J(g, t) {
372
592
  return async (i) => {
373
593
  try {
374
- return await R(i, p, u), new Response("OK", { status: 200 });
594
+ return await T(i, g, t), new Response("OK", { status: 200 });
375
595
  } catch (e) {
376
596
  return console.error("[Proxy] Handler error:", e), new Response("Internal Server Error", { status: 500 });
377
597
  }
378
598
  };
379
599
  }
380
600
  export {
381
- V as BaseAnalyticsProvider,
382
- N as BentoServerProvider,
601
+ B as BaseAnalyticsProvider,
602
+ H as BentoServerProvider,
603
+ G as EmitKitServerProvider,
383
604
  W as PirschServerProvider,
384
- Q as PostHogServerProvider,
385
- G as createProxyHandler,
386
- R as ingestProxyEvents
605
+ X as PostHogServerProvider,
606
+ J as createProxyHandler,
607
+ T as ingestProxyEvents
387
608
  };