@hanzo/event 0.3.26 → 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.cts 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.cjs';
3
+ import { e as Analytics, f as AnalyticsConfig } from './core-usULsfUB.cjs';
4
4
 
5
5
  declare const Ctx: react.Context<Analytics | null>;
6
6
  interface AnalyticsProviderProps {
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.26";
310
+ var VERSION = "0.3.27";
311
311
 
312
312
  // src/sentry.ts
313
313
  var MAX_FRAMES = 50;
@@ -784,6 +784,20 @@ var Analytics = class {
784
784
  config.dsn ?? readEnvDsn() ?? dsnForProduct(this.cfg.product, this.cfg.ingestKey)
785
785
  );
786
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
+ }
787
801
  /** errorPlaneEnabled reports whether captured exceptions can actually reach the
788
802
  * error host. False means a DSN was never configured — the documented
789
803
  * fail-safe. Exposed so an app (or a test) can assert its wiring instead of
@@ -834,9 +848,14 @@ var Analytics = class {
834
848
  group(groupId, traits) {
835
849
  this.enqueue("group", void 0, { groupId, properties: traits });
836
850
  }
837
- /** 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. */
838
854
  pageview(path, properties) {
839
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;
840
859
  this.enqueue("pageview", PAGEVIEW, { path: p, properties });
841
860
  }
842
861
  /** capture records a named product event with optional properties. Commerce
@@ -897,8 +916,8 @@ var Analytics = class {
897
916
  * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
898
917
  * unload-safe transport. Auth is orthogonal to the wire:
899
918
  *
900
- * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
901
- * ?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.
902
921
  * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
903
922
  * carry a header, so token apps fall back to keepalive fetch on unload).
904
923
  * • else a cookie app rides same-origin credentials (beacon carries the
@@ -1021,19 +1040,35 @@ var Analytics = class {
1021
1040
  }
1022
1041
  }
1023
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
+ }
1024
1052
  function createAnalytics(config) {
1025
- 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;
1026
1064
  }
1027
1065
 
1028
1066
  // src/react.tsx
1029
1067
  var Ctx = createContext(null);
1030
1068
  function AnalyticsProvider(props) {
1031
1069
  const { client, config, autoPageview = true, children } = props;
1032
- const instance = useMemo(() => {
1033
- if (client) return client;
1034
- if (config) return createAnalytics(config);
1035
- throw new Error("AnalyticsProvider requires `client` or `config`");
1036
- }, [client]);
1070
+ if (!client && !config) throw new Error("AnalyticsProvider requires `client` or `config`");
1071
+ const instance = client ?? createAnalytics(config);
1037
1072
  useEffect(() => {
1038
1073
  instance.init();
1039
1074
  if (autoPageview) instance.pageview();