@hanzo/event 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -1,18 +1,30 @@
1
1
  # @hanzo/event
2
2
 
3
- Tiny, batched product-analytics capture client for Hanzo surfaces. Emits
4
- `pageview` / `event` / `identify` / `group` to **Hanzo Cloud** — never to a
5
- third-party or to `insights-capture` directly. Cloud (`/v1/analytics` +
6
- `/v1/tracker`) is the one front door and fans out to the insights/datastore
7
- warehouse.
3
+ The ONE telemetry client for Hanzo surfaces. Emits every kind of event —
4
+ `pageview` / `event` / `identify` / `group` **and errors** — to **Hanzo Cloud**,
5
+ never to a third-party. There is exactly one front door:
6
+
7
+ ```
8
+ POST {host}/v1/event body: { batch: [Event, …] } -> { accepted, dropped }
9
+ ```
10
+
11
+ Cloud resolves the tenant server-side and fans the one stream out into three
12
+ lenses: **product analytics** (insights.hanzo.ai), **web analytics**
13
+ (analytics.hanzo.ai), and **error tracking** (sentry.hanzo.ai). Errors are just
14
+ events (`type:'error'`) on the same pipe — one client, one door.
8
15
 
9
16
  - **Batched** with a size + interval flush, and **beacon-on-unload**
10
- (`sendBeacon` for cookie apps, `fetch(keepalive)` for token apps).
17
+ (`sendBeacon` for cookie/publishable-key apps, `fetch(keepalive)` for token apps).
18
+ - **Auto error capture** (subsumes `@sentry`): `window.onerror`,
19
+ `unhandledrejection`, and a React `ErrorBoundary` become `type:'error'` events;
20
+ Cloud stamps `event_type='error'` → the sentry lens. Opt out with
21
+ `captureErrors: false`.
11
22
  - **First-touch attribution**: UTM + referrer + `refCode` are parsed once and
12
23
  persisted, then attached to every event.
13
24
  - **Cohorts**: `signupWeek`, `channel`, `refCode` ride each event.
14
25
  - **Tenant-safe**: the client NEVER sends an org/tenant — Cloud stamps it from
15
- the validated session.
26
+ the validated session or the signed publishable key.
27
+ - **Fail-soft**: telemetry loss is swallowed; the client never throws into the app.
16
28
  - **SSR-safe**: importing on the server is a no-op; it only acts in the browser.
17
29
 
18
30
  ## Core (framework-agnostic)
@@ -58,6 +70,41 @@ function UpgradeButton() {
58
70
  }
