@hanzo/event 0.3.25 → 0.3.27

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/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as Cohort, A as Attribution, S as SentryEvent, D as Dsn, a as CaptureErrorOptions, b as SentryFrame, E as ExceptionEntry, c as SentryLevel, d as ExceptionProperties } from './core-DO8tBUy7.cjs';
2
- export { e as Analytics, f as AnalyticsConfig, g as EventKind, h as Exception, i as ExceptionFrame, T as Transport, W as WireEvent, j as createAnalytics } from './core-DO8tBUy7.cjs';
1
+ import { C as Cohort, A as Attribution, S as SentryEvent, D as Dsn, a as CaptureErrorOptions, b as SentryFrame, E as ExceptionEntry, c as SentryLevel, d as ExceptionProperties } from './core-usULsfUB.cjs';
2
+ export { e as Analytics, f as AnalyticsConfig, g as EventKind, h as Exception, i as ExceptionFrame, T as Transport, W as WireEvent, j as createAnalytics } from './core-usULsfUB.cjs';
3
3
  import { FunnelId } from '@hanzo/events';
4
4
  export { EVENTS, EXCEPTION, EventName, FUNNELS, FunnelDef, FunnelId, FunnelStep, PAGEVIEW, PRODUCTS, ProductId, eventsOf } from '@hanzo/events';
5
5
 
@@ -8,7 +8,7 @@ declare function getFirstTouch(): Attribution | undefined;
8
8
  /** Read persisted cohort dimensions. */
9
9
  declare function getCohort(): Cohort | undefined;
10
10
 
11
- declare const VERSION = "0.3.25";
11
+ declare const VERSION = "0.3.27";
12
12
 
13
13
  /**
14
14
  * parseDsn parses "https://<version>:<hmac>@<host>/v1/sentry/<projectId>" into its
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as Cohort, A as Attribution, S as SentryEvent, D as Dsn, a as CaptureErrorOptions, b as SentryFrame, E as ExceptionEntry, c as SentryLevel, d as ExceptionProperties } from './core-DO8tBUy7.js';
2
- export { e as Analytics, f as AnalyticsConfig, g as EventKind, h as Exception, i as ExceptionFrame, T as Transport, W as WireEvent, j as createAnalytics } from './core-DO8tBUy7.js';
1
+ import { C as Cohort, A as Attribution, S as SentryEvent, D as Dsn, a as CaptureErrorOptions, b as SentryFrame, E as ExceptionEntry, c as SentryLevel, d as ExceptionProperties } from './core-usULsfUB.js';
2
+ export { e as Analytics, f as AnalyticsConfig, g as EventKind, h as Exception, i as ExceptionFrame, T as Transport, W as WireEvent, j as createAnalytics } from './core-usULsfUB.js';
3
3
  import { FunnelId } from '@hanzo/events';
4
4
  export { EVENTS, EXCEPTION, EventName, FUNNELS, FunnelDef, FunnelId, FunnelStep, PAGEVIEW, PRODUCTS, ProductId, eventsOf } from '@hanzo/events';
5
5
 
@@ -8,7 +8,7 @@ declare function getFirstTouch(): Attribution | undefined;
8
8
  /** Read persisted cohort dimensions. */
9
9
  declare function getCohort(): Cohort | undefined;
10
10
 
11
- declare const VERSION = "0.3.25";
11
+ declare const VERSION = "0.3.27";
12
12
 
