@uniweb/core 1.6.2 → 1.6.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/tracker.js +60 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/core",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
package/src/tracker.js CHANGED
@@ -162,6 +162,36 @@ function captureAcquisition() {
162
162
  return Object.keys(context).length > 0 ? context : null
163
163
  }
164
164
 
165
+ /**
166
+ * The landing address's values for the query keys a conversion carries, read once at construction.
167
+ *
168
+ * ⭐ **The keys are named by whoever collects, never by this file** — `tracking.landing`, a host's
169
+ * or a site's own. Which campaign keys and which ad-platform click IDs matter is a collector's
170
+ * business, and the list grows with the platforms it sends to; a list held here would name vendors
171
+ * in a public package and need a release for each [framework, answering hosting, 2026-11-03].
172
+ *
173
+ * ⛔ Only the keys named, only from the address the visitor arrived at, held for this document and
174
+ * sent only on `conversion` — the event whose attribution otherwise depends on a collector joining
175
+ * it to the arrival. A query can carry a search term or a token, so nothing unnamed is read.
176
+ *
177
+ * Called only for an enabled tracker in a browser, so it checks neither. Every site loads this
178
+ * file, so it is as small as it can be read: no keys, or none on the address, is `null`.
179
+ *
180
+ * @param {string[]} [keys]
181
+ * @returns {Object<string, string>|null}
182
+ */
183
+ function captureLanding(keys) {
184
+ let landing = null
185
+ if (Array.isArray(keys)) {
186
+ const params = new URLSearchParams(window.location.search)
187
+ for (const key of keys) {
188
+ const value = typeof key === 'string' && params.get(key)
189
+ if (value) (landing ||= {})[key] = value
190
+ }
191
+ }
192
+ return landing
193
+ }
194
+
165
195
  /**
166
196
  * An opaque key for one document lifetime. Never stored, never persisted.
167
197
  *
@@ -199,6 +229,8 @@ export default class Tracker {
199
229
  * @param {number} [options.flushIntervalMs=5000] - batch window, MILLISECONDS
200
230
  * @param {number} [options.maxQueueSize=10]
201
231
  * @param {boolean} [options.debug=false]
232
+ * @param {string[]} [options.landing] - query keys of the landing address each `conversion`
233
+ * carries, as `landing: { key: value }`; see `captureLanding`
202
234
  */
