@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/README.md CHANGED
@@ -13,7 +13,7 @@ referring site, kept the same way in a `traq_src` cookie — so a sign-up from a
13
13
  Instagram bio or a newsletter is still reported under its source and campaign.
14
14
 
15
15
  It also records web events and visits, with consent handling and in-page erasure. No
16
- fingerprinting, no third-party cookie, no advertising id. ~7 KB gzipped, no dependency.
16
+ fingerprinting, no third-party cookie, no advertising id. ~20 KB gzipped, no dependency.
17
17
 
18
18
  ## Install
19
19
 
@@ -25,7 +25,7 @@ Or without a build step, with a `<script>` tag (it initialises itself from the
25
25
  `data-` attributes and exposes `window.Traqio`):
26
26
 
27
27
  ```html
28
- <script src="https://cdn.jsdelivr.net/npm/@traqio/web@0.1/build/traqio.min.js"
28
+ <script src="https://cdn.jsdelivr.net/npm/@traqio/web@0/build/traqio.min.js"
29
29
  data-sdk-key="YOUR_WEB_KEY" async></script>
30
30
  ```
31
31
 
@@ -101,6 +101,11 @@ because a browser and a signed-in user are not tied together yet.
101
101
  Events are batched, retried, kept across page loads, and flushed when the tab is
102
102
  hidden or the page is left.
103
103
 
104
+ **A wrong or revoked key is not silent.** When the API answers 401 / 403, the SDK logs
105
+ one `console.warn` and calls `onError({ code: "invalid_sdk_key", status, message })`
106
+ once — not on every retry. The events are kept and retried with backoff (up to 5 min),
107
+ so fixing the key in the console delivers them.
108
+
104
109
  ## Product analytics
105
110
 
106
111
  Page views, traffic sources, clicks, scroll depth, front-end errors, Core Web Vitals and your own
@@ -223,6 +228,7 @@ networks.
223
228
  | `cookies` | `true` | `false` = localStorage only, no sharing across subdomains |
224
229
  | `debug` | detected | Debug traffic, kept out of every report (see below) |
225
230
  | `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, rageClicks, scroll, errors, webVitals }` (`webVitals: { sampleRate }`, default 0.5) |
231
+ | `onError` | — | Called once if the API refuses `sdkKey` (401 / 403) — see below |
226
232
 
227
233
  **Debug traffic.** Omitted, `debug` is on for a page served from `localhost`, `127.0.0.1`, `*.localhost`, `*.local` or `file:`, and off everywhere else; `true` forces it (a staging site), `false` counts a local page. `traqio.isDebug` tells which mode is on. Debug traffic shows in the console's **Live events › Debug** view, but it is never counted in a report, attributed, sent to an ad network or a webhook, or billed.
228
234
 
