@corbado/observe 0.16.5 → 0.16.6

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.mts CHANGED
@@ -58,7 +58,10 @@ type UserReference = {
58
58
  /** Shared UUID used to link sessions across environments. */
59
59
  crossEnvironmentTransactionID?: string;
60
60
  };
61
- /** A stable reference to all flows opened by one flow_started event. */
61
+ /**
62
+ * A stable reference to the flows one flow event belongs to: every flow a `flow_started` opened,
63
+ * or the flow a `flow_finished` finished.
64
+ */
62
65
  interface FlowHandle {
63
66
  /** Attach identity to this journey, including after it finishes or the tracker session changes.
64
67
  * Resolves when tracking/enqueue completes, not when server delivery is acknowledged.
@@ -262,6 +265,14 @@ interface EventBatchMeta {
262
265
  * changes propagate with page-load lag, so calendar-date cohorts are mixed).
263
266
  */
264
267
  configVersion?: string;
268
+ /**
269
+ * Project-scoped data policy code (integer 0..255; an opaque selector of a policy configured in
270
+ * Corbado, 0 = the project default) the tracker is running under, as set via
271
+ * `init({ dataPolicy })` or `setDataPolicy()`. Once known it is sent on every batch (including an
272
+ * explicit 0); omitted while no policy has ever been set. The server records the latest code it
273
+ * receives per session (last write wins), so the SDK always sends the current value.
274
+ */
275
+ dataPolicy?: number;
265
276
  }