203
235
  constructor(options = {}) {
204
236
  this.endpoint = options.endpoint || null
@@ -232,6 +264,8 @@ export default class Tracker {
232
264
 
233
265
  this.queue = []
234
266
  this.acquisition = null
267
+ // What a conversion carries of the landing address, when a collector named keys.
268
+ this.landing = null
235
269
 
236
270
  // One key per document, minted only when there is somewhere to send it.
237
271
  // Rides the ENVELOPE beside `event` rather than inside a payload, because
@@ -258,10 +292,6 @@ export default class Tracker {
258
292
  // already run.
259
293
  /** When the current path became active. */
260
294
  this.pageEnteredAt = null
261
- /** Hidden time accrued on the current path, in ms. */
262
- this.hiddenMs = 0
263
- /** When the document last became hidden, or `null` while visible. */
264
- this.hiddenSince = null
265
295
  /** Guards against a second report for one page visit — see `reportTimeOnPage`. */
266
296
  this.pageReported = false
267
297
 
@@ -295,6 +325,7 @@ export default class Tracker {
295
325
 
296
326
  if (isBrowser && this.isEnabled()) {
297
327
  this.acquisition = captureAcquisition()
328
+ this.landing = captureLanding(options.landing)
298
329
  // Minted even when consent is pending: events buffered before the visitor
299
330
  // answers must carry the SAME key as those after it, or granting consent
300
331
  // would split one visit in two. It never leaves the device until the
@@ -424,6 +455,11 @@ export default class Tracker {
424
455
  /**
425
456
  * Report an event.
426
457
  *
458
+ * A `conversion` also carries `landing`, the landing address's values for the keys a
459
+ * collector named (`captureLanding`), so its attribution survives navigation without the
460
+ * collector joining it to the arrival. Set after the caller's fields: it is this page
461
+ * load's fact, not a caller's.
462
+ *
427
463
  * @param {string} event - event name, e.g. 'video_milestone'. Open registry.
428
464
  * @param {Object} [data] - the caller's own fields; `path` overrides the
429
465
  * current route, which is what block-scoped callers supply.
@@ -431,7 +467,8 @@ export default class Tracker {
431
467
  track(event, data = {}) {
432
468
  if (!this.isEnabled() || !event) return
433
469
  const { path, ...rest } = data
434
- this.enqueue({ event, path: path || this.currentPath || undefined, ...rest })
470
+ const landing = event === 'conversion' && this.landing ? { landing: this.landing } : null
471
+ this.enqueue({ event, path: path || this.currentPath || undefined, ...rest, ...landing })
435
472
  }
436
473
 
437
474
  /**
@@ -621,8 +658,6 @@ export default class Tracker {
621
658
  */
622
659
  startTimeOnPage() {
623
660
  this.pageEnteredAt = Date.now()
624
- this.hiddenMs = 0
625
- this.hiddenSince = null
626
661
  this.pageReported = false
627
662
  }
628
663
 
@@ -636,21 +671,20 @@ export default class Tracker {
636
671
  * skewed — a tab left open overnight sits in the same mean as forty readers —
637
672
  * so a clamp is genuinely needed; it is just not needed *here*.
638
673
  *
639
- * ⛔ **Hidden time is subtracted**, or this measures tab-open rather than
640
- * reading. A backgrounded tab accrues nothing.
641
- *
642
- * ⛔ **Reported at a route change and at `pagehide` — NOT when the document
643
- * merely becomes hidden.** Visibility is a *pause*, not an end: someone who
644
- * switches tabs to look something up and comes back is still reading. Emitting
645
- * on hidden would truncate exactly the engaged readers this metric exists to
646
- * find, and — because the consumer derives a mean from a sum and a count —
647
- * emitting more than once per page visit would inflate the count and make
648
- * every mean wrong. Hence `pageReported`: **at most one per page visit.**
674
+ * ⭐ **Reported at the first of three ends: a route change, `pagehide`, or the
675
+ * document becoming hidden** [Diego, 2026-11-03]. A phone visitor who switches
676
+ * apps, to paste a copied prompt or answer a message, may never come back, and
677
+ * the phone can then discard the page with no `pagehide`. Reported only at
678
+ * `pagehide`, about four in ten views on a mostly-phone site carried no time
679
+ * at all (24robert.ca, 2026-11-01, measured by hosting). So every view now
680
+ * reports the time until the visitor first left it, and the number is a floor.
649
681
  *
650
- * ⚠️ **The cost of that choice, stated rather than hidden:** a page discarded
651
- * while hidden, without `pagehide`, is never reported. That is an
652
- * under-count, it is bounded, and it fails in the direction that does not
653
- * corrupt the statistic.
682
+ * ⚖️ **The cost, stated rather than hidden:** a reader who switches away and
683
+ * comes back is counted only up to the switch. The view still reports once,
684
+ * since the consumer derives a mean from a sum and a count: a second event for
685
+ * one visit would inflate the count and make every mean wrong. Hence
686
+ * `pageReported`: **at most one per page visit.** The next page starts a new
687
+ * one. *(Until that day, hidden was a pause and its time was subtracted.)*
654
688
  *
655
689
  * @private
656
690
  */
@@ -658,11 +692,7 @@ export default class Tracker {
658
692
  if (this.pageReported || this.pageEnteredAt === null || !this.currentPath) return
659
693
  if (!this.arms('time_on_page')) return
660
694
 
661
- const now = Date.now()
662
- // A tab hidden at this instant has not yet had its span folded in.
663
- const openHidden = this.hiddenSince === null ? 0 : now - this.hiddenSince
664
- const durationMs = now - this.pageEnteredAt - this.hiddenMs - openHidden
665
-
695
+ const durationMs = Date.now() - this.pageEnteredAt
666
696
  this.pageReported = true
667
697
  // Negative is not reachable by arithmetic, but a clock adjustment mid-visit
668
698
  // would produce one, and a negative duration poisons a sum the consumer
@@ -685,13 +715,12 @@ export default class Tracker {
685
715
  this.flush(true)
686
716
  })
687
717
  window.addEventListener('visibilitychange', () => {
718
+ // The last moment a phone promises: a page hidden may be discarded with no
719
+ // `pagehide`. So the view's time is queued first, as at `pagehide`, and
720
+ // everything goes by beacon.
688
721
  if (document.visibilityState === 'hidden') {
689
- // A pause, not an end — see `reportTimeOnPage`. Only the clock stops.
690
- if (this.hiddenSince === null) this.hiddenSince = Date.now()
722
+ this.reportTimeOnPage()
691
723
  this.flush(true)
692
- } else if (this.hiddenSince !== null) {
693
- this.hiddenMs += Date.now() - this.hiddenSince
694
- this.hiddenSince = null
695
724
  }
696
725
  })
697
726
  }