13
13
  /**
14
14
  * parseDsn parses "https://<version>:<hmac>@<host>/v1/sentry/<projectId>" into its
package/dist/index.mjs CHANGED
@@ -318,7 +318,7 @@ function uuidv7Time(id) {
318
318
  }
319
319
 
320
320
  // src/version.ts
321
- var VERSION = "0.3.25";
321
+ var VERSION = "0.3.27";
322
322
 
323
323
  // src/sentry.ts
324
324
  var MAX_FRAMES = 50;
@@ -735,17 +735,18 @@ var DefaultTransport = class {
735
735
  }
736
736
  }
737
737
  if (typeof fetch !== "function") return;
738
+ const simple = opts.ingestKey !== void 0 && opts.contentType === void 0;
738
739
  const headers = {
739
- "Content-Type": opts.contentType ?? "application/json"
740
+ "Content-Type": simple ? BEACON_CONTENT_TYPE : opts.contentType ?? "application/json"
740
741
  };
741
- const bearer = opts.ingestKey ?? opts.token;
742
+ const bearer = simple ? void 0 : opts.ingestKey ?? opts.token;
742
743
  if (bearer) headers.Authorization = `Bearer ${bearer}`;
743
- void fetch(url, {
744
+ void fetch(simple ? appendQuery(url, "ingest_key", opts.ingestKey) : url, {
744
745
  method: "POST",
745
746
  headers,
746
747
  body,
747
748
  keepalive: true,
748
- credentials: "include"
749
+ credentials: simple ? "omit" : "include"
749
750
  }).then((res) => {
750
751
  if (!res.ok && opts.debug) {
751
752
  console.warn("[event] ingest rejected", res.status, url.split("?")[0]);
@@ -794,6 +795,20 @@ var Analytics = class {
794
795
  config.dsn ?? readEnvDsn() ?? dsnForProduct(this.cfg.product, this.cfg.ingestKey)
795
796
  );
796
797
  }
798
+ /** adopt gives this client a credential it does not have. The key belongs to
799
+ * the stream, not to whichever caller happened to ask for the handle first,
800
+ * so a later caller carrying one hands it over. The error plane derives from
801
+ * the key, so it comes up here too when it was inert for want of one.
802
+ * Present fields are never overwritten: the first caller's key stays. */
803
+ adopt(config) {
804
+ if (config.ingestKey && !this.cfg.ingestKey) this.cfg.ingestKey = config.ingestKey;
805
+ if (config.getToken && !this.cfg.getToken) this.cfg.getToken = config.getToken;
806
+ if (!this.dsn) {
807
+ this.dsn = parseDsn(
808
+ config.dsn ?? readEnvDsn() ?? dsnForProduct(this.cfg.product, this.cfg.ingestKey)
809
+ );
810
+ }
811
+ }
797
812
  /** errorPlaneEnabled reports whether captured exceptions can actually reach the
798
813
  * error host. False means a DSN was never configured — the documented
799
814
  * fail-safe. Exposed so an app (or a test) can assert its wiring instead of
@@ -844,9 +859,14 @@ var Analytics = class {
844
859
  group(groupId, traits) {
845
860
  this.enqueue("group", void 0, { groupId, properties: traits });
846
861
  }
847
- /** pageview records a $pageview for the current (or given) location. */
862
+ /** pageview records a $pageview for the current (or given) location, once per
863
+ * view. A view is the path plus the full location, so a query or hash change
864
+ * is a new one and a repeat call for the same place is not. */
848
865
  pageview(path, properties) {
849
866
  const p = path ?? (isBrowser() ? window.location.pathname : void 0);
867
+ const view = (p ?? "") + "\0" + (isBrowser() ? window.location.href : "");
868
+ if (view === this.counted) return;
869
+ this.counted = view;
850
870
  this.enqueue("pageview", PAGEVIEW, { path: p, properties });
851
871
  }
852
872
  /** capture records a named product event with optional properties. Commerce
@@ -907,8 +927,8 @@ var Analytics = class {
907
927
  * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
908
928
  * unload-safe transport. Auth is orthogonal to the wire:
909
929
  *
910
- * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
911
- * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.
930
+ * • publishable key set → rides ?ingest_key=pk-… on both sends, keeping each
931
+ * a CORS-simple request that any origin may send.
912
932
  * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
913
933
  * carry a header, so token apps fall back to keepalive fetch on unload).
914
934
  * • else a cookie app rides same-origin credentials (beacon carries the
@@ -1031,8 +1051,27 @@ var Analytics = class {
1031
1051
  }
1032
1052
  }
1033
1053
  };
1054
+ var CLIENTS = /* @__PURE__ */ Symbol.for("hanzo.event.clients");
1055
+ function registry() {
1056
+ const g = globalThis;
1057
+ const existing = g[CLIENTS];
1058
+ if (existing) return existing;
1059
+ const fresh = /* @__PURE__ */ new Map();
1060
+ g[CLIENTS] = fresh;
1061
+ return fresh;
1062
+ }
1034
1063
  function createAnalytics(config) {
1035
- return new Analytics(config);
1064
+ if (!isBrowser()) return new Analytics(config);
1065
+ const key = (config.host ?? DEFAULT_HOST) + "\0" + config.product;
1066
+ const clients = registry();
1067
+ const existing = clients.get(key);
1068
+ if (existing) {
1069
+ existing.adopt(config);
1070
+ return existing;
1071
+ }
1072
+ const fresh = new Analytics(config);
1073
+ clients.set(key, fresh);
1074
+ return fresh;
1036
1075
  }
1037
1076
 
1038
1077
  // src/goals.ts