@depsmith/depsmith-sdk 0.8.0 → 0.9.0

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
@@ -257,6 +257,15 @@ interface DepsmithSdkConfig {
257
257
  * consent, like deviceFingerprinting. Default false.
258
258
  */
259
259
  geolocation?: boolean;
260
+ /**
261
+ * Opt-in. Strip the fragment and query string from every URL the SDK sends — `context.page`
262
+ * url/referrer/previous_url, `context.referrer`, `$ac_href`, `$ac_form_action`, `$net_*`.
263
+ * `{ keepParams: ['utm_source'] }` keeps the named query parameters. Default false: URLs are
264
+ * sent whole. See API_SPEC §12.
265
+ */
266
+ scrubUrls?: boolean | {
267
+ keepParams?: string[];
268
+ };
260
269
  /** Per-field context suppression, all keys default true. Subtractive only; see API_SPEC §12.2. */
261
270
  trackingOptions?: TrackingOptions;
262
271
  /** Persist identity keys in a cookie scoped to this domain (cross-subdomain identity). */
@@ -321,6 +330,12 @@ interface AutocaptureConfig {
321
330
  * Given as bare names: `['product-id']` captures `data-product-id` as `$ac_attr_product_id`.
322
331
  */
323
332
  attributes?: string[];
333
+ /**
334
+ * `$network_request` for failed `fetch` calls, which wraps `window.fetch`. Default true (on
335
+ * whenever autocapture is); false leaves `fetch` untouched while keeping the rest of
336
+ * autocapture. See API_SPEC §12.6.
337
+ */
338
+ networkErrors?: boolean;
324
339
  }
325
340
 
326
341
  /**
@@ -332,7 +347,7 @@ interface KVStorage {
332
347
  set(key: string, value: string): void;
333
348
  remove(key: string): void;
334
349
  }
335
- declare const DEPSMITH_SDK_STORAGE_KEYS: readonly ["depsmith_anonymous_id", "depsmith_user_id", "depsmith_group_id", "depsmith_user_props", "depsmith_session", "depsmith_consent", "depsmith_queue", "depsmith_config_fp", "depsmith_net_breaker", "depsmith_campaign", "depsmith_referrer"];
350
+ declare const DEPSMITH_SDK_STORAGE_KEYS: readonly ["depsmith_anonymous_id", "depsmith_anon_reset_at", "depsmith_user_id", "depsmith_group_id", "depsmith_user_props", "depsmith_session", "depsmith_consent", "depsmith_queue", "depsmith_config_fp", "depsmith_net_breaker", "depsmith_campaign", "depsmith_referrer", "depsmith_super_props"];
336
351
  /**
337
352
  * Minimal async key-value backend. Shaped to accept Shopify's `browser.localStorage`
338
353
  * / `browser.sessionStorage` directly (all methods Promise-returning, proxied to the
@@ -440,7 +455,7 @@ declare class DepsmithSdkClient {
440
455
  * Autocapture's unload-time `$page_leave` trigger, when pageview tracking is on.
441
456
  *
442
457
  * The client owns the call site rather than autocapture, because it must run BEFORE the
443
- * teardown drain — see {@link attachLifecycle}. Undefined means no page-leave tracking:
458
+ * teardown drain — see `lifecycle.ts`. Undefined means no page-leave tracking:
444
459
  * autocapture is off, compiled out, or still loading.
445
460
  */
446
461
  private leaveOnUnload?;
@@ -450,13 +465,10 @@ declare class DepsmithSdkClient {
450
465
  private vitalsReported;
451
466
  private queue;
452
467
  private uploader;
453
- private flushTimer;
454
- /**
455
- * Set once a teardown drain has run, so `pagehide` and `visibilitychange`→hidden do not
456
- * both run it on one navigation. Cleared on resume, on `pageshow`, and by any newly
457
- * captured event ({@link enqueueEvent}).
458
- */
459
- private drained;
468
+ /** Timer and page listeners. Undefined under `manualLifecycle`, and in the pixel build. */
469
+ private lifecycle?;
470
+ /** Set by shutdown(); a lazily loaded autocapture chunk checks it before starting. */
471
+ private stopped;
460
472
  private teardownFns;
461
473
  private contextProvider?;
462
474
  /** Resolves once async storage (IndexedDB) is hydrated; uploads gate on this. */
@@ -475,14 +487,14 @@ declare class DepsmithSdkClient {
475
487
  *
476
488
  * Dropping the call is harsh, and it is the lesser harm. `event`, `user_id` and `group_id` are
477
489
  * all capped at 255 in event.schema.json, the collector validates every batch with Ajv, and a
478
- * 400 is non-retryable — so the uploader discards the WHOLE batch as a poison pill. For
479
- * `user_id` and `group_id` that is not a one-batch loss either: both are persisted and ride on
490
+ * 400 is non-retryable — the uploader bisects the batch and drops the event that fails. For
491
+ * `user_id` and `group_id` that is not a one-event loss either: both are persisted and ride on
480
492
  * every subsequent event, so a single over-long id silently destroys everything that user sends
481
493
  * until reset(). A JWT or a concatenated composite key passed as a user id does it.
482
494
  *
483
495
  * Truncating instead would keep the events but silently merge two users sharing a 255-char
484
496
  * prefix, inventing data rather than losing it. Rejecting a schema-invalid value at capture is
485
- * the rule throughout: such an event would 400 and take the whole batch with it.
497
+ * the rule throughout: such an event would 400 and be dropped at upload, after a bisection.
486
498
  */
487
499
  /** Client traits as sent: `$` keys dropped, then sanitised like event properties. */
488
500
  private traits;
@@ -520,6 +532,8 @@ declare class DepsmithSdkClient {
520
532
  group(groupId: string, traits?: Properties): void;
521
533
  setGroupTraits(traits: Properties): void;
522
534
  setProperty(key: string, value: Properties[string]): void;
535
+ /** The shared tail of setProperty / setOnceProperty: remember it locally, emit the operator. */
536
+ private setUserProp;
523
537
  /**
524
538
  * Sets a property only if this device has not already set it.
525
539
  *
@@ -598,16 +612,21 @@ declare class DepsmithSdkClient {
598
612
  */
599
613
  private persistNow;
600
614
  /**
601
- * Queues an event and re-arms the teardown drain.
602
- *
603
- * `drained` exists only to stop `pagehide` and `visibilitychange`→hidden running the
604
- * same drain twice on one navigation. It must not outlive the work it covered: on
605
- * desktop, `visibilitychange`→hidden fires on an ordinary tab switch, and a backgrounded
606
- * tab keeps capturing (the batch-interval timer, `online`, a host-driven `track()`). If
607
- * that tab is then closed without ever becoming visible again, a latch left set would
608
- * skip both the final `sendBeaconAll()` and the teardown write, losing everything
609
- * captured while hidden — a correctness hole opened by a pure optimisation.
615
+ * Device signals (fingerprint, Client Hints, position) may be read and sent: not COPPA mode,
616
+ * and consent granted — `canUpload()` is false while pending, after opt-out and under DNT.
617
+ * The ONE gate for both the reads below and the emission in `contextOptions()` (#87).
618
+ */
619
+ private deviceSignals;
620
+ /**
621
+ * Starts the once-per-document device-signal reads (high-entropy Client Hints, and the
622
+ * geolocation permission query + position), off the capture path — only when their results
623
+ * could be sent. Under pending or denied consent, or DNT, nothing is read; `optIn()` calls
624
+ * this again. Defaults are unchanged: this only skips reads whose results would be discarded.
625
+ * The geolocation gate is re-checked before every later read (permission change, stale-fix
626
+ * refresh), so `optOut()` stops those too.
610
627
  */
628
+ private primeDeviceSignals;
629
+ /** Queues an event and re-arms the teardown drain — see {@link BrowserLifecycle.rearm}. */
611
630
  private enqueueEvent;
612
631
  /**
613
632
  * The single entry point for starting an upload.
@@ -619,10 +638,12 @@ declare class DepsmithSdkClient {
619
638
  * exists to remove, reappearing in the exact case that matters most (a backed-up queue).
620
639
  */
621
640
  private flushUploads;
641
+ /** Starts an upload when there is something to send and consent allows it. */
642
+ private flushIfQueued;
622
643
  /**
623
- * Fingerprinting is opt-in and privacy-gated: it requires explicit config, granted
624
- * consent (consent.current is 'denied' under DNT and never 'granted' while pending),
625
- * and non-COPPA mode.
644
+ * Fingerprinting is on by default (opt-out via `deviceFingerprinting: false`) and
645
+ * privacy-gated: it requires granted consent (consent.current is 'denied' under DNT and
646
+ * never 'granted' while pending) and non-COPPA mode.
626
647
  */
627
648
  private contextOptions;
628
649
  /**
@@ -648,23 +669,46 @@ declare class DepsmithSdkClient {
648
669
  /**
649
670
  * Config drift (guidelines §12): events queued under a different apiKey belong to
650
671
  * another project — drop them rather than misattribute. Fingerprint, not raw key.
672
+ * The upload pause goes too: a 401 or Retry-After for that key says nothing about this one.
651
673
  */
652
674
  private detectConfigDrift;
675
+ /** The synchronous store. IndexedDB is never returned here — see the constructor. */
653
676
  private pickStorage;
654
677
  private enqueueSessionEnd;
655
678
  /**
656
679
  * Announces a session the SDK just observed beginning.
657
680
  *
658
- * Consent-gated for the same reason as enqueueSessionEnd: the visibilitychange resume
659
- * path reaches this without going through capture(). Under `pending` consent
660
- * canCapture() is true, so it is queued and held — matching capture() exactly.
661
- *
662
681
  * Not emitted by reset()/rotate() (an explicit user action, not an observed boundary)
663
682
  * nor by peek()'s mint-if-absent branch (analytics from a getter).
664
683
  */
665
684
  private enqueueSessionStart;
666
- private startFlushTimer;
667
- private attachLifecycle;
685
+ /**
686
+ * `$session_start` / `$session_end`, built in one place: the two envelopes differ only in
687
+ * their name, their timestamp and the fingerprint components the opening one carries.
688
+ */
689
+ private enqueueSessionBoundary;
690
+ /**
691
+ * Stamps session activity and emits the boundary events that revealed. The one place a
692
+ * session is touched, for capture() and for the tab-resume path alike: the resume path used
693
+ * to emit both boundaries with no sample check, so at `sampleRate: 0.1` nine in ten resumed
694
+ * sessions reached the warehouse as a bare `$session_start` with nothing after it.
695
+ *
696
+ * Order is load-bearing (API_SPEC §5): the old session's end precedes the new session's
697
+ * start, and both precede the event that triggered the rotation.
698
+ *
699
+ * Each boundary follows ITS OWN session's sample decision, not the current event's: the
700
+ * ending and starting sessions have different ids and can therefore decide differently, and
701
+ * an exempt $identify must not drag a boundary event into an out-of-sample session.
702
+ */
703
+ private touchSession;
704
+ /**
705
+ * `visibilitychange`→visible: run the session-timeout check.
706
+ *
707
+ * Not at all while capture is denied. capture() returns before touching the session in that
708
+ * state; this path did not, so every refocus by an opted-out or DNT visitor wrote a session id
709
+ * and an activity timestamp to storage.
710
+ */
711
+ private resumeSession;
668
712
  /** Lazily loads the autocapture engine so the core bundle stays <10KB gz. */
669
713
  private startAutocapture;
670
714
  private guard;
@@ -678,6 +722,7 @@ declare class DepsmithSdkClient {
678
722
  * DepsmithSdk.track('button_clicked', { color: 'red' });
679
723
  *
680
724
  * Re-init with the same apiKey is a no-op; a different apiKey replaces the client (API_SPEC §2).
725
+ * Without a `window` (server rendering) init() does nothing and returns null — see below.
681
726
  */
682
727
  declare const DepsmithSdk: {
683
728
  init(config: DepsmithSdkConfig, overrides?: InternalOverrides): DepsmithSdkClient | null;
package/dist/index.d.ts CHANGED
@@ -257,6 +257,15 @@ interface DepsmithSdkConfig {
257
257
  * consent, like deviceFingerprinting. Default false.
258
258
  */
259
259
  geolocation?: boolean;
260
+ /**
261
+ * Opt-in. Strip the fragment and query string from every URL the SDK sends — `context.page`
262
+ * url/referrer/previous_url, `context.referrer`, `$ac_href`, `$ac_form_action`, `$net_*`.
263
+ * `{ keepParams: ['utm_source'] }` keeps the named query parameters. Default false: URLs are
264
+ * sent whole. See API_SPEC §12.
265
+ */
266
+ scrubUrls?: boolean | {
267
+ keepParams?: string[];
268
+ };
260
269
  /** Per-field context suppression, all keys default true. Subtractive only; see API_SPEC §12.2. */
261
270
  trackingOptions?: TrackingOptions;
262
271
  /** Persist identity keys in a cookie scoped to this domain (cross-subdomain identity). */
@@ -321,6 +330,12 @@ interface AutocaptureConfig {
321
330
  * Given as bare names: `['product-id']` captures `data-product-id` as `$ac_attr_product_id`.
322
331
  */
323
332
  attributes?: string[];
333
+ /**
334
+ * `$network_request` for failed `fetch` calls, which wraps `window.fetch`. Default true (on
335
+ * whenever autocapture is); false leaves `fetch` untouched while keeping the rest of
336
+ * autocapture. See API_SPEC §12.6.
337
+ */
338
+ networkErrors?: boolean;
324
339
  }
325
340
 
326
341
  /**
@@ -332,7 +347,7 @@ interface KVStorage {
332
347
  set(key: string, value: string): void;
333
348
  remove(key: string): void;
334
349
  }
335
- declare const DEPSMITH_SDK_STORAGE_KEYS: readonly ["depsmith_anonymous_id", "depsmith_user_id", "depsmith_group_id", "depsmith_user_props", "depsmith_session", "depsmith_consent", "depsmith_queue", "depsmith_config_fp", "depsmith_net_breaker", "depsmith_campaign", "depsmith_referrer"];
350
+ declare const DEPSMITH_SDK_STORAGE_KEYS: readonly ["depsmith_anonymous_id", "depsmith_anon_reset_at", "depsmith_user_id", "depsmith_group_id", "depsmith_user_props", "depsmith_session", "depsmith_consent", "depsmith_queue", "depsmith_config_fp", "depsmith_net_breaker", "depsmith_campaign", "depsmith_referrer", "depsmith_super_props"];
336
351
  /**
337
352
  * Minimal async key-value backend. Shaped to accept Shopify's `browser.localStorage`
338
353
  * / `browser.sessionStorage` directly (all methods Promise-returning, proxied to the
@@ -440,7 +455,7 @@ declare class DepsmithSdkClient {
440
455
  * Autocapture's unload-time `$page_leave` trigger, when pageview tracking is on.
441
456
  *
442
457
  * The client owns the call site rather than autocapture, because it must run BEFORE the
443
- * teardown drain — see {@link attachLifecycle}. Undefined means no page-leave tracking:
458
+ * teardown drain — see `lifecycle.ts`. Undefined means no page-leave tracking:
444
459
  * autocapture is off, compiled out, or still loading.
445
460
  */
446
461
  private leaveOnUnload?;
@@ -450,13 +465,10 @@ declare class DepsmithSdkClient {
450
465
  private vitalsReported;
451
466
  private queue;
452
467
  private uploader;
453
- private flushTimer;
454
- /**
455
- * Set once a teardown drain has run, so `pagehide` and `visibilitychange`→hidden do not
456
- * both run it on one navigation. Cleared on resume, on `pageshow`, and by any newly
457
- * captured event ({@link enqueueEvent}).
458
- */
459
- private drained;
468
+ /** Timer and page listeners. Undefined under `manualLifecycle`, and in the pixel build. */
469
+ private lifecycle?;
470
+ /** Set by shutdown(); a lazily loaded autocapture chunk checks it before starting. */
471
+ private stopped;
460
472
  private teardownFns;
461
473
  private contextProvider?;
462
474
  /** Resolves once async storage (IndexedDB) is hydrated; uploads gate on this. */
@@ -475,14 +487,14 @@ declare class DepsmithSdkClient {
475
487
  *
476
488
  * Dropping the call is harsh, and it is the lesser harm. `event`, `user_id` and `group_id` are
477
489
  * all capped at 255 in event.schema.json, the collector validates every batch with Ajv, and a
478
- * 400 is non-retryable — so the uploader discards the WHOLE batch as a poison pill. For
479
- * `user_id` and `group_id` that is not a one-batch loss either: both are persisted and ride on
490
+ * 400 is non-retryable — the uploader bisects the batch and drops the event that fails. For
491
+ * `user_id` and `group_id` that is not a one-event loss either: both are persisted and ride on
480
492
  * every subsequent event, so a single over-long id silently destroys everything that user sends
481
493
  * until reset(). A JWT or a concatenated composite key passed as a user id does it.
482
494
  *
483
495
  * Truncating instead would keep the events but silently merge two users sharing a 255-char
484
496
  * prefix, inventing data rather than losing it. Rejecting a schema-invalid value at capture is
485
- * the rule throughout: such an event would 400 and take the whole batch with it.
497
+ * the rule throughout: such an event would 400 and be dropped at upload, after a bisection.
486
498
  */
487
499
  /** Client traits as sent: `$` keys dropped, then sanitised like event properties. */
488
500
  private traits;
@@ -520,6 +532,8 @@ declare class DepsmithSdkClient {
520
532
  group(groupId: string, traits?: Properties): void;
521
533
  setGroupTraits(traits: Properties): void;
522
534
  setProperty(key: string, value: Properties[string]): void;
535
+ /** The shared tail of setProperty / setOnceProperty: remember it locally, emit the operator. */
536
+ private setUserProp;
523
537
  /**
524
538
  * Sets a property only if this device has not already set it.
525
539
  *
@@ -598,16 +612,21 @@ declare class DepsmithSdkClient {
598
612
  */
599
613
  private persistNow;
600
614
  /**
601
- * Queues an event and re-arms the teardown drain.
602
- *
603
- * `drained` exists only to stop `pagehide` and `visibilitychange`→hidden running the
604
- * same drain twice on one navigation. It must not outlive the work it covered: on
605
- * desktop, `visibilitychange`→hidden fires on an ordinary tab switch, and a backgrounded
606
- * tab keeps capturing (the batch-interval timer, `online`, a host-driven `track()`). If
607
- * that tab is then closed without ever becoming visible again, a latch left set would
608
- * skip both the final `sendBeaconAll()` and the teardown write, losing everything
609
- * captured while hidden — a correctness hole opened by a pure optimisation.
615
+ * Device signals (fingerprint, Client Hints, position) may be read and sent: not COPPA mode,
616
+ * and consent granted — `canUpload()` is false while pending, after opt-out and under DNT.
617
+ * The ONE gate for both the reads below and the emission in `contextOptions()` (#87).
618
+ */
619
+ private deviceSignals;
620
+ /**
621
+ * Starts the once-per-document device-signal reads (high-entropy Client Hints, and the
622
+ * geolocation permission query + position), off the capture path — only when their results
623
+ * could be sent. Under pending or denied consent, or DNT, nothing is read; `optIn()` calls
624
+ * this again. Defaults are unchanged: this only skips reads whose results would be discarded.
625
+ * The geolocation gate is re-checked before every later read (permission change, stale-fix
626
+ * refresh), so `optOut()` stops those too.
610
627
  */
628
+ private primeDeviceSignals;
629
+ /** Queues an event and re-arms the teardown drain — see {@link BrowserLifecycle.rearm}. */
611
630
  private enqueueEvent;
612
631
  /**
613
632
  * The single entry point for starting an upload.
@@ -619,10 +638,12 @@ declare class DepsmithSdkClient {
619
638
  * exists to remove, reappearing in the exact case that matters most (a backed-up queue).
620
639
  */
621
640
  private flushUploads;
641
+ /** Starts an upload when there is something to send and consent allows it. */
642
+ private flushIfQueued;
622
643
  /**
623
- * Fingerprinting is opt-in and privacy-gated: it requires explicit config, granted
624
- * consent (consent.current is 'denied' under DNT and never 'granted' while pending),
625
- * and non-COPPA mode.
644
+ * Fingerprinting is on by default (opt-out via `deviceFingerprinting: false`) and
645
+ * privacy-gated: it requires granted consent (consent.current is 'denied' under DNT and
646
+ * never 'granted' while pending) and non-COPPA mode.
626
647
  */
627
648
  private contextOptions;
628
649
  /**
@@ -648,23 +669,46 @@ declare class DepsmithSdkClient {
648
669
  /**
649
670
  * Config drift (guidelines §12): events queued under a different apiKey belong to
650
671
  * another project — drop them rather than misattribute. Fingerprint, not raw key.
672
+ * The upload pause goes too: a 401 or Retry-After for that key says nothing about this one.
651
673
  */
652
674
  private detectConfigDrift;
675
+ /** The synchronous store. IndexedDB is never returned here — see the constructor. */
653
676
  private pickStorage;
654
677
  private enqueueSessionEnd;
655
678
  /**
656
679
  * Announces a session the SDK just observed beginning.
657
680
  *
658
- * Consent-gated for the same reason as enqueueSessionEnd: the visibilitychange resume
659
- * path reaches this without going through capture(). Under `pending` consent
660
- * canCapture() is true, so it is queued and held — matching capture() exactly.
661
- *
662
681
  * Not emitted by reset()/rotate() (an explicit user action, not an observed boundary)
663
682
  * nor by peek()'s mint-if-absent branch (analytics from a getter).
664
683
  */
665
684
  private enqueueSessionStart;
666
- private startFlushTimer;
667
- private attachLifecycle;
685
+ /**
686
+ * `$session_start` / `$session_end`, built in one place: the two envelopes differ only in
687
+ * their name, their timestamp and the fingerprint components the opening one carries.
688
+ */
689
+ private enqueueSessionBoundary;
690
+ /**
691
+ * Stamps session activity and emits the boundary events that revealed. The one place a
692
+ * session is touched, for capture() and for the tab-resume path alike: the resume path used
693
+ * to emit both boundaries with no sample check, so at `sampleRate: 0.1` nine in ten resumed
694
+ * sessions reached the warehouse as a bare `$session_start` with nothing after it.
695
+ *
696
+ * Order is load-bearing (API_SPEC §5): the old session's end precedes the new session's
697
+ * start, and both precede the event that triggered the rotation.
698
+ *
699
+ * Each boundary follows ITS OWN session's sample decision, not the current event's: the
700
+ * ending and starting sessions have different ids and can therefore decide differently, and
701
+ * an exempt $identify must not drag a boundary event into an out-of-sample session.
702
+ */
703
+ private touchSession;
704
+ /**
705
+ * `visibilitychange`→visible: run the session-timeout check.
706
+ *
707
+ * Not at all while capture is denied. capture() returns before touching the session in that
708
+ * state; this path did not, so every refocus by an opted-out or DNT visitor wrote a session id
709
+ * and an activity timestamp to storage.
710
+ */
711
+ private resumeSession;
668
712
  /** Lazily loads the autocapture engine so the core bundle stays <10KB gz. */
669
713
  private startAutocapture;
670
714
  private guard;
@@ -678,6 +722,7 @@ declare class DepsmithSdkClient {
678
722
  * DepsmithSdk.track('button_clicked', { color: 'red' });
679
723
  *
680
724
  * Re-init with the same apiKey is a no-op; a different apiKey replaces the client (API_SPEC §2).
725
+ * Without a `window` (server rendering) init() does nothing and returns null — see below.
681
726
  */
682
727
  declare const DepsmithSdk: {
683
728
  init(config: DepsmithSdkConfig, overrides?: InternalOverrides): DepsmithSdkClient | null;