package/build/index.cjs CHANGED
@@ -825,7 +825,7 @@ var require_build = __commonJS({
825
825
  var adservices_1 = require_adservices();
826
826
  var deeplink_1 = require_deeplink();
827
827
  var adRevenue_1 = require_adRevenue();
828
- exports.SDK_VERSION = "0.11.0";
828
+ exports.SDK_VERSION = "0.12.0";
829
829
  var skan_2 = require_skan();
830
830
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
831
831
  return skan_2.Skan;
@@ -911,6 +911,7 @@ var require_build = __commonJS({
911
911
  var MAX_EARLY_ANALYTICS = 100;
912
912
  var MAX_BATCH = 20;
913
913
  var NON_RETRYABLE_STATUS = /* @__PURE__ */ new Set([400, 413, 422]);
914
+ var KEY_REJECTED_STATUS = /* @__PURE__ */ new Set([401, 403]);
914
915
  var MAX_QUEUE2 = 500;
915
916
  var MAX_AD_BATCH = 100;
916
917
  var MAX_AD_QUEUE = 1e3;
@@ -1025,6 +1026,16 @@ var require_build = __commonJS({
1025
1026
  return void 0;
1026
1027
  }
1027
1028
  }
1029
+ function isServerRender() {
1030
+ var _a, _b;
1031
+ if (typeof window !== "undefined")
1032
+ return false;
1033
+ try {
1034
+ return ((_b = (_a = (init_empty(), __toCommonJS(empty_exports))) == null ? void 0 : _a.Platform) == null ? void 0 : _b.OS) === "web";
1035
+ } catch {
1036
+ return false;
1037
+ }
1038
+ }
1028
1039
  var Client2 = class {
1029
1040
  constructor() {
1030
1041
  this.queue = [];
@@ -1037,6 +1048,7 @@ var require_build = __commonJS({
1037
1048
  this.flushing = false;
1038
1049
  this.flushTimer = null;
1039
1050
  this.retryMs = BASE_RETRY_MS;
1051
+ this.keyRejected = null;
1040
1052
  this.skan = new skan_1.Skan();
1041
1053
  this.erased = false;
1042
1054
  this.deepLinkListeners = /* @__PURE__ */ new Set();
@@ -1047,6 +1059,7 @@ var require_build = __commonJS({
1047
1059
  this.adIdConsent = null;
1048
1060
  this.adIdSync = null;
1049
1061
  this.debug = false;
1062
+ this.contextReady = false;
1050
1063
  this.analyticsQueue = [];
1051
1064
  this.analyticsSession = null;
1052
1065
  this.earlyAnalytics = [];
@@ -1072,6 +1085,8 @@ var require_build = __commonJS({
1072
1085
  var _a, _b;
1073
1086
  if (this.started)
1074
1087
  return;
1088
+ if (isServerRender())
1089
+ return;
1075
1090
  this.started = true;
1076
1091
  this.opts = { collectContext: true, ...opts, userId: (_a = opts.userId) != null ? _a : this.earlyUserId, storage: (_b = opts.storage) != null ? _b : defaultStorage() };
1077
1092
  if (!this.opts.storage) {
@@ -1084,6 +1099,7 @@ var require_build = __commonJS({
1084
1099
  await this.restoreAdQueue();
1085
1100
  await this.restoreAnalytics();
1086
1101
  await this.buildContext();
1102
+ this.contextReady = true;
1087
1103
  if (await this.restoreErased()) {
1088
1104
  this.earlyAnalytics = [];
1089
1105
  return;
@@ -2334,7 +2350,7 @@ var require_build = __commonJS({
2334
2350
  }
2335
2351
  async drain() {
2336
2352
  var _a, _b;
2337
- if (this.erased)
2353
+ if (this.erased || !this.contextReady)
2338
2354
  return;
2339
2355
  if (this.flushing || !this.queue.length && !this.adQueue.length && !this.analyticsQueue.length)
2340
2356
  return;
@@ -2416,11 +2432,33 @@ var require_build = __commonJS({
2416
2432
  body: JSON.stringify({ sdkKey: this.opts.sdkKey, ...body, ...this.debugField() }),
2417
2433
  ...this.keepalive()
2418
2434
  });
2435
+ this.noteStatus(res.status, res.ok);
2419
2436
  return res.ok ? await res.json() : null;
2420
2437
  } catch {
2421
2438
  return null;
2422
2439
  }
2423
2440
  }
2441
+ /**
2442
+ * A refused key is the one failure the developer must hear about: the batch is kept
2443
+ * and replayed (a key fixed in the console makes it succeed), so without this the
2444
+ * SDK would retry in silence forever. Reported once per status; a success re-arms it.
2445
+ */
2446
+ noteStatus(status, ok) {
2447
+ var _a, _b;
2448
+ if (ok) {
2449
+ this.keyRejected = null;
2450
+ return;
2451
+ }
2452
+ if (!KEY_REJECTED_STATUS.has(status) || this.keyRejected === status)
2453
+ return;
2454
+ this.keyRejected = status;
2455
+ 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.`;
2456
+ console.warn(message);
2457
+ try {
2458
+ (_b = (_a = this.opts).onError) == null ? void 0 : _b.call(_a, { code: "invalid_sdk_key", status, message });
2459
+ } catch {
2460
+ }
2461
+ }
2424
2462
  /**
2425
2463
  * The init option wins over what is stored (it is the app's current answer); a
2426
2464
  * `setConsent()` made before `init()` survives too — it is only kept in memory
@@ -2465,7 +2503,8 @@ var require_build = __commonJS({
2465
2503
  * `post`, with the failure split in two: `refused` is a status the same body will
2466
2504
  * get again on every retry (400 malformed, 413 too large, 422 unprocessable);
2467
2505
  * `retry` is everything else — network down, 5xx, 429, and 401/403 too, since a
2468
- * key fixed in the console makes the same queued batch succeed.
2506
+ * key fixed in the console makes the same queued batch succeed (those two are
2507
+ * reported once through `noteStatus`: console.warn + `onError`).
2469
2508
  */
2470
2509
  async send(body, path = "/ingest") {
2471
2510
  const consent = this.consent && (path === "/ingest" || path === "/ingest/session" || path === AD_ID_PATH) ? { consent: this.consent } : {};
@@ -2483,6 +2522,7 @@ var require_build = __commonJS({
2483
2522
  }),
2484
2523
  ...this.keepalive()
2485
2524
  });
2525
+ this.noteStatus(res.status, res.ok);
2486
2526
  if (res.ok)
2487
2527
  return "ok";
2488
2528
  return NON_RETRYABLE_STATUS.has(res.status) ? "refused" : "retry";
@@ -3578,6 +3618,7 @@ var TraqioWeb = class {
3578
3618
  userId: this.userId,
3579
3619
  consent,
3580
3620
  debug: opts.debug,
3621
+ onError: opts.onError,
3581
3622
  lifecycle: ({ background, foreground }) => {
3582
3623
  document.addEventListener("visibilitychange", () => document.visibilityState === "hidden" ? background() : foreground());
3583
3624
  window.addEventListener("pagehide", background);