59
71
  ```
60
72
 
73
+ ## Errors (the @sentry replacement)
74
+
75
+ Unhandled errors and promise rejections are captured automatically. React render
76
+ errors never reach `window.onerror`, so wrap your tree in the `ErrorBoundary` to
77
+ catch those too. Report caught errors yourself with `captureError`.
78
+
79
+ ```tsx
80
+ import { ErrorBoundary } from '@hanzo/event/react'
81
+
82
+ <AnalyticsProvider config={{ product: 'console' }}>
83
+ <ErrorBoundary fallback={(err, reset) => <Crash error={err} onReset={reset} />}>
84
+ <App />
85
+ </ErrorBoundary>
86
+ </AnalyticsProvider>
87
+ ```
88
+
89
+ ```ts
90
+ try { risky() } catch (err) { analytics.captureError(err, { properties: { where: 'checkout' } }) }
91
+ ```
92
+
93
+ Each becomes a `type:'error'` event carrying the exception; Cloud folds it into
94
+ `properties.$exception`, stamps `event_type='error'`, and it surfaces in the
95
+ error-tracking lens (`GET /v1/errors` → sentry.hanzo.ai).
96
+
97
+ ## Publishable key (public pages, no bearer)
98
+
99
+ Marketing/public pages have no session. Mint a write-only publishable key
100
+ (`POST /v1/ingest/keys`) and pass it as `ingestKey`; it rides `Authorization`
101
+ on fetch and `?ingest_key` on an unload beacon, so all three lenses light up
102
+ anonymously. It is safe to ship in a bundle (write-only, cannot read).
103
+
104
+ ```ts
105
+ createAnalytics({ product: 'site', host: 'https://api.hanzo.ai', ingestKey: 'pk_live_…' })
106
+ ```
107
+
61
108
  ## Goals & cohorts
62
109
 
63
110
  `GOALS` and `COHORTS` (see `goals.ts`) are the shared, machine-readable insights
@@ -1,8 +1,10 @@
1
1
  /** The event kinds — the closed set the server understands. An error is just
2
- * another event on the one stream (lensed to the error-tracking view). */
2
+ * another event on the one stream (Cloud stamps type:'error' event_type='error',
3
+ * the key the error-tracking lens filters on). */
3
4
  type EventKind = 'pageview' | 'event' | 'identify' | 'group' | 'error';
4
- /** A captured exception. Carried on a `type:'error'` event; the server lenses it
5
- * into the error-tracking view (sentry.hanzo.ai). */
5
+ /** A captured exception. Carried on a `type:'error'` event's top-level `error`
6
+ * field; Cloud folds it into properties.$exception and lenses the event into the
7
+ * error-tracking view (sentry.hanzo.ai). */
6
8
  interface Exception {
7
9
  /** Constructor/class name, e.g. "TypeError". */
8
10
  type?: string;
@@ -35,8 +37,10 @@ interface Cohort {
35
37
  channel?: string;
36
38
  refCode?: string;
37
39
  }
38
- /** One event as sent on the wire. tenant/org is NEVER set here the server
39
- * stamps it from the validated session. */
40
+ /** One event as sent on the wire the canonical Hanzo Cloud event. Maps 1:1 to
41
+ * the cloud `CaptureEvent` (camelCase JSON keys); a batch of these is POSTed to
42
+ * the ONE front door `/v1/event` as `{ batch: [WireEvent, …] }`. tenant/org is
43
+ * NEVER a field here — the server stamps it from the validated session/key. */
40
44
  interface WireEvent {
41
45
  messageId: string;
42
46
  type: EventKind;
@@ -59,29 +63,43 @@ interface WireEvent {
59
63
  quantity?: number;
60
64
  revenue?: number;
61
65
  currency?: string;
62
- /** Set on `type:'error'` events — the captured exception. */
66
+ /** Set on `type:'error'` events — the captured exception. Cloud lifts it into
67
+ * properties.$exception (foldException) for the error-tracking lens. */
63
68
  error?: Exception;
64
69
  properties?: Record<string, unknown>;
65
70
  library?: string;
66
71
  libraryVersion?: string;
67
72
  }
68
- /** Injectable transports — overridden in tests; default in core.ts uses fetch. */
73
+ /** Injectable transports — overridden in tests; the default in core.ts uses fetch
74
+ * (keepalive) and sendBeacon. A bearer JWT or a publishable pk_ key rides
75
+ * Authorization on fetch; on a headerless beacon a publishable key rides the
76
+ * ?ingest_key query. */
69
77
  interface Transport {
70
78
  /** Durable POST usable during page unload (fetch keepalive / sendBeacon). */
71
79
  send(url: string, body: string, opts: {
72
80
  beacon: boolean;
73
81
  token?: string;
82
+ ingestKey?: string;
74
83
  }): void;
75
84
  }
76
85
  interface AnalyticsConfig {
77
- /** Cloud base URL. Same-origin ("") for cookie-auth apps (console/admin);
78
- * e.g. "https://api.hanzo.ai" for bearer apps (app/site). */
86
+ /** Cloud base URL. Defaults to "https://api.hanzo.ai" (the one edge). Set to
87
+ * same-origin ("") for cookie-auth apps served behind the same edge
88
+ * (console/admin/chat), so the browser rides the session cookie. */
79
89
  host?: string;
80
90
  /** Emitting surface: console | chat | app | site | admin. */
81
91
  product: string;
82
92
  /** Bearer token provider for token-auth apps. Omit for cookie/session apps
83
93
  * (the client then relies on same-origin credentials). */
84
94
  getToken?: () => string | undefined | null;
95
+ /** Publishable ingest key (pk_…). When set, the client authenticates to the ONE
96
+ * front door `/v1/event` with this key instead of a bearer/cookie: it rides
97
+ * Authorization: Bearer pk_… on fetch and ?ingest_key=pk_… on a headerless
98
+ * page-unload beacon, so ALL THREE lenses (web + product + error) light up with
99
+ * no bearer and unload beacons work anonymously. The key is write-only (cannot
100
+ * read) and safe to ship in a bundle; mint one per org via POST /v1/ingest/keys.
101
+ * Recommended for marketing/public pages and the full sentry-subsuming setup. */
102
+ ingestKey?: string;
85
103
  /** Max events buffered before an automatic flush. */
86
104
  batchSize?: number;
87
105
  /** Auto-flush cadence in ms. */
@@ -98,7 +116,7 @@ interface AnalyticsConfig {
98
116
  debug?: boolean;
99
117
  }
100
118
 
101
- declare const VERSION = "0.2.0";
119
+ declare const VERSION = "0.3.0";
102
120
  declare class Analytics {
103
121
  private cfg;
104
122
  private transport;
@@ -110,8 +128,9 @@ declare class Analytics {
110
128
  private started;
111
129
  constructor(config: AnalyticsConfig);
112
130
  /** init is idempotent and browser-only for its side effects: capture first-touch
113
- * attribution, hydrate cohort, and register the unload flush. Safe to call from
114
- * a React effect on every render. */
131
+ * attribution, hydrate cohort, register the unload flush, and (unless opted out)
132
+ * auto-capture unhandled errors. Safe to call from a React effect on every
133
+ * render. */
115
134
  init(): void;
116
135
  /** identify binds the current visitor to a stable person id (post-login). */
117
136
  identify(personId: string, traits?: Record<string, unknown>): void;
@@ -126,11 +145,12 @@ declare class Analytics {
126
145
  /** track is an alias of capture (Segment familiarity). */
127
146
  track: (event: string, properties?: Record<string, unknown>, commerce?: Pick<WireEvent, "productId" | "quantity" | "revenue" | "currency">) => void;
128
147
  /** captureError records an exception as a first-class error event — the ONE
129
- * error path (subsumes @sentry). A caught error, an unhandled rejection, or a
130
- * manual report all become a type:'error' event on the same stream, lensed to
131
- * the error-tracking view server-side. Never throws back into the app; errors
132
- * are higher-signal than pageviews, so it flushes promptly (a crash may unload
133
- * the page moments later). */
148
+ * error path (subsumes @sentry). A caught error, an unhandled rejection, a
149
+ * React render error, or a manual report all become a type:'error' event on the
150
+ * same stream; Cloud folds the exception into properties.$exception and stamps
151
+ * event_type='error', so it surfaces in the error-tracking lens. Never throws
152
+ * back into the app; errors are higher-signal than pageviews, so it flushes
153
+ * promptly (a crash may unload the page moments later). */
134
154
  captureError(err: unknown, context?: {
135
155
  handled?: boolean;
136
156
  properties?: Record<string, unknown>;
@@ -143,8 +163,17 @@ declare class Analytics {
143
163
  /** setCohort persists cohort dimensions (e.g. signupWeek at signup) so they ride
144
164
  * every subsequent event. */
145
165
  setCohort(patch: Cohort): void;
146
- /** flush drains the buffer to the server as one batch. beacon=true uses the
147
- * unload-safe path. */
166
+ /** flush drains the buffer to the server as ONE batch through the ONE ingest
167
+ * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
168
+ * unload-safe transport. Auth is orthogonal to the wire:
169
+ *
170
+ * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
171
+ * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.
172
+ * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
173
+ * carry a header, so token apps fall back to keepalive fetch on unload).
174
+ * • else a cookie app rides same-origin credentials (beacon carries the
175
+ * cookie fine).
176
+ */
148
177
  flush(beacon?: boolean): void;
149
178
  private enqueue;
150
179
  private build;
@@ -1,8 +1,10 @@
1
1
  /** The event kinds — the closed set the server understands. An error is just
2
- * another event on the one stream (lensed to the error-tracking view). */
2
+ * another event on the one stream (Cloud stamps type:'error' event_type='error',
3
+ * the key the error-tracking lens filters on). */
3
4
  type EventKind = 'pageview' | 'event' | 'identify' | 'group' | 'error';
4
- /** A captured exception. Carried on a `type:'error'` event; the server lenses it
5
- * into the error-tracking view (sentry.hanzo.ai). */
5
+ /** A captured exception. Carried on a `type:'error'` event's top-level `error`
6
+ * field; Cloud folds it into properties.$exception and lenses the event into the
7
+ * error-tracking view (sentry.hanzo.ai). */
6
8
  interface Exception {
7
9
  /** Constructor/class name, e.g. "TypeError". */
8
10
  type?: string;
@@ -35,8 +37,10 @@ interface Cohort {
35
37
  channel?: string;
36
38
  refCode?: string;
37
39
  }
38
- /** One event as sent on the wire. tenant/org is NEVER set here the server
39
- * stamps it from the validated session. */
40
+ /** One event as sent on the wire the canonical Hanzo Cloud event. Maps 1:1 to
41
+ * the cloud `CaptureEvent` (camelCase JSON keys); a batch of these is POSTed to
42
+ * the ONE front door `/v1/event` as `{ batch: [WireEvent, …] }`. tenant/org is
43
+ * NEVER a field here — the server stamps it from the validated session/key. */
40
44
  interface WireEvent {
41
45
  messageId: string;
42
46
  type: EventKind;
@@ -59,29 +63,43 @@ interface WireEvent {
59
63
  quantity?: number;
60
64
  revenue?: number;
61
65
  currency?: string;
62
- /** Set on `type:'error'` events — the captured exception. */
66
+ /** Set on `type:'error'` events — the captured exception. Cloud lifts it into
67
+ * properties.$exception (foldException) for the error-tracking lens. */
63
68
  error?: Exception;
64
69
  properties?: Record<string, unknown>;
65
70
  library?: string;
66
71
  libraryVersion?: string;
67
72
  }
68
- /** Injectable transports — overridden in tests; default in core.ts uses fetch. */
73
+ /** Injectable transports — overridden in tests; the default in core.ts uses fetch
74
+ * (keepalive) and sendBeacon. A bearer JWT or a publishable pk_ key rides
75
+ * Authorization on fetch; on a headerless beacon a publishable key rides the
76
+ * ?ingest_key query. */
69
77
  interface Transport {
70
78
  /** Durable POST usable during page unload (fetch keepalive / sendBeacon). */
71
79
  send(url: string, body: string, opts: {
72
80
  beacon: boolean;
73
81
  token?: string;
82
+ ingestKey?: string;
74
83
  }): void;
75
84
  }
76
85
  interface AnalyticsConfig {
77
- /** Cloud base URL. Same-origin ("") for cookie-auth apps (console/admin);
78
- * e.g. "https://api.hanzo.ai" for bearer apps (app/site). */
86
+ /** Cloud base URL. Defaults to "https://api.hanzo.ai" (the one edge). Set to
87
+ * same-origin ("") for cookie-auth apps served behind the same edge
88
+ * (console/admin/chat), so the browser rides the session cookie. */
79
89
  host?: string;
80
90
  /** Emitting surface: console | chat | app | site | admin. */
81
91
  product: string;
82
92
  /** Bearer token provider for token-auth apps. Omit for cookie/session apps
83
93
  * (the client then relies on same-origin credentials). */
84
94
  getToken?: () => string | undefined | null;
95
+ /** Publishable ingest key (pk_…). When set, the client authenticates to the ONE
96
+ * front door `/v1/event` with this key instead of a bearer/cookie: it rides
97
+ * Authorization: Bearer pk_… on fetch and ?ingest_key=pk_… on a headerless
98
+ * page-unload beacon, so ALL THREE lenses (web + product + error) light up with
99
+ * no bearer and unload beacons work anonymously. The key is write-only (cannot
100
+ * read) and safe to ship in a bundle; mint one per org via POST /v1/ingest/keys.
101
+ * Recommended for marketing/public pages and the full sentry-subsuming setup. */
102
+ ingestKey?: string;
85
103
  /** Max events buffered before an automatic flush. */
86
104
  batchSize?: number;
87
105
  /** Auto-flush cadence in ms. */
@@ -98,7 +116,7 @@ interface AnalyticsConfig {
98
116
  debug?: boolean;
99
117
  }
100
118
 
101
- declare const VERSION = "0.2.0";
119
+ declare const VERSION = "0.3.0";
102
120
  declare class Analytics {
103
121
  private cfg;
104
122
  private transport;
@@ -110,8 +128,9 @@ declare class Analytics {
110
128
  private started;
111
129
  constructor(config: AnalyticsConfig);
112
130
  /** init is idempotent and browser-only for its side effects: capture first-touch
113
- * attribution, hydrate cohort, and register the unload flush. Safe to call from
114
- * a React effect on every render. */
131
+ * attribution, hydrate cohort, register the unload flush, and (unless opted out)
132
+ * auto-capture unhandled errors. Safe to call from a React effect on every
133
+ * render. */
115
134
  init(): void;
116
135
  /** identify binds the current visitor to a stable person id (post-login). */
117
136
  identify(personId: string, traits?: Record<string, unknown>): void;
@@ -126,11 +145,12 @@ declare class Analytics {
126
145
  /** track is an alias of capture (Segment familiarity). */
127
146
  track: (event: string, properties?: Record<string, unknown>, commerce?: Pick<WireEvent, "productId" | "quantity" | "revenue" | "currency">) => void;
128
147
  /** captureError records an exception as a first-class error event — the ONE
129
- * error path (subsumes @sentry). A caught error, an unhandled rejection, or a
130
- * manual report all become a type:'error' event on the same stream, lensed to
131
- * the error-tracking view server-side. Never throws back into the app; errors
132
- * are higher-signal than pageviews, so it flushes promptly (a crash may unload
133
- * the page moments later). */
148
+ * error path (subsumes @sentry). A caught error, an unhandled rejection, a
149
+ * React render error, or a manual report all become a type:'error' event on the
150
+ * same stream; Cloud folds the exception into properties.$exception and stamps
151
+ * event_type='error', so it surfaces in the error-tracking lens. Never throws
152
+ * back into the app; errors are higher-signal than pageviews, so it flushes
153
+ * promptly (a crash may unload the page moments later). */
134
154
  captureError(err: unknown, context?: {
135
155
  handled?: boolean;
136
156
  properties?: Record<string, unknown>;
@@ -143,8 +163,17 @@ declare class Analytics {
143
163
  /** setCohort persists cohort dimensions (e.g. signupWeek at signup) so they ride
144
164
  * every subsequent event. */
145
165
  setCohort(patch: Cohort): void;
146
- /** flush drains the buffer to the server as one batch. beacon=true uses the
147
- * unload-safe path. */
166
+ /** flush drains the buffer to the server as ONE batch through the ONE ingest
167
+ * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
168
+ * unload-safe transport. Auth is orthogonal to the wire:
169
+ *
170
+ * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
171
+ * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.
172
+ * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
173
+ * carry a header, so token apps fall back to keepalive fetch on unload).
174
+ * • else a cookie app rides same-origin credentials (beacon carries the
175
+ * cookie fine).
176
+ */
148
177
  flush(beacon?: boolean): void;
149
178
  private enqueue;
150
179
  private build;
@@ -193,9 +193,12 @@ function mergeCohort(patch) {
193
193
  }
194
194
 
195
195
  // src/core.ts
196
- var VERSION = "0.2.0";
197
- var ANALYTICS_PATH = "/v1/analytics";
198
- var TRACKER_PATH = "/v1/tracker";
196
+ var VERSION = "0.3.0";
197
+ var EVENT_PATH = "/v1/event";
198
+ var DEFAULT_HOST = "https://api.hanzo.ai";
199
+ function appendQuery(url, key, value) {
200
+ return url + (url.includes("?") ? "&" : "?") + key + "=" + encodeURIComponent(value);
201
+ }
199
202
  function uid2() {
200
203
  const c = typeof crypto !== "undefined" ? crypto : void 0;
201
204
  if (c && "randomUUID" in c) return c.randomUUID();
@@ -216,15 +219,17 @@ var isBrowser = () => typeof window !== "undefined";
216
219
  var DefaultTransport = class {
217
220
  send(url, body, opts) {
218
221
  if (opts.beacon && isBrowser() && typeof navigator.sendBeacon === "function") {
222
+ const beaconUrl = opts.ingestKey ? appendQuery(url, "ingest_key", opts.ingestKey) : url;
219
223
  try {
220
- navigator.sendBeacon(url, new Blob([body], { type: "application/json" }));
224
+ navigator.sendBeacon(beaconUrl, new Blob([body], { type: "application/json" }));
221
225
  return;
222
226
  } catch {
223
227
  }
224
228
  }
225
229
  if (typeof fetch !== "function") return;
226
230
  const headers = { "Content-Type": "application/json" };
227
- if (opts.token) headers.Authorization = `Bearer ${opts.token}`;
231
+ const bearer = opts.ingestKey ?? opts.token;
232
+ if (bearer) headers.Authorization = `Bearer ${bearer}`;
228
233
  void fetch(url, {
229
234
  method: "POST",
230
235
  headers,
@@ -247,7 +252,7 @@ var Analytics = class {
247
252
  /** captureException — @sentry-familiar alias of captureError. */
248
253
  this.captureException = this.captureError.bind(this);
249
254
  this.cfg = {
250
- host: "",
255
+ host: DEFAULT_HOST,
251
256
  batchSize: 20,
252
257
  flushIntervalMs: 5e3,
253
258
  enabled: true,
@@ -257,8 +262,9 @@ var Analytics = class {
257
262
  this.transport = config.transport ?? new DefaultTransport();
258
263
  }
259
264
  /** init is idempotent and browser-only for its side effects: capture first-touch
260
- * attribution, hydrate cohort, and register the unload flush. Safe to call from
261
- * a React effect on every render. */
265
+ * attribution, hydrate cohort, register the unload flush, and (unless opted out)
266
+ * auto-capture unhandled errors. Safe to call from a React effect on every
267
+ * render. */
262
268
  init() {
263
269
  if (this.started || !this.cfg.enabled) return;
264
270
  this.started = true;
@@ -305,11 +311,12 @@ var Analytics = class {
305
311
  this.enqueue("event", event, { properties, ...commerce });
306
312
  }
307
313
  /** captureError records an exception as a first-class error event — the ONE
308
- * error path (subsumes @sentry). A caught error, an unhandled rejection, or a
309
- * manual report all become a type:'error' event on the same stream, lensed to
310
- * the error-tracking view server-side. Never throws back into the app; errors
311
- * are higher-signal than pageviews, so it flushes promptly (a crash may unload
312
- * the page moments later). */
314
+ * error path (subsumes @sentry). A caught error, an unhandled rejection, a
315
+ * React render error, or a manual report all become a type:'error' event on the
316
+ * same stream; Cloud folds the exception into properties.$exception and stamps
317
+ * event_type='error', so it surfaces in the error-tracking lens. Never throws
318
+ * back into the app; errors are higher-signal than pageviews, so it flushes
319
+ * promptly (a crash may unload the page moments later). */
313
320
  captureError(err, context) {
314
321
  const ex = normalizeError(err);
315
322
  ex.handled = context?.handled ?? true;
@@ -321,19 +328,28 @@ var Analytics = class {
321
328
  setCohort(patch) {
322
329
  this.cohort = mergeCohort(patch);
323
330
  }
324
- /** flush drains the buffer to the server as one batch. beacon=true uses the
325
- * unload-safe path. */
331
+ /** flush drains the buffer to the server as ONE batch through the ONE ingest
332
+ * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the
333
+ * unload-safe transport. Auth is orthogonal to the wire:
334
+ *
335
+ * • publishable key set → rides Authorization: Bearer pk_… (fetch) or
336
+ * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.
337
+ * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot
338
+ * carry a header, so token apps fall back to keepalive fetch on unload).
339
+ * • else a cookie app rides same-origin credentials (beacon carries the
340
+ * cookie fine).
341
+ */
326
342
  flush(beacon = false) {
327
343
  if (!this.cfg.enabled || this.queue.length === 0) return;
328
344
  const batch = this.queue;
329
345
  this.queue = [];
330
346
  this.clearTimer();
331
- const token = this.cfg.getToken?.() ?? void 0;
347
+ const key = this.cfg.ingestKey?.trim() || void 0;
348
+ const token = key ? void 0 : this.cfg.getToken?.() ?? void 0;
332
349
  const useBeacon = beacon && !token;
333
- const path = useBeacon ? TRACKER_PATH : ANALYTICS_PATH;
334
350
  const body = JSON.stringify({ batch });
335
- if (this.cfg.debug) console.debug("[analytics] flush", batch.length, path);
336
- this.transport.send(this.cfg.host + path, body, { beacon: useBeacon, token: token ?? void 0 });
351
+ if (this.cfg.debug) console.debug("[event] flush \u2192", EVENT_PATH, batch.length);
352
+ this.transport.send(this.cfg.host + EVENT_PATH, body, { beacon: useBeacon, token, ingestKey: key });
337
353
  }
338
354
  // ── internals ────────────────────────────────────────────────────────────
339
355
  enqueue(kind, event, extra) {
@@ -429,5 +445,5 @@ exports.hasAttribution = hasAttribution;
429
445
  exports.hostOf = hostOf;
430
446
  exports.isoWeek = isoWeek;
431
447
  exports.parseAttribution = parseAttribution;
432
- //# sourceMappingURL=index.js.map
433
- //# sourceMappingURL=index.js.map
448
+ //# sourceMappingURL=index.cjs.map
449
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/attribution.ts","../src/events.ts","../src/storage.ts","../src/core.ts","../src/goals.ts"],"names":["uid"],"mappings":";;;AAMA,IAAM,YAAA,GAAe;AAAA,EACnB,WAAA;AAAA,EAAa,YAAA;AAAA,EAAc,UAAA;AAAA,EAAY,OAAA;AAAA,EAAS,MAAA;AAAA,EAAQ,WAAA;AAAA,EACxD,SAAA;AAAA,EAAW,UAAA;AAAA,EAAY,SAAA;AAAA,EAAW,YAAA;AAAA,EAAc;AAClD,CAAA;AACA,IAAM,eAAe,CAAC,SAAA,EAAW,SAAS,aAAA,EAAe,QAAA,EAAU,UAAU,SAAS,CAAA;AAI/E,SAAS,gBAAA,CAAiB,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,IAAA;AAAA,IACf;AAAA,EACF;AACF;AAGO,SAAS,gBAAgB,MAAA,EAAoC;AAClE,EAAA,OAAO,IAAI,UAAU,MAAM,CAAA;AAC7B;;;ACvSO,IAAM,KAAA,GAA8D;AAAA;AAAA,EAEzE,MAAA,EAAQ;AAAA,IACN,KAAA,EAAO,QAAA;AAAA,IACP,OAAO,MAAA,CAAO,gBAAA;AAAA,IACd,MAAA,EAAQ;AAAA,MACN,MAAA,CAAO,aAAA;AAAA,MACP,MAAA,CAAO,gBAAA;AAAA,MACP,MAAA,CAAO,eAAA;AAAA,MACP,MAAA,CAAO;AAAA;AACT,GACF;AAAA;AAAA,EAEA,IAAA,EAAM;AAAA,IACJ,KAAA,EAAO,MAAA;AAAA,IACP,OAAO,MAAA,CAAO,eAAA;AAAA,IACd,MAAA,EAAQ,EAAE,QAAA,EAAU,MAAA,EAAQ,QAAQ,MAAA;AAAO,GAC7C;AAAA;AAAA,EAEA,aAAA,EAAe;AAAA,IACb,KAAA,EAAO,gBAAA;AAAA,IACP,OAAO,MAAA,CAAO,YAAA;AAAA,IACd,QAAQ,CAAC,MAAA,CAAO,gBAAgB,MAAA,CAAO,YAAA,EAAc,OAAO,gBAAgB;AAAA;AAEhF;AAQO,IAAM,OAAA,GAAmE;AAAA,EAC9E,UAAA,EAAY,EAAE,KAAA,EAAO,aAAA,EAAe,OAAO,aAAA,EAAc;AAAA,EACzD,OAAA,EAAS,EAAE,KAAA,EAAO,SAAA,EAAW,OAAO,qBAAA,EAAsB;AAAA,EAC1D,OAAA,EAAS,EAAE,KAAA,EAAO,UAAA,EAAY,OAAO,eAAA;AACvC","file":"index.cjs","sourcesContent":["// Pure attribution helpers: parse first-touch UTM/referrer/refCode from a URL,\n// derive the acquisition channel, and compute the ISO week for the signup cohort.\n// No I/O, no globals — trivially testable.\n\nimport type { Attribution } from './types'\n\nconst SOCIAL_HOSTS = [\n 'facebook.', 'instagram.', 'twitter.', 'x.com', 't.co', 'linkedin.',\n 'reddit.', 'youtube.', 'tiktok.', 'pinterest.', 'news.ycombinator.com',\n]\nconst SEARCH_HOSTS = ['google.', 'bing.', 'duckduckgo.', 'yahoo.', 'baidu.', 'ecosia.']\n\n/** parseAttribution reads UTM params + ref/refCode from a query string and pairs\n * them with the referrer. `search` is a location.search value (\"?utm_source=…\"). */\nexport function parseAttribution(search: string, referrer: string): Attribution {\n const q = new URLSearchParams(search || '')\n const get = (k: string) => {\n const v = q.get(k)\n return v ? v.trim() : undefined\n }\n const a: Attribution = {\n utm: {\n source: get('utm_source'),\n medium: get('utm_medium'),\n campaign: get('utm_campaign'),\n term: get('utm_term'),\n content: get('utm_content'),\n },\n referrer: referrer ? referrer.trim() : undefined,\n refCode: get('ref') || get('refCode') || get('ref_code') || undefined,\n }\n a.channel = deriveChannel(a)\n return a\n}\n\n/** deriveChannel classifies the visit: paid | referral | social | organic | direct. */\nexport function deriveChannel(a: Attribution): string {\n const medium = (a.utm.medium || '').toLowerCase()\n if (/(cpc|ppc|paid|paidsearch|display|cpm)/.test(medium)) return 'paid'\n if (a.utm.source || a.utm.campaign) return 'campaign'\n if (a.refCode) return 'referral'\n const host = hostOf(a.referrer)\n if (!host) return 'direct'\n if (SOCIAL_HOSTS.some((h) => host.includes(h))) return 'social'\n if (SEARCH_HOSTS.some((h) => host.includes(h))) return 'organic'\n return 'referral'\n}\n\n/** hostOf extracts a bare lowercase host from a URL; \"\" when unparseable. */\nexport function hostOf(raw?: string): string {\n if (!raw) return ''\n let s = raw.trim()\n const scheme = s.indexOf('://')\n if (scheme >= 0) s = s.slice(scheme + 3)\n const cut = s.search(/[/?#]/)\n if (cut >= 0) s = s.slice(0, cut)\n const at = s.indexOf('@')\n if (at >= 0) s = s.slice(at + 1)\n const colon = s.indexOf(':')\n if (colon >= 0) s = s.slice(0, colon)\n return s.toLowerCase().trim()\n}\n\n/** hasAttribution reports whether anything was captured (so we don't persist an\n * empty first-touch that would shadow a later real one). */\nexport function hasAttribution(a: Attribution): boolean {\n return Boolean(\n a.utm.source || a.utm.medium || a.utm.campaign || a.utm.term ||\n a.utm.content || a.refCode || (a.referrer && hostOf(a.referrer)),\n )\n}\n\n/** isoWeek returns the ISO-8601 week label, e.g. \"2026-W28\". */\nexport function isoWeek(d: Date): string {\n // Copy to UTC midnight; ISO week: Thursday-anchored.\n const date = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()))\n const day = date.getUTCDay() || 7\n date.setUTCDate(date.getUTCDate() + 4 - day)\n const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1))\n const week = Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7)\n return `${date.getUTCFullYear()}-W${String(week).padStart(2, '0')}`\n}\n","// The ONE product-analytics vocabulary. Every Hanzo surface emits these exact\n// names so funnels, goals, and cohorts line up across console/chat/app/site/admin.\n// Pageviews use the reserved \"$pageview\" name (emitted by analytics.pageview()),\n// matching the server read lens.\n\nexport const EVENTS = {\n // Signup funnel: view -> submit -> verify -> completed -> first action.\n SIGNUP_VIEWED: 'signup_viewed',\n SIGNUP_SUBMITTED: 'signup_submitted',\n SIGNUP_VERIFIED: 'signup_verified',\n SIGNUP_COMPLETED: 'signup_completed',\n FIRST_ACTION: 'first_action',\n\n // Waitlist + referral.\n WAITLIST_JOINED: 'waitlist_joined',\n WAITLIST_SHARED: 'waitlist_shared',\n REFERRAL_USED: 'referral_used',\n REFERRAL_CLAIMED: 'referral_claimed',\n\n // Upgrade-intent + purchase.\n PRICING_VIEWED: 'pricing_viewed',\n PLAN_CLICKED: 'plan_clicked',\n CHECKOUT_STARTED: 'checkout_started',\n ORDER_COMPLETED: 'order_completed',\n\n // Feature usage — generic + the common key surfaces across products.\n FEATURE_USED: 'feature_used',\n API_KEY_CREATED: 'api_key_created',\n APP_CREATED: 'app_created',\n DEPLOY_STARTED: 'deploy_started',\n PROJECT_CREATED: 'project_created',\n AGENT_CREATED: 'agent_created',\n CHAT_STARTED: 'chat_started',\n CHAT_MESSAGE_SENT: 'chat_message_sent',\n TASK_STARTED: 'task_started',\n TASK_COMPLETED: 'task_completed',\n} as const\n\nexport type EventName = (typeof EVENTS)[keyof typeof EVENTS]\n\n/** The reserved event name a pageview is stored under (server + read lens). */\nexport const PAGEVIEW = '$pageview'\n","// SSR-safe browser storage for stable identifiers and first-touch state. Every\n// accessor no-ops (returns undefined) when there is no window/localStorage, so the\n// client imports cleanly in a Next.js server component.\n\nimport type { Attribution, Cohort } from './types'\n\nconst KEY = {\n anon: 'hz_anon_id',\n session: 'hz_session',\n firstTouch: 'hz_first_touch',\n cohort: 'hz_cohort',\n} as const\n\n/** 30-minute inactivity window defines a session (PostHog/GA convention). */\nconst SESSION_TTL_MS = 30 * 60 * 1000\n\nfunction ls(): Storage | undefined {\n try {\n if (typeof window === 'undefined' || !window.localStorage) return undefined\n return window.localStorage\n } catch {\n return undefined // Safari private mode / blocked storage\n }\n}\n\nfunction uid(): string {\n const c = typeof crypto !== 'undefined' ? crypto : undefined\n if (c && 'randomUUID' in c) return c.randomUUID()\n return 'a-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 10)\n}\n\n/** Stable anonymous id, minted once per browser and reused across sessions. */\nexport function anonId(): string | undefined {\n const s = ls()\n if (!s) return undefined\n let v = s.getItem(KEY.anon)\n if (!v) {\n v = uid()\n s.setItem(KEY.anon, v)\n }\n return v\n}\n\ninterface SessionState {\n id: string\n last: number\n}\n\n/** Current session id, rotated after SESSION_TTL_MS of inactivity. */\nexport function sessionId(now = Date.now()): string | undefined {\n const s = ls()\n if (!s) return undefined\n let state: SessionState | null = null\n try {\n state = JSON.parse(s.getItem(KEY.session) || 'null')\n } catch {\n state = null\n }\n if (!state || now - state.last > SESSION_TTL_MS) {\n state = { id: uid(), last: now }\n } else {\n state.last = now\n }\n s.setItem(KEY.session, JSON.stringify(state))\n return state.id\n}\n\n/** Read the persisted first-touch attribution. */\nexport function getFirstTouch(): Attribution | undefined {\n const s = ls()\n if (!s) return undefined\n try {\n const v = s.getItem(KEY.firstTouch)\n return v ? (JSON.parse(v) as Attribution) : undefined\n } catch {\n return undefined\n }\n}\n\n/** Persist first-touch attribution ONCE — never overwrite an existing record. */\nexport function setFirstTouchOnce(a: Attribution): Attribution {\n const s = ls()\n const existing = getFirstTouch()\n if (existing) return existing\n if (s) s.setItem(KEY.firstTouch, JSON.stringify(a))\n return a\n}\n\n/** Read persisted cohort dimensions. */\nexport function getCohort(): Cohort | undefined {\n const s = ls()\n if (!s) return undefined\n try {\n const v = s.getItem(KEY.cohort)\n return v ? (JSON.parse(v) as Cohort) : undefined\n } catch {\n return undefined\n }\n}\n\n/** Merge + persist cohort dimensions (signupWeek set once). */\nexport function mergeCohort(patch: Cohort): Cohort {\n const s = ls()\n const cur = getCohort() || {}\n const next: Cohort = {\n signupWeek: cur.signupWeek || patch.signupWeek,\n channel: patch.channel || cur.channel,\n refCode: cur.refCode || patch.refCode,\n }\n if (s) s.setItem(KEY.cohort, JSON.stringify(next))\n return next\n}\n","// The framework-agnostic event client. Buffers events and flushes them as ONE\n// batch through the ONE Hanzo Cloud ingestion front door:\n//\n// POST {host}/v1/event body: { batch: [Event, …] } -> { accepted, dropped }\n//\n// It NEVER sends the org/tenant: Cloud resolves that server-side (from the\n// validated session, or the signed publishable key) and stamps it. The client\n// only supplies its own visitor identity. Errors are just events (type:'error')\n// on the same stream — one client, one pipe, lensed server-side into product\n// analytics (insights), web analytics (analytics), and error tracking (sentry).\n//\n// Auth is orthogonal — the SAME body to the SAME door, differing only in how the\n// caller proves its tenant:\n//\n// • cookie/session app (host:'') — same-origin credentials ride the request.\n// • bearer app (getToken) — Authorization: Bearer <jwt>.\n// • publishable-key app (ingestKey: 'pk_…') — Authorization: Bearer pk_… on\n// fetch, ?ingest_key=pk_… on a headerless page-unload beacon. Write-only and\n// safe to ship in a bundle; the door HMAC-verifies it to an org server-side.\n//\n// The wire is the canonical `Event` (== the cloud CaptureEvent): its `type` field\n// is what Cloud folds to event_type='error', so a captured exception reaches the\n// error-tracking lens. (A four-field {event,distinctId,time,properties} object has\n// no `type`, so it can never be lensed as an error — this batched Event wire is\n// the one that lights up all three lenses.)\n\nimport {\n parseAttribution,\n hasAttribution,\n deriveChannel,\n} from './attribution'\nimport { PAGEVIEW } from './events'\nimport {\n anonId,\n sessionId,\n getFirstTouch,\n setFirstTouchOnce,\n getCohort,\n mergeCohort,\n} from './storage'\nimport type {\n AnalyticsConfig,\n Attribution,\n Cohort,\n EventKind,\n Exception,\n Transport,\n WireEvent,\n} from './types'\n\nexport const VERSION = '0.3.0'\n\nconst EVENT_PATH = '/v1/event' // the ONE canonical ingestion front door\nconst DEFAULT_HOST = 'https://api.hanzo.ai' // the one edge; cookie apps pass host:''\n\n/** appendQuery adds a single query param to a URL string — used to carry a\n * publishable key on a headerless sendBeacon (?ingest_key=…). */\nfunction appendQuery(url: string, key: string, value: string): string {\n return url + (url.includes('?') ? '&' : '?') + key + '=' + encodeURIComponent(value)\n}\n\nfunction uid(): string {\n const c = typeof crypto !== 'undefined' ? crypto : undefined\n if (c && 'randomUUID' in c) return c.randomUUID()\n return 'm-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 10)\n}\n\n/** Normalize anything thrown (Error | string | unknown) into an Exception. */\nfunction normalizeError(err: unknown): Exception {\n if (err instanceof Error) {\n return { type: err.name, message: err.message, stack: err.stack }\n }\n if (typeof err === 'string') return { message: err }\n try {\n return { message: JSON.stringify(err) }\n } catch {\n return { message: String(err) }\n }\n}\n\nconst isBrowser = () => typeof window !== 'undefined'\n\n/** DefaultTransport: fetch(keepalive) for authenticated/normal sends;\n * navigator.sendBeacon for headerless page-unload beacons. A bearer (a JWT or a\n * publishable pk_ key) rides Authorization on fetch; on a beacon — which cannot\n * set headers — a publishable key rides the ?ingest_key query instead. */\nclass DefaultTransport implements Transport {\n send(url: string, body: string, opts: { beacon: boolean; token?: string; ingestKey?: string }): void {\n if (opts.beacon && isBrowser() && typeof navigator.sendBeacon === 'function') {\n const beaconUrl = opts.ingestKey ? appendQuery(url, 'ingest_key', opts.ingestKey) : url\n try {\n navigator.sendBeacon(beaconUrl, new Blob([body], { type: 'application/json' }))\n return\n } catch {\n /* fall through to fetch */\n }\n }\n if (typeof fetch !== 'function') return\n const headers: Record<string, string> = { 'Content-Type': 'application/json' }\n const bearer = opts.ingestKey ?? opts.token\n if (bearer) headers.Authorization = `Bearer ${bearer}`\n void fetch(url, {\n method: 'POST',\n headers,\n body,\n keepalive: true,\n credentials: 'include',\n }).catch(() => {\n /* telemetry loss is acceptable; never throw into the app */\n })\n }\n}\n\nexport class Analytics {\n private cfg: Required<\n Pick<AnalyticsConfig, 'product' | 'batchSize' | 'flushIntervalMs' | 'enabled' | 'captureErrors'>\n > &\n AnalyticsConfig\n private transport: Transport\n private queue: WireEvent[] = []\n private timer: ReturnType<typeof setTimeout> | null = null\n private personId?: string\n private attribution: Attribution = { utm: {} }\n private cohort: Cohort = {}\n private started = false\n\n constructor(config: AnalyticsConfig) {\n this.cfg = {\n host: DEFAULT_HOST,\n batchSize: 20,\n flushIntervalMs: 5000,\n enabled: true,\n captureErrors: true,\n ...config,\n }\n this.transport = config.transport ?? new DefaultTransport()\n }\n\n /** init is idempotent and browser-only for its side effects: capture first-touch\n * attribution, hydrate cohort, register the unload flush, and (unless opted out)\n * auto-capture unhandled errors. Safe to call from a React effect on every\n * render. */\n init(): void {\n if (this.started || !this.cfg.enabled) return\n this.started = true\n if (!isBrowser()) return\n\n const parsed = parseAttribution(window.location.search, document.referrer)\n this.attribution = hasAttribution(parsed)\n ? setFirstTouchOnce(parsed)\n : getFirstTouch() ?? parsed\n this.cohort = mergeCohort({\n channel: this.attribution.channel ?? deriveChannel(this.attribution),\n refCode: this.attribution.refCode,\n })\n\n const flushHidden = () => {\n if (document.visibilityState === 'hidden') this.flush(true)\n }\n window.addEventListener('visibilitychange', flushHidden)\n window.addEventListener('pagehide', () => this.flush(true))\n\n // Auto error capture — the drop-in @sentry replacement. Unhandled errors and\n // rejected promises become type:'error' events on the same stream, which Cloud\n // stamps event_type='error' → the sentry.hanzo.ai lens.\n if (this.cfg.captureErrors) {\n window.addEventListener('error', (e: ErrorEvent) => {\n this.captureError(e.error ?? e.message, { handled: false })\n })\n window.addEventListener('unhandledrejection', (e: PromiseRejectionEvent) => {\n this.captureError(e.reason, { handled: false })\n })\n }\n }\n\n /** identify binds the current visitor to a stable person id (post-login). */\n identify(personId: string, traits?: Record<string, unknown>): void {\n this.personId = personId\n this.enqueue('identify', undefined, { properties: traits })\n }\n\n /** group associates the visitor with an org/team (analytics grouping, not the\n * server tenant — the server still derives tenant from the session). */\n group(groupId: string, traits?: Record<string, unknown>): void {\n this.enqueue('group', undefined, { groupId, properties: traits })\n }\n\n /** pageview records a $pageview for the current (or given) location. */\n pageview(path?: string, properties?: Record<string, unknown>): void {\n const url = isBrowser() ? window.location.href : undefined\n const p = path ?? (isBrowser() ? window.location.pathname : undefined)\n this.enqueue('pageview', PAGEVIEW, { url, path: p, properties })\n }\n\n /** capture records a named product event with optional properties. Commerce\n * fields (productId/quantity/revenue/currency) may be passed for order events. */\n capture(\n event: string,\n properties?: Record<string, unknown>,\n commerce?: Pick<WireEvent, 'productId' | 'quantity' | 'revenue' | 'currency'>,\n ): void {\n this.enqueue('event', event, { properties, ...commerce })\n }\n\n /** track is an alias of capture (Segment familiarity). */\n track = this.capture.bind(this)\n\n /** captureError records an exception as a first-class error event — the ONE\n * error path (subsumes @sentry). A caught error, an unhandled rejection, a\n * React render error, or a manual report all become a type:'error' event on the\n * same stream; Cloud folds the exception into properties.$exception and stamps\n * event_type='error', so it surfaces in the error-tracking lens. Never throws\n * back into the app; errors are higher-signal than pageviews, so it flushes\n * promptly (a crash may unload the page moments later). */\n captureError(\n err: unknown,\n context?: { handled?: boolean; properties?: Record<string, unknown> },\n ): void {\n const ex = normalizeError(err)\n ex.handled = context?.handled ?? true\n this.enqueue('error', ex.message, { error: ex, properties: context?.properties })\n this.flush()\n }\n\n /** captureException — @sentry-familiar alias of captureError. */\n captureException = this.captureError.bind(this)\n\n /** setCohort persists cohort dimensions (e.g. signupWeek at signup) so they ride\n * every subsequent event. */\n setCohort(patch: Cohort): void {\n this.cohort = mergeCohort(patch)\n }\n\n /** flush drains the buffer to the server as ONE batch through the ONE ingest\n * front door POST /v1/event, body { batch: [Event…] }. beacon=true selects the\n * unload-safe transport. Auth is orthogonal to the wire:\n *\n * • publishable key set → rides Authorization: Bearer pk_… (fetch) or\n * ?ingest_key=pk_… (beacon), so unload beacons work anonymously.\n * • else a bearer JWT rides Authorization (fetch only — sendBeacon cannot\n * carry a header, so token apps fall back to keepalive fetch on unload).\n * • else a cookie app rides same-origin credentials (beacon carries the\n * cookie fine).\n */\n flush(beacon = false): void {\n if (!this.cfg.enabled || this.queue.length === 0) return\n const batch = this.queue\n this.queue = []\n this.clearTimer()\n\n const key = this.cfg.ingestKey?.trim() || undefined\n // A publishable key and a bearer JWT are mutually exclusive doors; the key wins.\n const token = key ? undefined : this.cfg.getToken?.() ?? undefined\n // Only a headerful bearer JWT blocks the beacon: sendBeacon cannot set an\n // Authorization header. A pk_ rides ?ingest_key; a cookie rides credentials.\n const useBeacon = beacon && !token\n const body = JSON.stringify({ batch })\n if (this.cfg.debug) console.debug('[event] flush →', EVENT_PATH, batch.length)\n this.transport.send(this.cfg.host + EVENT_PATH, body, { beacon: useBeacon, token, ingestKey: key })\n }\n\n // ── internals ────────────────────────────────────────────────────────────\n\n private enqueue(kind: EventKind, event: string | undefined, extra: Partial<WireEvent>): void {\n if (!this.cfg.enabled) return\n if (!this.started) this.init()\n this.queue.push(this.build(kind, event, extra))\n if (this.queue.length >= this.cfg.batchSize) this.flush()\n else this.schedule()\n }\n\n private build(kind: EventKind, event: string | undefined, extra: Partial<WireEvent>): WireEvent {\n const anon = anonId()\n return {\n messageId: uid(),\n type: kind,\n event,\n timestamp: new Date().toISOString(),\n distinctId: this.personId ?? anon,\n anonymousId: anon,\n personId: this.personId,\n sessionId: sessionId(),\n product: this.cfg.product,\n referrer: this.attribution.referrer,\n utm: this.attribution.utm,\n refCode: this.cohort.refCode ?? this.attribution.refCode,\n channel: this.cohort.channel ?? this.attribution.channel,\n signupWeek: this.cohort.signupWeek,\n library: '@hanzo/event',\n libraryVersion: VERSION,\n ...extra,\n }\n }\n\n private schedule(): void {\n if (this.timer || !this.cfg.enabled) return\n this.timer = setTimeout(() => {\n this.timer = null\n this.flush()\n }, this.cfg.flushIntervalMs)\n }\n\n private clearTimer(): void {\n if (this.timer) {\n clearTimeout(this.timer)\n this.timer = null\n }\n }\n}\n\n/** createAnalytics builds a client instance. Most apps use one shared instance. */\nexport function createAnalytics(config: AnalyticsConfig): Analytics {\n return new Analytics(config)\n}\n\n// Re-export the hydrate helpers so consumers can read persisted cohort/attribution\n// (e.g. to send refCode to the referrals API) without reaching into storage.\nexport { getCohort, getFirstTouch }\n","// Insights goals + cohorts, defined once as data so the console/insights UI and\n// every product agree on what \"a Signup\", \"a Sale\", and \"upgrade intent\" mean.\n// This is the machine-readable spec — the shared source of truth a sync step can\n// push into Insights, and what the guide documents.\n\nimport { EVENTS } from './events'\n\nexport interface GoalDef {\n /** Human label shown in Insights. */\n label: string\n /** The event whose occurrence counts as the goal conversion. */\n event: string\n /** Optional ordered funnel leading to the goal (for funnel insights). */\n funnel?: string[]\n /** Optional property equality filter that qualifies the conversion. */\n filter?: { property: string; equals: string }\n}\n\nexport const GOALS: Record<'signup' | 'sale' | 'upgradeIntent', GoalDef> = {\n // Signup: the conversion is signup_completed; the funnel is the four steps.\n signup: {\n label: 'Signup',\n event: EVENTS.SIGNUP_COMPLETED,\n funnel: [\n EVENTS.SIGNUP_VIEWED,\n EVENTS.SIGNUP_SUBMITTED,\n EVENTS.SIGNUP_VERIFIED,\n EVENTS.FIRST_ACTION,\n ],\n },\n // Sale: a completed order qualified as a plan purchase (kind=plan).\n sale: {\n label: 'Sale',\n event: EVENTS.ORDER_COMPLETED,\n filter: { property: 'kind', equals: 'plan' },\n },\n // Upgrade intent: a plan click; pricing_viewed is the top of its funnel.\n upgradeIntent: {\n label: 'Upgrade Intent',\n event: EVENTS.PLAN_CLICKED,\n funnel: [EVENTS.PRICING_VIEWED, EVENTS.PLAN_CLICKED, EVENTS.CHECKOUT_STARTED],\n },\n}\n\nexport interface CohortDef {\n /** The hanzo.events column the cohort dimension maps to. */\n field: string\n label: string\n}\n\nexport const COHORTS: Record<'signupWeek' | 'channel' | 'refCode', CohortDef> = {\n signupWeek: { field: 'signup_week', label: 'Signup week' },\n channel: { field: 'channel', label: 'Acquisition channel' },\n refCode: { field: 'ref_code', label: 'Referral code' },\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as Cohort, A as Attribution } from './core-DDGwms7M.cjs';
2
- export { a as Analytics, b as AnalyticsConfig, E as EventKind, c as Exception, T as Transport, V as VERSION, W as WireEvent, d as createAnalytics } from './core-DDGwms7M.cjs';
1
+ import { C as Cohort, A as Attribution } from './core-CrbiAQhN.cjs';
2
+ export { a as Analytics, b as AnalyticsConfig, E as EventKind, c as Exception, T as Transport, V as VERSION, W as WireEvent, d as createAnalytics } from './core-CrbiAQhN.cjs';
3
3
 
4
4
  /** Read the persisted first-touch attribution. */
5
5
  declare function getFirstTouch(): Attribution | undefined;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as Cohort, A as Attribution } from './core-DDGwms7M.js';
2
- export { a as Analytics, b as AnalyticsConfig, E as EventKind, c as Exception, T as Transport, V as VERSION, W as WireEvent, d as createAnalytics } from './core-DDGwms7M.js';
1
+ import { C as Cohort, A as Attribution } from './core-CrbiAQhN.js';
2
+ export { a as Analytics, b as AnalyticsConfig, E as EventKind, c as Exception, T as Transport, V as VERSION, W as WireEvent, d as createAnalytics } from './core-CrbiAQhN.js';
3
3
 
4
4
  /** Read the persisted first-touch attribution. */
5
5
  declare function getFirstTouch(): Attribution | undefined;