266
277
  interface EventMeta {
267
278
  initiator?: LowEventInitiator;
@@ -1227,6 +1238,12 @@ interface TrackerOptions {
1227
1238
  * assignments and then attached to every event the tracker produces (see {@link CorbadoTracker.setExperiment}).
1228
1239
  */
1229
1240
  experiments?: Record<string, string>;
1241
+ /**
1242
+ * Data policy code to seed at init: a project-scoped non-negative integer (0..255) selecting a
1243
+ * policy configured in Corbado (0 is the project default). Overlays any persisted code and is then
1244
+ * sent as `meta.dataPolicy` on every batch (see {@link CorbadoTracker.setDataPolicy}).
1245
+ */
1246
+ dataPolicy?: number;
1230
1247
  applicationId?: string;
1231
1248
  /**
1232
1249
  * @deprecated The flush interval is server-controlled via the SDK reliability config
@@ -1267,6 +1284,11 @@ declare class CorbadoTracker {
1267
1284
  private lastActivityWriteAt;
1268
1285
  /** Persistent experiment assignments (experiment key → variant), attached to every produced event. */
1269
1286
  private experiments;
1287
+ /**
1288
+ * Persistent data policy code, stamped into every batch's `meta.dataPolicy` once set. Never
1289
+ * cleared (only overwritten): it survives reloads, session rotation, resetSession and destroy.
1290
+ */
1291
+ private dataPolicy?;
1270
1292
  /** Self-instrumentation state: re-entrancy guard, readiness, per-load dedupe, and pre-queue buffer. */
1271
1293
  private telemetryReporting;
1272
1294
  private telemetryReady;
@@ -1315,6 +1337,23 @@ declare class CorbadoTracker {
1315
1337
  * @returns A frozen snapshot, created fresh on every call.
1316
1338
  */
1317
1339
  getSdkConfig(): ObserveSdkConfigSnapshot;
1340
+ /**
1341
+ * Set the data policy code for this tracker (e.g. `setDataPolicy(1)` once the user agreed to
1342
+ * extended processing; `setDataPolicy(0)` to return to the project default). Codes are
1343
+ * project-scoped non-negative integers (0..255) selecting a policy configured in Corbado; they
1344
+ * carry no ordering.
1345
+ *
1346
+ * The code is persisted (localStorage, project-scoped) and sent as `meta.dataPolicy` on every
1347
+ * subsequent batch, across reloads and session rotation. It is never cleared, only overwritten.
1348
+ * The server records the latest code it receives for a session (last write wins). Invalid codes
1349
+ * are ignored with a debug log; never throws.
1350
+ */
1351
+ setDataPolicy(code: number): void;
1352
+ /** Returns the current data policy code, or undefined when none has ever been set. */
1353
+ getDataPolicy(): number | undefined;
1354
+ private static isValidDataPolicy;
1355
+ private loadPersistedDataPolicy;
1356
+ private persistDataPolicy;
1318
1357
  private static isValidExperimentKey;
1319
1358
  private static isValidExperimentVariant;
1320
1359
  private loadPersistedExperiments;
@@ -1427,7 +1466,7 @@ declare class CorbadoTracker {
1427
1466
  * (such as login vs signup), pass `flowNames` and optionally `defaultFlowName`.
1428
1467
  *
1429
1468
  * The returned handle can attach identity after completion with `await flow.enrich({ userId })`.
1430
- * Enrichment requires a backend supporting explicit start-event targeting.
1469
+ * Enrichment requires a backend supporting explicit event targeting.
1431
1470
  *
1432
1471
  * @param data - Flow metadata, including one flow (`flowName`) or multiple candidates (`flowNames`).
1433
1472
  * @param tags - Optional key-value tags for filtering and segmentation.
@@ -1491,7 +1530,12 @@ declare class CorbadoTracker {
1491
1530
  * @remarks
1492
1531
  * Fire this when the user has finished the target flow end-to-end, such as completed login,
1493
1532
  * signup, or recovery. Record identity with `setUser()` while the flow is active,
1494
- * or use the handle returned by `flowStarted()` to enrich it after completion.
1533
+ * or enrich it after completion through the handle returned by `flowStarted()` or by this call.
1534
+ *
1535
+ * The returned handle targets the flow this event finished, so identity learned only after
1536
+ * completion — for example from a profile request that follows account creation — can be
1537
+ * attached with `await finished.enrich({ userId })` even on a page that never saw the flow start.
1538
+ * Enrichment requires a backend supporting explicit flow-event targeting.
1495
1539
  *
1496
1540
  * Legacy identity fields in `data` remain supported but are deprecated.
1497
1541
  *
@@ -1500,12 +1544,14 @@ declare class CorbadoTracker {
1500
1544
  *
1501
1545
  * @example
1502
1546
  * ```typescript
1503
- * tracker.flowFinished({
1504
- * flowName: "login",
1505
- * });
1547
+ * const finished = tracker.flowFinished({ flowName: "signup" });
1548
+ * const userId = await hashUserId(createdAccountId);
1549
+ * await finished.enrich({ userId });
1506
1550
  * ```
1507
1551
  */
1508
- flowFinished(data: FlowFinished, tags?: Record<string, string>, experiments?: Record<string, string>): void;
1552
+ flowFinished(data: FlowFinished, tags?: Record<string, string>, experiments?: Record<string, string>): FlowHandle;
1553
+ /** Emits `flow_enriched` against an explicit target, in the session that emitted the target. */
1554
+ private flowHandle;
1509
1555
  /**
1510
1556
  * Track when an in-progress flow is reset and restarted.
1511
1557
  *
@@ -1951,6 +1997,11 @@ interface QueueOptions {
1951
1997
  * {@link EventStore}). Defaults to the shared {@link OUTBOX_LOCK_NAME}.
1952
1998
  */
1953
1999
  outboxLockName?: string;
2000
+ /**
2001
+ * Returns the data policy code to stamp into `meta.dataPolicy` of every batch, or undefined when
2002
+ * none has ever been set. Read at send time so a change applies to the next batch without a flush.
2003
+ */
2004
+ getDataPolicy?: () => number | undefined;
1954
2005
  }
1955
2006
  /**
1956
2007
  * @remarks When `window` is defined, `visibilitychange` (when the document becomes hidden) and `pagehide`
@@ -2004,6 +2055,7 @@ declare class RequestQueue {
2004
2055
  private readonly debug;
2005
2056
  private readonly requestConfig;
2006
2057
  private readonly onConfigReceived?;
2058
+ private readonly getDataPolicy?;
2007
2059
  /** Current delivery policy, including live updates. */
2008
2060
  private config;
2009
2061
  /**
@@ -2074,10 +2126,13 @@ declare class RequestQueue {
2074
2126
  * Stamps delivery metadata onto the batch: the trigger that caused this flush, the config version
2075
2127
  * this load runs under (omitted on built-in defaults) and the number of prior failed delivery
2076
2128
  * attempts (the max across its entries, only when at least one entry is a retry). `entry.attempts`
2077
- * is incremented on each failed flush, so it equals the retry count at send time. The transport
2078
- * later merges in `sent`/`transport`.
2129
+ * is incremented on each failed flush, so it equals the retry count at send time. The data policy
2130
+ * code (when one has ever been set) is read live, so it reflects the tracker state at send time.
2131
+ * The transport later merges in `sent`/`transport`.
2079
2132
  */
2080
2133
  private attachBatchMeta;
2134
+ /** The data policy code from the tracker, or undefined; a throwing getter must never break a flush. */
2135
+ private readDataPolicy;
2081
2136
  private getDuePending;
2082
2137
  private takeLows;
2083
2138
  private validPendingLow;
package/dist/index.d.ts CHANGED
@@ -58,7 +58,10 @@ type UserReference = {
58
58
  /** Shared UUID used to link sessions across environments. */
59
59
  crossEnvironmentTransactionID?: string;
60
60
  };
61
- /** A stable reference to all flows opened by one flow_started event. */
61
+ /**
62
+ * A stable reference to the flows one flow event belongs to: every flow a `flow_started` opened,
63
+ * or the flow a `flow_finished` finished.
64
+ */
62
65
  interface FlowHandle {
63
66
  /** Attach identity to this journey, including after it finishes or the tracker session changes.
64
67
  * Resolves when tracking/enqueue completes, not when server delivery is acknowledged.
@@ -262,6 +265,14 @@ interface EventBatchMeta {
262
265
  * changes propagate with page-load lag, so calendar-date cohorts are mixed).
263
266
  */
264
267
  configVersion?: string;
268
+ /**
269
+ * Project-scoped data policy code (integer 0..255; an opaque selector of a policy configured in
270
+ * Corbado, 0 = the project default) the tracker is running under, as set via
271
+ * `init({ dataPolicy })` or `setDataPolicy()`. Once known it is sent on every batch (including an
272
+ * explicit 0); omitted while no policy has ever been set. The server records the latest code it
273
+ * receives per session (last write wins), so the SDK always sends the current value.
274
+ */
275
+ dataPolicy?: number;
265
276
  }
266
277
  interface EventMeta {
267
278
  initiator?: LowEventInitiator;
@@ -1227,6 +1238,12 @@ interface TrackerOptions {
1227
1238
  * assignments and then attached to every event the tracker produces (see {@link CorbadoTracker.setExperiment}).
1228
1239
  */
1229
1240
  experiments?: Record<string, string>;
1241
+ /**
1242
+ * Data policy code to seed at init: a project-scoped non-negative integer (0..255) selecting a
1243
+ * policy configured in Corbado (0 is the project default). Overlays any persisted code and is then
1244
+ * sent as `meta.dataPolicy` on every batch (see {@link CorbadoTracker.setDataPolicy}).
1245
+ */
1246
+ dataPolicy?: number;
1230
1247
  applicationId?: string;
1231
1248
  /**
1232
1249
  * @deprecated The flush interval is server-controlled via the SDK reliability config
@@ -1267,6 +1284,11 @@ declare class CorbadoTracker {
1267
1284
  private lastActivityWriteAt;
1268
1285
  /** Persistent experiment assignments (experiment key → variant), attached to every produced event. */
1269
1286
  private experiments;
1287
+ /**
1288
+ * Persistent data policy code, stamped into every batch's `meta.dataPolicy` once set. Never
1289
+ * cleared (only overwritten): it survives reloads, session rotation, resetSession and destroy.
1290
+ */
1291
+ private dataPolicy?;
1270
1292
  /** Self-instrumentation state: re-entrancy guard, readiness, per-load dedupe, and pre-queue buffer. */
1271
1293
  private telemetryReporting;
1272
1294
  private telemetryReady;
@@ -1315,6 +1337,23 @@ declare class CorbadoTracker {
1315
1337
  * @returns A frozen snapshot, created fresh on every call.
1316
1338
  */
1317
1339
  getSdkConfig(): ObserveSdkConfigSnapshot;
1340
+ /**
1341
+ * Set the data policy code for this tracker (e.g. `setDataPolicy(1)` once the user agreed to
1342
+ * extended processing; `setDataPolicy(0)` to return to the project default). Codes are
1343
+ * project-scoped non-negative integers (0..255) selecting a policy configured in Corbado; they
1344
+ * carry no ordering.
1345
+ *
1346
+ * The code is persisted (localStorage, project-scoped) and sent as `meta.dataPolicy` on every
1347
+ * subsequent batch, across reloads and session rotation. It is never cleared, only overwritten.
1348
+ * The server records the latest code it receives for a session (last write wins). Invalid codes
1349
+ * are ignored with a debug log; never throws.
1350
+ */
1351
+ setDataPolicy(code: number): void;
1352
+ /** Returns the current data policy code, or undefined when none has ever been set. */
1353
+ getDataPolicy(): number | undefined;
1354
+ private static isValidDataPolicy;
1355
+ private loadPersistedDataPolicy;
1356
+ private persistDataPolicy;
1318
1357
  private static isValidExperimentKey;
1319
1358
  private static isValidExperimentVariant;
1320
1359
  private loadPersistedExperiments;
@@ -1427,7 +1466,7 @@ declare class CorbadoTracker {
1427
1466
  * (such as login vs signup), pass `flowNames` and optionally `defaultFlowName`.
1428
1467
  *
1429
1468
  * The returned handle can attach identity after completion with `await flow.enrich({ userId })`.
1430
- * Enrichment requires a backend supporting explicit start-event targeting.
1469
+ * Enrichment requires a backend supporting explicit event targeting.
1431
1470
  *
1432
1471
  * @param data - Flow metadata, including one flow (`flowName`) or multiple candidates (`flowNames`).
1433
1472
  * @param tags - Optional key-value tags for filtering and segmentation.
@@ -1491,7 +1530,12 @@ declare class CorbadoTracker {
1491
1530
  * @remarks
1492
1531
  * Fire this when the user has finished the target flow end-to-end, such as completed login,
1493
1532
  * signup, or recovery. Record identity with `setUser()` while the flow is active,
1494
- * or use the handle returned by `flowStarted()` to enrich it after completion.
1533
+ * or enrich it after completion through the handle returned by `flowStarted()` or by this call.
1534
+ *
1535
+ * The returned handle targets the flow this event finished, so identity learned only after
1536
+ * completion — for example from a profile request that follows account creation — can be
1537
+ * attached with `await finished.enrich({ userId })` even on a page that never saw the flow start.
1538
+ * Enrichment requires a backend supporting explicit flow-event targeting.
1495
1539
  *
1496
1540
  * Legacy identity fields in `data` remain supported but are deprecated.
1497
1541
  *
@@ -1500,12 +1544,14 @@ declare class CorbadoTracker {
1500
1544
  *
1501
1545
  * @example
1502
1546
  * ```typescript
1503
- * tracker.flowFinished({
1504
- * flowName: "login",
1505
- * });
1547
+ * const finished = tracker.flowFinished({ flowName: "signup" });
1548
+ * const userId = await hashUserId(createdAccountId);
1549
+ * await finished.enrich({ userId });
1506
1550
  * ```
1507
1551
  */
1508
- flowFinished(data: FlowFinished, tags?: Record<string, string>, experiments?: Record<string, string>): void;
1552
+ flowFinished(data: FlowFinished, tags?: Record<string, string>, experiments?: Record<string, string>): FlowHandle;
1553
+ /** Emits `flow_enriched` against an explicit target, in the session that emitted the target. */
1554
+ private flowHandle;
1509
1555
  /**
1510
1556
  * Track when an in-progress flow is reset and restarted.
1511
1557
  *
@@ -1951,6 +1997,11 @@ interface QueueOptions {
1951
1997
  * {@link EventStore}). Defaults to the shared {@link OUTBOX_LOCK_NAME}.
1952
1998
  */
1953
1999
  outboxLockName?: string;
2000
+ /**
2001
+ * Returns the data policy code to stamp into `meta.dataPolicy` of every batch, or undefined when
2002
+ * none has ever been set. Read at send time so a change applies to the next batch without a flush.
2003
+ */
2004
+ getDataPolicy?: () => number | undefined;
1954
2005
  }
1955
2006
  /**
1956
2007
  * @remarks When `window` is defined, `visibilitychange` (when the document becomes hidden) and `pagehide`
@@ -2004,6 +2055,7 @@ declare class RequestQueue {
2004
2055
  private readonly debug;
2005
2056
  private readonly requestConfig;
2006
2057
  private readonly onConfigReceived?;
2058
+ private readonly getDataPolicy?;
2007
2059
  /** Current delivery policy, including live updates. */
2008
2060
  private config;
2009
2061
  /**
@@ -2074,10 +2126,13 @@ declare class RequestQueue {
2074
2126
  * Stamps delivery metadata onto the batch: the trigger that caused this flush, the config version
2075
2127
  * this load runs under (omitted on built-in defaults) and the number of prior failed delivery
2076
2128
  * attempts (the max across its entries, only when at least one entry is a retry). `entry.attempts`
2077
- * is incremented on each failed flush, so it equals the retry count at send time. The transport
2078
- * later merges in `sent`/`transport`.
2129
+ * is incremented on each failed flush, so it equals the retry count at send time. The data policy
2130
+ * code (when one has ever been set) is read live, so it reflects the tracker state at send time.
2131
+ * The transport later merges in `sent`/`transport`.
2079
2132
  */
2080
2133
  private attachBatchMeta;
2134
+ /** The data policy code from the tracker, or undefined; a throwing getter must never break a flush. */
2135
+ private readDataPolicy;
2081
2136
  private getDuePending;
2082
2137
  private takeLows;
2083
2138
  private validPendingLow;