@stacksee/analytics 0.9.6 → 0.9.7

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,9 +1,9 @@
1
1
  var j = Object.defineProperty;
2
- var q = (d, h, i) => h in d ? j(d, h, { enumerable: !0, configurable: !0, writable: !0, value: i }) : d[h] = i;
3
- var g = (d, h, i) => q(d, typeof h != "symbol" ? h + "" : h, i);
4
- import { B as V } from "../base.provider-AfFL5W_P.js";
2
+ var D = (d, h, i) => h in d ? j(d, h, { enumerable: !0, configurable: !0, writable: !0, value: i }) : d[h] = i;
3
+ var g = (d, h, i) => D(d, typeof h != "symbol" ? h + "" : h, i);
4
+ import { B as q } from "../base.provider-AfFL5W_P.js";
5
5
  import { P as Q } from "../server-DjEk1fUD.js";
6
- class M extends V {
6
+ class N extends q {
7
7
  constructor(i) {
8
8
  super({ debug: i.debug, enabled: i.enabled });
9
9
  g(this, "name", "Bento-Server");
@@ -23,7 +23,7 @@ class M 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: s, enabled: a, ...u } = this.config;
26
+ const { Analytics: r } = await import("../bento-node-sdk.esm-CWEAoj97.js"), { debug: a, enabled: s, ...u } = this.config;
27
27
  this.client = new r(u), this.initialized = !0, this.log("Initialized successfully", {
28
28
  siteUuid: this.config.siteUuid
29
29
  });
@@ -43,12 +43,12 @@ class M extends V {
43
43
  return;
44
44
  }
45
45
  this.currentUserEmail = r;
46
- const s = e ? { ...e } : {};
47
- delete s.email, this.client.V1.addSubscriber({
46
+ const a = e ? { ...e } : {};
47
+ delete a.email, this.client.V1.addSubscriber({
48
48
  email: r,
49
- fields: s
50
- }).catch((a) => {
51
- console.error("[Bento-Server] Failed to identify user:", a);
49
+ fields: a
50
+ }).catch((s) => {
51
+ console.error("[Bento-Server] Failed to identify user:", s);
52
52
  }), this.log("Identified user", { userId: i, email: r, traits: e });
53
53
  }
54
54
  async track(i, e) {
@@ -61,7 +61,7 @@ class M extends V {
61
61
  );
62
62
  return;
63
63
  }
64
- const s = {
64
+ const a = {
65
65
  ...i.properties,
66
66
  category: i.category,
67
67
  timestamp: i.timestamp || Date.now(),
@@ -81,16 +81,16 @@ class M extends V {
81
81
  ...(e == null ? void 0 : e.utm) && { utm: e.utm },
82
82
  site: this.config.siteUuid,
83
83
  ...((p = e == null ? void 0 : e.user) == null ? void 0 : p.userId) && { visitor: e.user.userId }
84
- }, a = ((n = e == null ? void 0 : e.user) == null ? void 0 : n.traits) || {};
84
+ }, s = ((n = e == null ? void 0 : e.user) == null ? void 0 : n.traits) || {};
85
85
  try {
86
86
  await this.client.V1.track({
87
87
  email: r,
88
88
  type: `$${i.action}`,
89
- details: s,
90
- fields: a
89
+ details: a,
90
+ fields: s
91
91
  }), this.log("Tracked event", { event: i, context: e });
92
- } catch (v) {
93
- console.error("[Bento-Server] Failed to track event:", v);
92
+ } catch (t) {
93
+ console.error("[Bento-Server] Failed to track event:", t);
94
94
  }
95
95
  }
96
96
  pageView(i, e) {
@@ -103,7 +103,7 @@ class M extends V {
103
103
  );
104
104
  return;
105
105
  }
106
- const s = {
106
+ const a = {
107
107
  ...i,
108
108
  date: (/* @__PURE__ */ new Date()).toISOString(),
109
109
  ...(e == null ? void 0 : e.page) && {
@@ -119,12 +119,12 @@ class M extends V {
119
119
  },
120
120
  site: this.config.siteUuid,
121
121
  ...((l = e == null ? void 0 : e.user) == null ? void 0 : l.userId) && { visitor: e.user.userId }
122
- }, a = ((p = e == null ? void 0 : e.user) == null ? void 0 : p.traits) || {};
122
+ }, s = ((p = e == null ? void 0 : e.user) == null ? void 0 : p.traits) || {};
123
123
  this.client.V1.track({
124
124
  email: r,
125
125
  type: "$view",
126
- details: s,
127
- fields: a
126
+ details: a,
127
+ fields: s
128
128
  }).catch((n) => {
129
129
  console.error("[Bento-Server] Failed to track page view:", n);
130
130
  }), this.log("Tracked page view", { properties: i, context: e });
@@ -136,7 +136,7 @@ class M extends V {
136
136
  this.client = void 0, this.initialized = !1, this.log("Shutdown complete");
137
137
  }
138
138
  }
139
- class N extends V {
139
+ class W extends q {
140
140
  constructor(i) {
141
141
  super({ debug: i.debug, enabled: i.enabled });
142
142
  g(this, "name", "Pirsch-Server");
@@ -146,22 +146,27 @@ class N extends V {
146
146
  this.config = i;
147
147
  }
148
148
  async initialize() {
149
- if (this.isEnabled() && !this.initialized) {
150
- if (!this.config.hostname || typeof this.config.hostname != "string")
151
- throw new Error("Pirsch requires a hostname");
152
- if (!this.config.clientSecret || typeof this.config.clientSecret != "string")
153
- throw new Error("Pirsch requires a clientSecret (or access key)");
154
- try {
155
- const { Pirsch: i } = await import("../index-zS7gy63J.js").then((a) => a.i), { debug: e, enabled: r, ...s } = this.config;
156
- this.client = new i(s), this.initialized = !0, this.log("Initialized successfully", {
157
- hostname: this.config.hostname
158
- });
159
- } catch (i) {
160
- throw console.error(
161
- "[Pirsch-Server] Failed to initialize. Make sure pirsch-sdk is installed:",
162
- i
163
- ), i;
164
- }
149
+ if (!this.isEnabled() || this.initialized) return;
150
+ if (!this.config.hostname || typeof this.config.hostname != "string")
151
+ throw new Error("Pirsch requires a hostname");
152
+ if (!this.config.clientSecret || typeof this.config.clientSecret != "string")
153
+ throw new Error("Pirsch requires a clientSecret (or access key)");
154
+ const i = this.config.clientSecret.startsWith("pa_");
155
+ if (!i && !this.config.clientId)
156
+ throw new Error(
157
+ "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
+ );
159
+ try {
160
+ const { Pirsch: e } = await import("../index-zS7gy63J.js").then((u) => u.i), { debug: r, enabled: a, ...s } = this.config;
161
+ this.client = new e(s), this.initialized = !0, this.log("Initialized successfully", {
162
+ hostname: this.config.hostname,
163
+ authMode: i ? "access-key" : "oauth"
164
+ });
165
+ } catch (e) {
166
+ throw console.error(
167
+ "[Pirsch-Server] Failed to initialize. Make sure pirsch-sdk is installed:",
168
+ e
169
+ ), e;
165
170
  }
166
171
  }
167
172
  identify(i, e) {
@@ -170,30 +175,39 @@ class N extends V {
170
175
  url: "https://identify",
171
176
  ip: "0.0.0.0",
172
177
  user_agent: "analytics-library"
173
- }, s = {
178
+ }, a = {
174
179
  userId: i,
175
180
  ...e && Object.fromEntries(
176
181
  Object.entries(e).filter(
177
- ([, a]) => typeof a == "string" || typeof a == "number" || typeof a == "boolean"
182
+ ([, s]) => typeof s == "string" || typeof s == "number" || typeof s == "boolean"
178
183
  )
179
184
  )
180
185
  };
181
- this.client.event("user_identified", r, 0, s).catch((a) => {
182
- console.error("[Pirsch-Server] Failed to track identify event:", a);
186
+ this.client.event("user_identified", r, 0, a).catch((s) => {
187
+ console.error("[Pirsch-Server] Failed to track identify event:", s);
183
188
  }), this.log("Identified user via event", { userId: i, traits: e });
184
189
  }
185
190
  async track(i, e) {
186
- var v, f, y, t, w, m, b, c, k, S, _, E, I, z, P, B, F, o, $, A, K, O;
191
+ var t, f, v, y, w, c, m, b, S, k, I, _, E, P, z, B, A, F, o, $, K, O, V;
187
192
  if (!this.isEnabled() || !this.initialized || !this.client) return;
188
- const r = e, s = ((v = r == null ? void 0 : r.device) == null ? void 0 : v.ip) || "0.0.0.0", a = ((f = r == null ? void 0 : r.server) == null ? void 0 : f.userAgent) || ((y = r == null ? void 0 : r.device) == null ? void 0 : y.userAgent) || "unknown", l = {
189
- url: ((t = e == null ? void 0 : e.page) == null ? void 0 : t.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}` : (c = e == null ? void 0 : e.page) != null && c.path ? `https://${this.config.hostname}${e.page.path}` : "https://event"),
190
- ip: s,
191
- user_agent: a,
193
+ const r = e, a = ((t = r == null ? void 0 : r.device) == null ? void 0 : t.ip) || ((f = r == null ? void 0 : r.server) == null ? void 0 : f.ip), s = ((v = r == null ? void 0 : r.server) == null ? void 0 : v.userAgent) || ((y = r == null ? void 0 : r.device) == null ? void 0 : y.userAgent);
194
+ if (!a || !s) {
195
+ this.log("Skipping event - missing required IP or user-agent from context", {
196
+ hasIp: !!a,
197
+ hasUserAgent: !!s,
198
+ event: i.action
199
+ });
200
+ return;
201
+ }
202
+ const l = {
203
+ url: ((w = e == null ? void 0 : e.page) == null ? void 0 : w.url) || ((c = e == null ? void 0 : e.page) != null && c.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: a,
205
+ user_agent: s,
192
206
  ...((k = e == null ? void 0 : e.page) == null ? void 0 : k.title) && { title: e.page.title },
193
- ...((S = e == null ? void 0 : e.page) == null ? void 0 : S.referrer) && { referrer: e.page.referrer },
207
+ ...((I = e == null ? void 0 : e.page) == null ? void 0 : I.referrer) && { referrer: e.page.referrer },
194
208
  ...((E = (_ = e == null ? void 0 : e.device) == null ? void 0 : _.screen) == null ? void 0 : E.width) && { screen_width: e.device.screen.width },
195
- ...((z = (I = e == null ? void 0 : e.device) == null ? void 0 : I.screen) == null ? void 0 : z.height) && { screen_height: e.device.screen.height },
196
- ...((B = (P = e == null ? void 0 : e.device) == null ? void 0 : P.viewport) == null ? void 0 : B.width) && { sec_ch_viewport_width: String(e.device.viewport.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) },
197
211
  ...((F = e == null ? void 0 : e.device) == null ? void 0 : F.language) && { accept_language: e.device.language },
198
212
  ...((o = e == null ? void 0 : e.device) == null ? void 0 : o.type) && { sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0" },
199
213
  ...(($ = e == null ? void 0 : e.device) == null ? void 0 : $.os) && { sec_ch_ua_platform: e.device.os }
@@ -207,9 +221,9 @@ class N extends V {
207
221
  timestamp: String(i.timestamp || Date.now()),
208
222
  ...i.userId && { userId: i.userId },
209
223
  ...i.sessionId && { sessionId: i.sessionId },
210
- ...((A = e == null ? void 0 : e.user) == null ? void 0 : A.email) && { user_email: e.user.email },
211
- ...((K = e == null ? void 0 : e.device) == null ? void 0 : K.timezone) && { timezone: e.device.timezone },
212
- ...((O = e == null ? void 0 : e.device) == null ? void 0 : O.browser) && { browser: e.device.browser }
224
+ ...((K = e == null ? void 0 : e.user) == null ? void 0 : K.email) && { user_email: e.user.email },
225
+ ...((O = e == null ? void 0 : e.device) == null ? void 0 : O.timezone) && { timezone: e.device.timezone },
226
+ ...((V = e == null ? void 0 : e.device) == null ? void 0 : V.browser) && { browser: e.device.browser }
213
227
  };
214
228
  try {
215
229
  await this.client.event(i.action, l, 0, n), this.log("Tracked event", { event: i, context: e });
@@ -218,19 +232,27 @@ class N extends V {
218
232
  }
219
233
  }
220
234
  pageView(i, e) {
221
- var p, n, v, f, y, t, w, m, b, c, k, S, _, E, I, z, P, B, F;
235
+ var p, n, t, f, v, y, w, c, m, b, S, k, I, _, E, P, z, B, A, F;
222
236
  if (!this.isEnabled() || !this.initialized || !this.client) return;
223
- const r = e, s = ((p = r == null ? void 0 : r.device) == null ? void 0 : p.ip) || "0.0.0.0", a = ((n = r == null ? void 0 : r.server) == null ? void 0 : n.userAgent) || ((v = r == null ? void 0 : r.device) == null ? void 0 : v.userAgent) || "unknown", l = {
224
- url: ((f = e == null ? void 0 : e.page) == null ? void 0 : f.url) || ((y = e == null ? void 0 : e.page) != null && y.protocol && ((t = e == null ? void 0 : e.page) != null && t.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"),
225
- ip: s,
226
- user_agent: a,
237
+ const r = e, a = ((p = r == null ? void 0 : r.device) == null ? void 0 : p.ip) || ((n = r == null ? void 0 : r.server) == null ? void 0 : n.ip), s = ((t = r == null ? void 0 : r.server) == null ? void 0 : t.userAgent) || ((f = r == null ? void 0 : r.device) == null ? void 0 : f.userAgent);
238
+ if (!a || !s) {
239
+ this.log("Skipping pageView - missing required IP or user-agent from context", {
240
+ hasIp: !!a,
241
+ hasUserAgent: !!s
242
+ });
243
+ return;
244
+ }
245
+ const l = {
246
+ url: ((v = e == null ? void 0 : e.page) == null ? void 0 : v.url) || ((y = e == null ? void 0 : e.page) != null && y.protocol && ((w = e == null ? void 0 : e.page) != null && w.host) && ((c = e == null ? void 0 : e.page) != null && c.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: a,
248
+ user_agent: s,
227
249
  ...((b = e == null ? void 0 : e.page) == null ? void 0 : b.title) && { title: e.page.title },
228
- ...((c = e == null ? void 0 : e.page) == null ? void 0 : c.referrer) && { referrer: e.page.referrer },
229
- ...((S = (k = e == null ? void 0 : e.device) == null ? void 0 : k.screen) == null ? void 0 : S.width) && { screen_width: e.device.screen.width },
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 },
230
252
  ...((E = (_ = e == null ? void 0 : e.device) == null ? void 0 : _.screen) == null ? void 0 : E.height) && { screen_height: e.device.screen.height },
231
- ...((z = (I = e == null ? void 0 : e.device) == null ? void 0 : I.viewport) == null ? void 0 : z.width) && { sec_ch_viewport_width: String(e.device.viewport.width) },
232
- ...((P = e == null ? void 0 : e.device) == null ? void 0 : P.language) && { accept_language: e.device.language },
233
- ...((B = e == null ? void 0 : e.device) == null ? void 0 : B.type) && { sec_ch_ua_mobile: e.device.type === "mobile" || e.device.type === "tablet" ? "?1" : "?0" },
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" },
234
256
  ...((F = e == null ? void 0 : e.device) == null ? void 0 : F.os) && { sec_ch_ua_platform: e.device.os },
235
257
  ...i && {
236
258
  tags: Object.fromEntries(
@@ -259,14 +281,14 @@ class N extends V {
259
281
  this.client = void 0, this.initialized = !1, this.log("Shutdown complete");
260
282
  }
261
283
  }
262
- async function D(d, h, i) {
284
+ async function R(d, h, i) {
263
285
  var e, r;
264
286
  try {
265
- const s = await d.json();
266
- if (!s.events || !Array.isArray(s.events))
287
+ const a = await d.json();
288
+ if (!a.events || !Array.isArray(a.events))
267
289
  throw new Error("Invalid payload: missing events array");
268
- const a = i != null && i.extractIp ? i.extractIp(d) : R(d), u = i != null && i.enrichContext ? i.enrichContext(d) : {};
269
- for (const l of s.events)
290
+ const s = i != null && i.extractIp ? i.extractIp(d) : T(d), u = i != null && i.enrichContext ? i.enrichContext(d) : {};
291
+ for (const l of a.events)
270
292
  try {
271
293
  switch (l.type) {
272
294
  case "track": {
@@ -277,7 +299,7 @@ async function D(d, h, i) {
277
299
  ...(e = l.context) == null ? void 0 : e.device,
278
300
  // Add IP (using type assertion for extended fields)
279
301
  // biome-ignore lint/suspicious/noExplicitAny: IP field not in base device type
280
- ...a ? { ip: a } : {}
302
+ ...s ? { ip: s } : {}
281
303
  }
282
304
  };
283
305
  await h.track(l.event.action, l.event.properties, {
@@ -300,7 +322,7 @@ async function D(d, h, i) {
300
322
  ...(r = l.context) == null ? void 0 : r.device,
301
323
  // biome-ignore lint/suspicious/noExplicitAny: IP field not in base device type
302
324
  // Add IP (using type assertion for extended fields)
303
- ...a ? { ip: a } : {}
325
+ ...s ? { ip: s } : {}
304
326
  }
305
327
  };
306
328
  h.pageView(l.properties, p);
@@ -314,11 +336,11 @@ async function D(d, h, i) {
314
336
  } catch (p) {
315
337
  i != null && i.onError ? i.onError(p) : console.error("[Proxy] Failed to process event:", p);
316
338
  }
317
- } catch (s) {
318
- throw i != null && i.onError ? i.onError(s) : console.error("[Proxy] Failed to ingest events:", s), s;
339
+ } catch (a) {
340
+ throw i != null && i.onError ? i.onError(a) : console.error("[Proxy] Failed to ingest events:", a), a;
319
341
  }
320
342
  }
321
- function R(d) {
343
+ function T(d) {
322
344
  var i;
323
345
  const h = [
324
346
  "x-forwarded-for",
@@ -337,17 +359,17 @@ function R(d) {
337
359
  function G(d, h) {
338
360
  return async (i) => {
339
361
  try {
340
- return await D(i, d, h), new Response("OK", { status: 200 });
362
+ return await R(i, d, h), new Response("OK", { status: 200 });
341
363
  } catch (e) {
342
364
  return console.error("[Proxy] Handler error:", e), new Response("Internal Server Error", { status: 500 });
343
365
  }
344
366
  };
345
367
  }
346
368
  export {
347
- V as BaseAnalyticsProvider,
348
- M as BentoServerProvider,
349
- N as PirschServerProvider,
369
+ q as BaseAnalyticsProvider,
370
+ N as BentoServerProvider,
371
+ W as PirschServerProvider,
350
372
  Q as PostHogServerProvider,
351
373
  G as createProxyHandler,
352
- D as ingestProxyEvents
374
+ R as ingestProxyEvents
353
375
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacksee/analytics",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "A highly typed, provider-agnostic analytics library for TypeScript applications",
5
5
  "type": "module",
6
6
  "exports": {