@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/react.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode, Component, ErrorInfo } from 'react';
3
- import { e as Analytics, f as AnalyticsConfig } from './core-DO8tBUy7.js';
3
+ import { e as Analytics, f as AnalyticsConfig } from './core-usULsfUB.js';
4
4
 
5
5
  declare const Ctx: react.Context<Analytics | null>;
6
6
  interface AnalyticsProviderProps {
package/dist/react.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, Component, useMemo, useEffect, createElement, useContext, useRef } from 'react';
1
+ import { createContext, Component, useEffect, createElement, useContext, useRef } from 'react';
2
2
  import { PAGEVIEW, EXCEPTION } from '@hanzo/events';
3
3
 
4
4
  // src/react.tsx
@@ -307,7 +307,7 @@ function hzAnonId() {
307
307
  }
308
308
 
309
309
  // src/version.ts
310
- var VERSION = "0.3.25";
310
+ var VERSION = "0.3.27";
311
311
 
312
312
  // src/sentry.ts
313
313
  var MAX_FRAMES = 50;
@@ -724,17 +724,18 @@ var DefaultTransport = class {
724
724
  }
725
725
  }
726
726
  if (typeof fetch !== "function") return;
727
+ const simple = opts.ingestKey !== void 0 && opts.contentType === void 0;
727
728
  const headers = {
728
- "Content-Type": opts.contentType ?? "application/json"
729
+ "Content-Type": simple ? BEACON_CONTENT_TYPE : opts.contentType ?? "application/json"
729
730
  };
730
- const bearer = opts.ingestKey ?? opts.token;
731
+ const bearer = simple ? void 0 : opts.ingestKey ?? opts.token;
731
732
  if (bearer) headers.Authorization = `Bearer ${bearer}`;
732
- void fetch(url, {
733
+ void fetch(simple ? appendQuery(url, "ingest_key", opts.ingestKey) : url, {
733
734
  method: "POST",
734
735
  headers,
735
736
  body,
736
737
  keepalive: true,
737
- credentials: "include"
738
+ credentials: simple ? "omit" : "include"
738
739
  }).then((res) => {
739
740
  if (!res.ok && opts.debug) {
740
741
  console.warn("[event] ingest rejected", res.status, url.split("?")[0]);
@@ -783,6 +784,20 @@ var Analytics = class {
783
784
  config.dsn ?? readEnvDsn() ?? dsnForProduct(this.cfg.product, this.cfg.ingestKey)
784
785
  );
785
786
  }
787
+ /** adopt gives this client a credential it does not have. The key belongs to
788
+ * the stream, not to whichever caller happened to ask for the handle first,
789
+ * so a later caller carrying one hands it over. The error plane derives from
790
+ * the key, so it comes up here too when it was inert for want of one.
791
+ * Present fields are never overwritten: the first caller's key stays. */
792
+ adopt(config) {
793
+ if (config.ingestKey && !this.cfg.ingestKey) this.cfg.ingestKey = config.ingestKey;
794
+ if (config.getToken && !this.cfg.getToken) this.cfg.getToken = config.getToken;
795
+ if (!this.dsn) {
796
+ this.dsn = parseDsn(
797
+ config.dsn ?? readEnvDsn() ?? dsnForProduct(this.cfg.product, this.cfg.ingestKey)
798
+ );
799
+ }
800
+ }
786
801
  /** errorPlaneEnabled reports whether captured exceptions can actually reach the
787
802
  * error host. False means a DSN was never configured — the documented
788
803
  * fail-safe. Exposed so an app (or a test) can assert its wiring instead of
@@ -833,9 +848,14 @@ var Analytics = class {
833
848
  group(groupId, traits) {
834
849
  this.enqueue("group", void 0, { groupId, properties: traits });
835
850
  }
836
- /** pageview records a $pageview for the current (or given) location. */
851
+ /** pageview records a $pageview for the current (or given) location, once per
852
+ * view. A view is the path plus the full location, so a query or hash change
853
+ * is a new one and a repeat call for the same place is not. */
837
854
  pageview(path, properties) {
838
855
  const p = path ?? (isBrowser() ? window.location.pathname : void 0);
856
+ const view = (p ?? "") + "\0" + (isBrowser() ? window.location.href : "");
857
+ if (view === this.counted) return;
858
+ this.counted = view;
839
859
  this.enqueue("pageview", PAGEVIEW, { path: p, properties });
840
860
  }
841
861
  /** capture records a named product event with optional properties. Commerce
@@ -896,8 +916,8 @@ var Analytics = class {
896
916
  * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
897
917
  * unload-safe transport. Auth is orthogonal to the wire:
898
918
  *
899
- * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
900
- * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.
919
+ * • publishable key set → rides ?ingest_key=pk-… on both sends, keeping each
920
+ * a CORS-simple request that any origin may send.
901
921
  * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
902
922
  * carry a header, so token apps fall back to keepalive fetch on unload).
903
923
  * • else a cookie app rides same-origin credentials (beacon carries the
@@ -1020,19 +1040,35 @@ var Analytics = class {
1020
1040
  }
1021
1041
  }
1022
1042
  };
1043
+ var CLIENTS = /* @__PURE__ */ Symbol.for("hanzo.event.clients");
1044
+ function registry() {
1045
+ const g = globalThis;
1046
+ const existing = g[CLIENTS];
1047
+ if (existing) return existing;
1048
+ const fresh = /* @__PURE__ */ new Map();
1049
+ g[CLIENTS] = fresh;
1050
+ return fresh;
1051
+ }
1023
1052
  function createAnalytics(config) {
1024
- return new Analytics(config);
1053
+ if (!isBrowser()) return new Analytics(config);
1054
+ const key = (config.host ?? DEFAULT_HOST) + "\0" + config.product;
1055
+ const clients = registry();
1056
+ const existing = clients.get(key);
1057
+ if (existing) {
1058
+ existing.adopt(config);
1059
+ return existing;
1060
+ }
1061
+ const fresh = new Analytics(config);
1062
+ clients.set(key, fresh);
1063
+ return fresh;
1025
1064
  }
1026
1065
 
1027
1066
  // src/react.tsx
1028
1067
  var Ctx = createContext(null);
1029
1068
  function AnalyticsProvider(props) {
1030
1069
  const { client, config, autoPageview = true, children } = props;
1031
- const instance = useMemo(() => {
1032
- if (client) return client;
1033
- if (config) return createAnalytics(config);
1034
- throw new Error("AnalyticsProvider requires `client` or `config`");
1035
- }, [client]);
1070
+ if (!client && !config) throw new Error("AnalyticsProvider requires `client` or `config`");
1071
+ const instance = client ?? createAnalytics(config);
1036
1072
  useEffect(() => {
1037
1073
  instance.init();
1038
1074
  if (autoPageview) instance.pageview();