@hitkeep/tracker 2.12.0 → 2.13.1

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.cjs CHANGED
@@ -39,6 +39,7 @@ module.exports = __toCommonJS(package_exports);
39
39
  var SESSION_KEY = "hk_session";
40
40
  var SESSION_EXPIRY = 30 * 60 * 1e3;
41
41
  var MAX_PENDING_REQUESTS = 10;
42
+ var MAX_PENDING_ACTIVATION_REQUESTS = 32;
42
43
  var RETRY_DELAY_MS = 2e3;
43
44
  var DUPLICATE_PAGEVIEW_WINDOW_MS = 1500;
44
45
  var OPT_OUT_KEY = "hk_ignore";
@@ -225,6 +226,7 @@ function classifyFormSubmit(form, currentUrl, submitter) {
225
226
  }
226
227
  function createTracker(win, config) {
227
228
  const { document, location, navigator, screen, history, sessionStorage, crypto } = win;
229
+ const prerenderDocument = document;
228
230
  win.hk = win.hk || {};
229
231
  if (win.hk._bootstrapped) {
230
232
  return null;
@@ -269,6 +271,7 @@ function createTracker(win, config) {
269
271
  const sessionId = getSessionId();
270
272
  const initialReferrer = document.referrer;
271
273
  const initialHost = location.hostname;
274
+ let awaitingActivation = prerenderDocument.prerendering === true || document.visibilityState === "prerender";
272
275
  const readUtmValue = (params, key) => {
273
276
  const value = params.get(key);
274
277
  if (!value) {
@@ -277,16 +280,20 @@ function createTracker(win, config) {
277
280
  const trimmed = value.trim();
278
281
  return trimmed.length > 0 ? trimmed : null;
279
282
  };
280
- const initialSearchParams = new URLSearchParams(location.search);
281
- const initialAttribution = {
282
- u_src: readUtmValue(initialSearchParams, "utm_source"),
283
- u_med: readUtmValue(initialSearchParams, "utm_medium"),
284
- u_cmp: readUtmValue(initialSearchParams, "utm_campaign"),
285
- u_trm: readUtmValue(initialSearchParams, "utm_term"),
286
- u_cnt: readUtmValue(initialSearchParams, "utm_content"),
287
- qr: readUtmValue(initialSearchParams, "hk_qr")
283
+ const readAttribution = () => {
284
+ const params = new URLSearchParams(location.search);
285
+ return {
286
+ u_src: readUtmValue(params, "utm_source"),
287
+ u_med: readUtmValue(params, "utm_medium"),
288
+ u_cmp: readUtmValue(params, "utm_campaign"),
289
+ u_trm: readUtmValue(params, "utm_term"),
290
+ u_cnt: readUtmValue(params, "utm_content"),
291
+ qr: readUtmValue(params, "hk_qr")
292
+ };
288
293
  };
294
+ let initialAttribution = awaitingActivation ? null : readAttribution();
289
295
  const pendingRequests = [];
296
+ const pendingActivationRequests = [];
290
297
  const removers = [];
291
298
  const on = (target, type, listener, options) => {
292
299
  target.addEventListener(type, listener, options);
@@ -298,6 +305,13 @@ function createTracker(win, config) {
298
305
  let lastPageviewPath = "";
299
306
  let lastPageviewAt = 0;
300
307
  let currentPageId = generateUUID();
308
+ let pendingPageview = false;
309
+ const queueActivationRequest = (request) => {
310
+ pendingActivationRequests.push(request);
311
+ if (pendingActivationRequests.length > MAX_PENDING_ACTIVATION_REQUESTS) {
312
+ pendingActivationRequests.splice(0, pendingActivationRequests.length - MAX_PENDING_ACTIVATION_REQUESTS);
313
+ }
314
+ };
301
315
  const queueRequest = (request) => {
302
316
  pendingRequests.push(request);
303
317
  if (pendingRequests.length > MAX_PENDING_REQUESTS) {
@@ -374,7 +388,7 @@ function createTracker(win, config) {
374
388
  return initialReferrer || null;
375
389
  };
376
390
  const emitEvent = (name, properties = {}) => {
377
- sendJson(config.eventEndpoint, {
391
+ const payload = {
378
392
  n: name,
379
393
  p: properties,
380
394
  r: currentReferrer(),
@@ -383,11 +397,22 @@ function createTracker(win, config) {
383
397
  ua: navigator.userAgent,
384
398
  tsrc: config.trackerSource,
385
399
  tv: config.trackerVersion
386
- });
400
+ };
401
+ if (awaitingActivation) {
402
+ queueActivationRequest({ kind: "event", endpoint: config.eventEndpoint, payload: JSON.parse(JSON.stringify(payload)) });
403
+ return;
404
+ }
405
+ sendJson(config.eventEndpoint, payload);
387
406
  };
388
407
  if (config.webVitalsBundleUrl) {
389
408
  win.hk._webVitals = {
390
- emit: (payload) => sendJson(config.webVitalsEndpoint, payload),
409
+ emit: (payload) => {
410
+ if (awaitingActivation) {
411
+ queueActivationRequest({ kind: "web-vital", endpoint: config.webVitalsEndpoint, payload: { ...payload } });
412
+ return;
413
+ }
414
+ sendJson(config.webVitalsEndpoint, payload);
415
+ },
391
416
  getPath: () => location.pathname || "/",
392
417
  sessionId,
393
418
  pageId: () => currentPageId,
@@ -397,7 +422,7 @@ function createTracker(win, config) {
397
422
  };
398
423
  loadWebVitalsBundle(win, config.webVitalsBundleUrl);
399
424
  }
400
- const sendPageView = () => {
425
+ const sendPageViewNow = () => {
401
426
  const currentPath = location.pathname;
402
427
  const now = Date.now();
403
428
  currentPageId = generateUUID();
@@ -412,6 +437,9 @@ function createTracker(win, config) {
412
437
  }
413
438
  const referrer = currentReferrer();
414
439
  const isUnique = lastPath === currentPath && referrer ? new URL(referrer, location.href).hostname !== initialHost : false;
440
+ if (initialAttribution === null) {
441
+ initialAttribution = readAttribution();
442
+ }
415
443
  sendJson(config.pageEndpoint, {
416
444
  path: currentPath,
417
445
  referrer: referrer || null,
@@ -432,6 +460,58 @@ function createTracker(win, config) {
432
460
  lastPageviewAt = now;
433
461
  lastPath = currentPath;
434
462
  };
463
+ const sendPageView = () => {
464
+ if (awaitingActivation) {
465
+ pendingPageview = true;
466
+ return;
467
+ }
468
+ sendPageViewNow();
469
+ };
470
+ const activate = () => {
471
+ if (!awaitingActivation) {
472
+ return;
473
+ }
474
+ awaitingActivation = false;
475
+ initialAttribution = readAttribution();
476
+ lastPath = location.pathname;
477
+ if (pendingPageview) {
478
+ pendingPageview = false;
479
+ sendPageViewNow();
480
+ }
481
+ const requests = pendingActivationRequests.splice(0, pendingActivationRequests.length);
482
+ for (const request of requests) {
483
+ if (request.kind === "event") {
484
+ sendJson(request.endpoint, {
485
+ ...request.payload,
486
+ r: currentReferrer(),
487
+ path: location.pathname || "/"
488
+ });
489
+ continue;
490
+ }
491
+ sendJson(request.endpoint, {
492
+ ...request.payload,
493
+ p: location.pathname || "/",
494
+ pid: currentPageId
495
+ });
496
+ }
497
+ };
498
+ if (prerenderDocument.prerendering === true) {
499
+ const handlePrerenderActivation = () => {
500
+ if (!awaitingActivation || prerenderDocument.prerendering === true) {
501
+ return;
502
+ }
503
+ activate();
504
+ };
505
+ on(document, "prerenderingchange", handlePrerenderActivation, { once: true });
506
+ } else if (document.visibilityState === "prerender") {
507
+ const handleLegacyPrerenderActivation = () => {
508
+ if (!awaitingActivation || document.visibilityState !== "visible") {
509
+ return;
510
+ }
511
+ activate();
512
+ };
513
+ on(document, "visibilitychange", handleLegacyPrerenderActivation);
514
+ }
435
515
  const patchHistoryMethod = (method) => {
436
516
  const original = history[method];
437
517
  const patched = function patchedHistory(...args) {
@@ -460,16 +540,7 @@ function createTracker(win, config) {
460
540
  on(win, "pagehide", flushQueue);
461
541
  on(document, "visibilitychange", handleVisibilityFlush);
462
542
  if (config.capturePageviews) {
463
- if (document.visibilityState === "prerender") {
464
- const handlePrerenderVisible = () => {
465
- if (document.visibilityState === "visible") {
466
- sendPageView();
467
- }
468
- };
469
- on(document, "visibilitychange", handlePrerenderVisible, { once: true });
470
- } else {
471
- sendPageView();
472
- }
543
+ sendPageView();
473
544
  }
474
545
  bindAutoTracking(document, () => new URL(location.href), config, emitEvent, on);
475
546
  const track2 = (name, properties) => emitEvent(name, properties ?? {});
@@ -481,6 +552,8 @@ function createTracker(win, config) {
481
552
  return;
482
553
  }
483
554
  active = false;
555
+ pendingPageview = false;
556
+ pendingActivationRequests.length = 0;
484
557
  if (retryTimer !== null) {
485
558
  clearTimeout(retryTimer);
486
559
  retryTimer = null;
@@ -548,7 +621,7 @@ function bindAutoTracking(document, getCurrentUrl, options, emitEvent, on) {
548
621
  }
549
622
 
550
623
  // src/tracker/version.ts
551
- var TRACKER_VERSION = "2.12.0";
624
+ var TRACKER_VERSION = "2.13.1";
552
625
 
553
626
  // src/tracker/package.ts
554
627
  var MAX_PRE_INIT_EVENTS = 32;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  var SESSION_KEY = "hk_session";
3
3
  var SESSION_EXPIRY = 30 * 60 * 1e3;
4
4
  var MAX_PENDING_REQUESTS = 10;
5
+ var MAX_PENDING_ACTIVATION_REQUESTS = 32;
5
6
  var RETRY_DELAY_MS = 2e3;
6
7
  var DUPLICATE_PAGEVIEW_WINDOW_MS = 1500;
7
8
  var OPT_OUT_KEY = "hk_ignore";
@@ -188,6 +189,7 @@ function classifyFormSubmit(form, currentUrl, submitter) {
188
189
  }
189
190
  function createTracker(win, config) {
190
191
  const { document, location, navigator, screen, history, sessionStorage, crypto } = win;
192
+ const prerenderDocument = document;
191
193
  win.hk = win.hk || {};
192
194
  if (win.hk._bootstrapped) {
193
195
  return null;
@@ -232,6 +234,7 @@ function createTracker(win, config) {
232
234
  const sessionId = getSessionId();
233
235
  const initialReferrer = document.referrer;
234
236
  const initialHost = location.hostname;
237
+ let awaitingActivation = prerenderDocument.prerendering === true || document.visibilityState === "prerender";
235
238
  const readUtmValue = (params, key) => {
236
239
  const value = params.get(key);
237
240
  if (!value) {
@@ -240,16 +243,20 @@ function createTracker(win, config) {
240
243
  const trimmed = value.trim();
241
244
  return trimmed.length > 0 ? trimmed : null;
242
245
  };
243
- const initialSearchParams = new URLSearchParams(location.search);
244
- const initialAttribution = {
245
- u_src: readUtmValue(initialSearchParams, "utm_source"),
246
- u_med: readUtmValue(initialSearchParams, "utm_medium"),
247
- u_cmp: readUtmValue(initialSearchParams, "utm_campaign"),
248
- u_trm: readUtmValue(initialSearchParams, "utm_term"),
249
- u_cnt: readUtmValue(initialSearchParams, "utm_content"),
250
- qr: readUtmValue(initialSearchParams, "hk_qr")
246
+ const readAttribution = () => {
247
+ const params = new URLSearchParams(location.search);
248
+ return {
249
+ u_src: readUtmValue(params, "utm_source"),
250
+ u_med: readUtmValue(params, "utm_medium"),
251
+ u_cmp: readUtmValue(params, "utm_campaign"),
252
+ u_trm: readUtmValue(params, "utm_term"),
253
+ u_cnt: readUtmValue(params, "utm_content"),
254
+ qr: readUtmValue(params, "hk_qr")
255
+ };
251
256
  };
257
+ let initialAttribution = awaitingActivation ? null : readAttribution();
252
258
  const pendingRequests = [];
259
+ const pendingActivationRequests = [];
253
260
  const removers = [];
254
261
  const on = (target, type, listener, options) => {
255
262
  target.addEventListener(type, listener, options);
@@ -261,6 +268,13 @@ function createTracker(win, config) {
261
268
  let lastPageviewPath = "";
262
269
  let lastPageviewAt = 0;
263
270
  let currentPageId = generateUUID();
271
+ let pendingPageview = false;
272
+ const queueActivationRequest = (request) => {
273
+ pendingActivationRequests.push(request);
274
+ if (pendingActivationRequests.length > MAX_PENDING_ACTIVATION_REQUESTS) {
275
+ pendingActivationRequests.splice(0, pendingActivationRequests.length - MAX_PENDING_ACTIVATION_REQUESTS);
276
+ }
277
+ };
264
278
  const queueRequest = (request) => {
265
279
  pendingRequests.push(request);
266
280
  if (pendingRequests.length > MAX_PENDING_REQUESTS) {
@@ -337,7 +351,7 @@ function createTracker(win, config) {
337
351
  return initialReferrer || null;
338
352
  };
339
353
  const emitEvent = (name, properties = {}) => {
340
- sendJson(config.eventEndpoint, {
354
+ const payload = {
341
355
  n: name,
342
356
  p: properties,
343
357
  r: currentReferrer(),
@@ -346,11 +360,22 @@ function createTracker(win, config) {
346
360
  ua: navigator.userAgent,
347
361
  tsrc: config.trackerSource,
348
362
  tv: config.trackerVersion
349
- });
363
+ };
364
+ if (awaitingActivation) {
365
+ queueActivationRequest({ kind: "event", endpoint: config.eventEndpoint, payload: JSON.parse(JSON.stringify(payload)) });
366
+ return;
367
+ }
368
+ sendJson(config.eventEndpoint, payload);
350
369
  };
351
370
  if (config.webVitalsBundleUrl) {
352
371
  win.hk._webVitals = {
353
- emit: (payload) => sendJson(config.webVitalsEndpoint, payload),
372
+ emit: (payload) => {
373
+ if (awaitingActivation) {
374
+ queueActivationRequest({ kind: "web-vital", endpoint: config.webVitalsEndpoint, payload: { ...payload } });
375
+ return;
376
+ }
377
+ sendJson(config.webVitalsEndpoint, payload);
378
+ },
354
379
  getPath: () => location.pathname || "/",
355
380
  sessionId,
356
381
  pageId: () => currentPageId,
@@ -360,7 +385,7 @@ function createTracker(win, config) {
360
385
  };
361
386
  loadWebVitalsBundle(win, config.webVitalsBundleUrl);
362
387
  }
363
- const sendPageView = () => {
388
+ const sendPageViewNow = () => {
364
389
  const currentPath = location.pathname;
365
390
  const now = Date.now();
366
391
  currentPageId = generateUUID();
@@ -375,6 +400,9 @@ function createTracker(win, config) {
375
400
  }
376
401
  const referrer = currentReferrer();
377
402
  const isUnique = lastPath === currentPath && referrer ? new URL(referrer, location.href).hostname !== initialHost : false;
403
+ if (initialAttribution === null) {
404
+ initialAttribution = readAttribution();
405
+ }
378
406
  sendJson(config.pageEndpoint, {
379
407
  path: currentPath,
380
408
  referrer: referrer || null,
@@ -395,6 +423,58 @@ function createTracker(win, config) {
395
423
  lastPageviewAt = now;
396
424
  lastPath = currentPath;
397
425
  };
426
+ const sendPageView = () => {
427
+ if (awaitingActivation) {
428
+ pendingPageview = true;
429
+ return;
430
+ }
431
+ sendPageViewNow();
432
+ };
433
+ const activate = () => {
434
+ if (!awaitingActivation) {
435
+ return;
436
+ }
437
+ awaitingActivation = false;
438
+ initialAttribution = readAttribution();
439
+ lastPath = location.pathname;
440
+ if (pendingPageview) {
441
+ pendingPageview = false;
442
+ sendPageViewNow();
443
+ }
444
+ const requests = pendingActivationRequests.splice(0, pendingActivationRequests.length);
445
+ for (const request of requests) {
446
+ if (request.kind === "event") {
447
+ sendJson(request.endpoint, {
448
+ ...request.payload,
449
+ r: currentReferrer(),
450
+ path: location.pathname || "/"
451
+ });
452
+ continue;
453
+ }
454
+ sendJson(request.endpoint, {
455
+ ...request.payload,
456
+ p: location.pathname || "/",
457
+ pid: currentPageId
458
+ });
459
+ }
460
+ };
461
+ if (prerenderDocument.prerendering === true) {
462
+ const handlePrerenderActivation = () => {
463
+ if (!awaitingActivation || prerenderDocument.prerendering === true) {
464
+ return;
465
+ }
466
+ activate();
467
+ };
468
+ on(document, "prerenderingchange", handlePrerenderActivation, { once: true });
469
+ } else if (document.visibilityState === "prerender") {
470
+ const handleLegacyPrerenderActivation = () => {
471
+ if (!awaitingActivation || document.visibilityState !== "visible") {
472
+ return;
473
+ }
474
+ activate();
475
+ };
476
+ on(document, "visibilitychange", handleLegacyPrerenderActivation);
477
+ }
398
478
  const patchHistoryMethod = (method) => {
399
479
  const original = history[method];
400
480
  const patched = function patchedHistory(...args) {
@@ -423,16 +503,7 @@ function createTracker(win, config) {
423
503
  on(win, "pagehide", flushQueue);
424
504
  on(document, "visibilitychange", handleVisibilityFlush);
425
505
  if (config.capturePageviews) {
426
- if (document.visibilityState === "prerender") {
427
- const handlePrerenderVisible = () => {
428
- if (document.visibilityState === "visible") {
429
- sendPageView();
430
- }
431
- };
432
- on(document, "visibilitychange", handlePrerenderVisible, { once: true });
433
- } else {
434
- sendPageView();
435
- }
506
+ sendPageView();
436
507
  }
437
508
  bindAutoTracking(document, () => new URL(location.href), config, emitEvent, on);
438
509
  const track2 = (name, properties) => emitEvent(name, properties ?? {});
@@ -444,6 +515,8 @@ function createTracker(win, config) {
444
515
  return;
445
516
  }
446
517
  active = false;
518
+ pendingPageview = false;
519
+ pendingActivationRequests.length = 0;
447
520
  if (retryTimer !== null) {
448
521
  clearTimeout(retryTimer);
449
522
  retryTimer = null;
@@ -511,7 +584,7 @@ function bindAutoTracking(document, getCurrentUrl, options, emitEvent, on) {
511
584
  }
512
585
 
513
586
  // src/tracker/version.ts
514
- var TRACKER_VERSION = "2.12.0";
587
+ var TRACKER_VERSION = "2.13.1";
515
588
 
516
589
  // src/tracker/package.ts
517
590
  var MAX_PRE_INIT_EVENTS = 32;
@@ -1 +1 @@
1
- export declare const TRACKER_VERSION = "2.12.0";
1
+ export declare const TRACKER_VERSION = "2.13.1";
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const TRACKER_VERSION = "2.12.0";
1
+ export declare const TRACKER_VERSION = "2.13.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hitkeep/tracker",
3
- "version": "2.12.0",
3
+ "version": "2.13.1",
4
4
  "description": "Typed, cookieless web analytics tracker for React, Vue, Angular, and Astro. Official browser SDK for HitKeep, a privacy-friendly, self-hostable Google Analytics alternative.",
5
5
  "license": "MIT",
6
6
  "type": "module",