@traqio/web 0.6.2 → 0.7.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/build/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
+ import { type TraqioError } from "@traqio/core";
1
2
  import { type WebSource } from "./source";
2
3
  import { type AnalyticsOptions, type Groups, type Traits } from "./analytics";
3
4
  export type { WebSource } from "./source";
4
5
  export type { AnalyticsOptions, Groups, Traits, TraitValue } from "./analytics";
5
6
  export type { WebVitalsOptions } from "./vitals";
7
+ export type { TraqioError } from "@traqio/core";
6
8
  export declare const DEFAULT_ENDPOINT = "https://api.traqio.app";
7
9
  export interface WebInitOptions {
8
10
  /** The site's WEB key (`tq_web_…`), from the console. Public by design: it is held to the site's origins. */
@@ -48,6 +50,12 @@ export interface WebInitOptions {
48
50
  * analytics events have their own route and table, apart from conversions.
49
51
  */
50
52
  analytics?: boolean | AnalyticsOptions;
53
+ /**
54
+ * Called once when the API refuses `sdkKey` (`code: "invalid_sdk_key"`, HTTP 401 / 403).
55
+ * Events stay queued and are retried with backoff, so fixing the key in the console
56
+ * delivers them. A `console.warn` is logged either way.
57
+ */
58
+ onError?: (error: TraqioError) => void;
51
59
  }
52
60
  /** What `track()` accepts beside the event name. */
53
61
  export interface WebTrackProps {
package/build/index.js CHANGED
@@ -820,7 +820,7 @@ var require_build = __commonJS({
820
820
  var adservices_1 = require_adservices();
821
821
  var deeplink_1 = require_deeplink();
822
822
  var adRevenue_1 = require_adRevenue();
823
- exports.SDK_VERSION = "0.11.0";
823
+ exports.SDK_VERSION = "0.12.0";
824
824
  var skan_2 = require_skan();
825
825
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
826
826
  return skan_2.Skan;
@@ -906,6 +906,7 @@ var require_build = __commonJS({
906
906
  var MAX_EARLY_ANALYTICS = 100;
907
907
  var MAX_BATCH = 20;
908
908
  var NON_RETRYABLE_STATUS = /* @__PURE__ */ new Set([400, 413, 422]);
909
+ var KEY_REJECTED_STATUS = /* @__PURE__ */ new Set([401, 403]);
909
910
  var MAX_QUEUE2 = 500;
910
911
  var MAX_AD_BATCH = 100;
911
912
  var MAX_AD_QUEUE = 1e3;
@@ -1020,6 +1021,16 @@ var require_build = __commonJS({
1020
1021
  return void 0;
1021
1022
  }
1022
1023
  }
1024
+ function isServerRender() {
1025
+ var _a, _b;
1026
+ if (typeof window !== "undefined")
1027
+ return false;
1028
+ try {
1029
+ return ((_b = (_a = (init_empty(), __toCommonJS(empty_exports))) == null ? void 0 : _a.Platform) == null ? void 0 : _b.OS) === "web";
1030
+ } catch {
1031
+ return false;
1032
+ }
1033
+ }
1023
1034
  var Client2 = class {
1024
1035
  constructor() {
1025
1036
  this.queue = [];
@@ -1032,6 +1043,7 @@ var require_build = __commonJS({
1032
1043
  this.flushing = false;
1033
1044
  this.flushTimer = null;
1034
1045
  this.retryMs = BASE_RETRY_MS;
1046
+ this.keyRejected = null;
1035
1047
  this.skan = new skan_1.Skan();
1036
1048
  this.erased = false;
1037
1049
  this.deepLinkListeners = /* @__PURE__ */ new Set();
@@ -1042,6 +1054,7 @@ var require_build = __commonJS({
1042
1054
  this.adIdConsent = null;
1043
1055
  this.adIdSync = null;
1044
1056
  this.debug = false;
1057
+ this.contextReady = false;
1045
1058
  this.analyticsQueue = [];
1046
1059
  this.analyticsSession = null;
1047
1060
  this.earlyAnalytics = [];
@@ -1067,6 +1080,8 @@ var require_build = __commonJS({
1067
1080
  var _a, _b;
1068
1081
  if (this.started)
1069
1082
  return;
1083
+ if (isServerRender())
1084
+ return;
1070
1085
  this.started = true;
1071
1086
  this.opts = { collectContext: true, ...opts, userId: (_a = opts.userId) != null ? _a : this.earlyUserId, storage: (_b = opts.storage) != null ? _b : defaultStorage() };
1072
1087
  if (!this.opts.storage) {
@@ -1079,6 +1094,7 @@ var require_build = __commonJS({
1079
1094
  await this.restoreAdQueue();
1080
1095
  await this.restoreAnalytics();
1081
1096
  await this.buildContext();
1097
+ this.contextReady = true;
1082
1098
  if (await this.restoreErased()) {
1083
1099
  this.earlyAnalytics = [];
1084
1100
  return;
@@ -2329,7 +2345,7 @@ var require_build = __commonJS({
2329
2345
  }
2330
2346
  async drain() {
2331
2347
  var _a, _b;
2332
- if (this.erased)
2348
+ if (this.erased || !this.contextReady)
2333
2349
  return;
2334
2350
  if (this.flushing || !this.queue.length && !this.adQueue.length && !this.analyticsQueue.length)
2335
2351
  return;
@@ -2411,11 +2427,33 @@ var require_build = __commonJS({
2411
2427
  body: JSON.stringify({ sdkKey: this.opts.sdkKey, ...body, ...this.debugField() }),
2412
2428
  ...this.keepalive()
2413
2429
  });
2430
+ this.noteStatus(res.status, res.ok);
2414
2431
  return res.ok ? await res.json() : null;
2415
2432
  } catch {
2416
2433
  return null;
2417
2434
  }
2418
2435
  }
2436
+ /**
2437
+ * A refused key is the one failure the developer must hear about: the batch is kept
2438
+ * and replayed (a key fixed in the console makes it succeed), so without this the
2439
+ * SDK would retry in silence forever. Reported once per status; a success re-arms it.
2440
+ */
2441
+ noteStatus(status, ok) {
2442
+ var _a, _b;
2443
+ if (ok) {
2444
+ this.keyRejected = null;
2445
+ return;
2446
+ }
2447
+ if (!KEY_REJECTED_STATUS.has(status) || this.keyRejected === status)
2448
+ return;
2449
+ this.keyRejected = status;
2450
+ const message = `[traqio] the server rejected the sdkKey (HTTP ${status}) \u2014 check the key in the console. Events are kept and retried with backoff until it is accepted.`;
2451
+ console.warn(message);
2452
+ try {
2453
+ (_b = (_a = this.opts).onError) == null ? void 0 : _b.call(_a, { code: "invalid_sdk_key", status, message });
2454
+ } catch {
2455
+ }
2456
+ }
2419
2457
  /**
2420
2458
  * The init option wins over what is stored (it is the app's current answer); a
2421
2459
  * `setConsent()` made before `init()` survives too — it is only kept in memory
@@ -2460,7 +2498,8 @@ var require_build = __commonJS({
2460
2498
  * `post`, with the failure split in two: `refused` is a status the same body will
2461
2499
  * get again on every retry (400 malformed, 413 too large, 422 unprocessable);
2462
2500
  * `retry` is everything else — network down, 5xx, 429, and 401/403 too, since a
2463
- * key fixed in the console makes the same queued batch succeed.
2501
+ * key fixed in the console makes the same queued batch succeed (those two are
2502
+ * reported once through `noteStatus`: console.warn + `onError`).
2464
2503
  */
2465
2504
  async send(body, path = "/ingest") {
2466
2505
  const consent = this.consent && (path === "/ingest" || path === "/ingest/session" || path === AD_ID_PATH) ? { consent: this.consent } : {};
@@ -2478,6 +2517,7 @@ var require_build = __commonJS({
2478
2517
  }),
2479
2518
  ...this.keepalive()
2480
2519
  });
2520
+ this.noteStatus(res.status, res.ok);
2481
2521
  if (res.ok)
2482
2522
  return "ok";
2483
2523
  return NON_RETRYABLE_STATUS.has(res.status) ? "refused" : "retry";
@@ -3565,6 +3605,7 @@ var TraqioWeb = class {
3565
3605
  userId: this.userId,
3566
3606
  consent,
3567
3607
  debug: opts.debug,
3608
+ onError: opts.onError,
3568
3609
  lifecycle: ({ background, foreground }) => {
3569
3610
  document.addEventListener("visibilitychange", () => document.visibilityState === "hidden" ? background() : foreground());
3570
3611
  window.addEventListener("pagehide", background);