@joryio/web-sdk 1.3.2 → 1.3.3-beta.2

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.
@@ -1,15 +1,12 @@
1
1
  /**
2
- * Joryio Web SDK v1.3.2
2
+ * Joryio Web SDK v1.3.3-beta.2
3
3
  * (c) 2026 Joryio. All rights reserved.
4
4
  * @license Commercial - see https://github.com/joryio/joryio-web-sdk/blob/main/LICENSE
5
5
  */
6
- (function (global, factory) {
7
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
8
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
9
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JoryioSDK = {}));
10
- })(this, (function (exports) { 'use strict';
6
+ var JoryioSDK = (function (exports) {
7
+ 'use strict';
11
8
 
12
- const SDK_VERSION = '1.3.2';
9
+ const SDK_VERSION = '1.3.3';
13
10
  const DEFAULT_API_ENDPOINT = 'https://api-eu1.joryio.com/api';
14
11
  function normalizeApiEndpoint(raw) {
15
12
  const trimmed = (raw || '').trim();
@@ -2287,6 +2284,11 @@
2287
2284
  const MAX_DISPLAYED_MESSAGES = 500;
2288
2285
  const MAX_IMPRESSION_TIMESTAMPS = 500;
2289
2286
  const MAX_WATCHED_ATTRIBUTES = 200;
2287
+ const IN_APP_DISPLAY_DWELL_MS = 1000;
2288
+ const PAGE_SETTLE_FALLBACK_MS = 50;
2289
+ const PAGE_SETTLE_IDLE_TIMEOUT_MS = 500;
2290
+ const PAGE_LOAD_MAX_WAIT_MS = 10000;
2291
+ const USER_DISMISS_ACTIONS = new Set(['close_button', 'backdrop', 'button', 'bridge_close']);
2290
2292
  function scopeCssRules(rules, scope) {
2291
2293
  var _a, _b;
2292
2294
  const scopeSelector = (selectorText) => selectorText
@@ -2396,6 +2398,10 @@
2396
2398
  this.generation = 0;
2397
2399
  this.syncAgain = false;
2398
2400
  this.claimed = new Set();
2401
+ this.awaitingDwell = new Map();
2402
+ this.pageLeaving = false;
2403
+ this.settleWaiters = new Set();
2404
+ this.pageListeners = [];
2399
2405
  this.iframes = new Map();
2400
2406
  this.frameMaxWidths = new Map();
2401
2407
  this.allowHtmlJsInAppMessages = false;
@@ -2419,6 +2425,176 @@
2419
2425
  this.auth = auth || null;
2420
2426
  this.loadImpressionTimestamps();
2421
2427
  this.installHostBridge();
2428
+ this.installPageLifecycle();
2429
+ }
2430
+ pageHidden() {
2431
+ if (typeof document === 'undefined')
2432
+ return false;
2433
+ return document.visibilityState === 'hidden' || document.hidden === true;
2434
+ }
2435
+ installPageLifecycle() {
2436
+ if (typeof window === 'undefined' || typeof window.addEventListener !== 'function')
2437
+ return;
2438
+ const on = (target, type, fn) => {
2439
+ if (!target || typeof target.addEventListener !== 'function')
2440
+ return;
2441
+ target.addEventListener(type, fn);
2442
+ this.pageListeners.push([target, type, fn]);
2443
+ };
2444
+ const leaving = () => {
2445
+ this.pageLeaving = true;
2446
+ this.pauseDwells();
2447
+ this.settleWaiters.forEach((cancel) => cancel());
2448
+ };
2449
+ const stayed = () => {
2450
+ if (!this.pageLeaving)
2451
+ return;
2452
+ this.pageLeaving = false;
2453
+ this.resumeDwells();
2454
+ };
2455
+ on(window, 'beforeunload', leaving);
2456
+ on(window, 'pagehide', leaving);
2457
+ on(window, 'pageshow', () => {
2458
+ this.pageLeaving = false;
2459
+ this.resumeDwells();
2460
+ });
2461
+ on(window, 'pointerdown', stayed);
2462
+ on(window, 'keydown', stayed);
2463
+ if (typeof document !== 'undefined') {
2464
+ on(document, 'visibilitychange', () => {
2465
+ if (this.pageHidden()) {
2466
+ this.pauseDwells();
2467
+ }
2468
+ else {
2469
+ this.pageLeaving = false;
2470
+ this.resumeDwells();
2471
+ }
2472
+ });
2473
+ }
2474
+ }
2475
+ removePageLifecycle() {
2476
+ var _a;
2477
+ for (const [target, type, fn] of this.pageListeners) {
2478
+ try {
2479
+ (_a = target.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(target, type, fn);
2480
+ }
2481
+ catch (_b) {
2482
+ }
2483
+ }
2484
+ this.pageListeners = [];
2485
+ }
2486
+ whenPageSettled() {
2487
+ if (typeof window === 'undefined' || typeof document === 'undefined')
2488
+ return Promise.resolve(true);
2489
+ if (this.pageLeaving)
2490
+ return Promise.resolve(false);
2491
+ return new Promise((resolve) => {
2492
+ let done = false;
2493
+ let loadTimer;
2494
+ let idleTimer;
2495
+ const w = window;
2496
+ const finish = (ok) => {
2497
+ if (done)
2498
+ return;
2499
+ done = true;
2500
+ this.settleWaiters.delete(cancel);
2501
+ if (loadTimer)
2502
+ clearTimeout(loadTimer);
2503
+ if (idleTimer)
2504
+ clearTimeout(idleTimer);
2505
+ if (typeof w.removeEventListener === 'function')
2506
+ w.removeEventListener('load', onLoad);
2507
+ resolve(ok && !this.pageLeaving);
2508
+ };
2509
+ const cancel = () => finish(false);
2510
+ const idle = () => {
2511
+ if (done)
2512
+ return;
2513
+ if (typeof w.requestIdleCallback === 'function') {
2514
+ w.requestIdleCallback(() => finish(true), { timeout: PAGE_SETTLE_IDLE_TIMEOUT_MS });
2515
+ }
2516
+ else {
2517
+ idleTimer = setTimeout(() => finish(true), PAGE_SETTLE_FALLBACK_MS);
2518
+ }
2519
+ };
2520
+ const onLoad = () => {
2521
+ if (loadTimer)
2522
+ clearTimeout(loadTimer);
2523
+ loadTimer = undefined;
2524
+ idle();
2525
+ };
2526
+ this.settleWaiters.add(cancel);
2527
+ const state = document.readyState;
2528
+ if (!state || state === 'complete' || typeof w.addEventListener !== 'function') {
2529
+ idle();
2530
+ }
2531
+ else {
2532
+ w.addEventListener('load', onLoad, { once: true });
2533
+ loadTimer = setTimeout(onLoad, PAGE_LOAD_MAX_WAIT_MS);
2534
+ }
2535
+ });
2536
+ }
2537
+ isMounted(campaignId) {
2538
+ if (!this.displayedMessages.has(campaignId) || typeof document === 'undefined')
2539
+ return false;
2540
+ const el = document.getElementById(`joryio-inapp-${campaignId}`);
2541
+ return !!el && el.isConnected !== false;
2542
+ }
2543
+ scheduleDwell(campaignId, generation) {
2544
+ const existing = this.awaitingDwell.get(campaignId);
2545
+ if (existing === null || existing === void 0 ? void 0 : existing.timer)
2546
+ clearTimeout(existing.timer);
2547
+ if (this.pageLeaving || this.pageHidden()) {
2548
+ this.awaitingDwell.set(campaignId, { timer: null, generation });
2549
+ return;
2550
+ }
2551
+ const timer = setTimeout(() => {
2552
+ const entry = this.awaitingDwell.get(campaignId);
2553
+ if (!entry || entry.timer !== timer)
2554
+ return;
2555
+ entry.timer = null;
2556
+ if (this.pageLeaving || this.pageHidden() || !this.isMounted(campaignId))
2557
+ return;
2558
+ if (entry.generation !== this.generation)
2559
+ return;
2560
+ this.confirmDisplay(campaignId).catch(() => undefined);
2561
+ }, IN_APP_DISPLAY_DWELL_MS);
2562
+ this.awaitingDwell.set(campaignId, { timer, generation });
2563
+ }
2564
+ pauseDwells() {
2565
+ this.awaitingDwell.forEach((entry) => {
2566
+ if (entry.timer)
2567
+ clearTimeout(entry.timer);
2568
+ entry.timer = null;
2569
+ });
2570
+ }
2571
+ resumeDwells() {
2572
+ if (this.pageLeaving || this.pageHidden())
2573
+ return;
2574
+ this.awaitingDwell.forEach((entry, campaignId) => {
2575
+ if (!entry.timer && this.isMounted(campaignId))
2576
+ this.scheduleDwell(campaignId, entry.generation);
2577
+ });
2578
+ }
2579
+ dropDwell(campaignId) {
2580
+ const entry = this.awaitingDwell.get(campaignId);
2581
+ if (entry === null || entry === void 0 ? void 0 : entry.timer)
2582
+ clearTimeout(entry.timer);
2583
+ this.awaitingDwell.delete(campaignId);
2584
+ }
2585
+ confirmDisplay(campaignId) {
2586
+ if (!this.awaitingDwell.has(campaignId))
2587
+ return Promise.resolve();
2588
+ this.dropDwell(campaignId);
2589
+ const timestamps = this.impressionTimestamps.get(campaignId) || [];
2590
+ timestamps.push(Date.now());
2591
+ this.boundedMapSet(this.impressionTimestamps, campaignId, timestamps, MAX_IMPRESSION_TIMESTAMPS);
2592
+ this.persistImpressionTimestamps();
2593
+ const reported = this.trackImpression(campaignId, 'displayed');
2594
+ const message = this.displayedMessages.get(campaignId);
2595
+ if (message)
2596
+ this.callHost('onMessageDisplay', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDisplay) === null || _b === void 0 ? void 0 : _b.call(_a, message); });
2597
+ return reported;
2422
2598
  }
2423
2599
  loadImpressionTimestamps() {
2424
2600
  if (!this.storage)
@@ -2554,6 +2730,7 @@
2554
2730
  this.syncRetried = false;
2555
2731
  this.pendingDelays.forEach((timer) => clearTimeout(timer));
2556
2732
  this.pendingDelays.clear();
2733
+ this.settleWaiters.forEach((cancel) => cancel());
2557
2734
  }
2558
2735
  startPeriodicSync() {
2559
2736
  if (this.periodicSyncTimer) {
@@ -2807,37 +2984,44 @@
2807
2984
  `(permission not 'default'); suppressing.`);
2808
2985
  return;
2809
2986
  }
2810
- const message = {
2811
- campaign,
2812
- renderedHtml,
2813
- displayedAt: new Date(),
2814
- };
2815
2987
  const commitDisplay = () => {
2988
+ const message = {
2989
+ campaign,
2990
+ renderedHtml,
2991
+ displayedAt: new Date(),
2992
+ };
2816
2993
  this.boundedMapSet(this.displayedMessages, campaign.id, message, MAX_DISPLAYED_MESSAGES);
2817
- const timestamps = this.impressionTimestamps.get(campaign.id) || [];
2818
- timestamps.push(Date.now());
2819
- this.boundedMapSet(this.impressionTimestamps, campaign.id, timestamps, MAX_IMPRESSION_TIMESTAMPS);
2820
- this.persistImpressionTimestamps();
2821
2994
  this.display(message);
2995
+ this.scheduleDwell(campaign.id, generation);
2996
+ };
2997
+ const show = async () => {
2998
+ if (!(await this.whenPageSettled())) {
2999
+ this.logger.debug(`Campaign ${campaign.id} not displayed: the page is unloading. It stays eligible.`);
3000
+ return;
3001
+ }
3002
+ if (generation !== this.generation || !this.runtime.canSend()) {
3003
+ this.logger.debug(`Campaign ${campaign.id} not displayed: the visitor or consent changed`);
3004
+ return;
3005
+ }
3006
+ if (this.displayedMessages.has(campaign.id) || !this.checkFrequencyCap(campaign))
3007
+ return;
3008
+ commitDisplay();
2822
3009
  };
2823
3010
  const delayMs = Math.max(0, Number(campaign.delay) || 0) * 1000;
2824
3011
  if (delayMs > 0) {
2825
3012
  const timer = setTimeout(() => {
2826
3013
  this.pendingDelays.delete(campaign.id);
2827
- if (generation !== this.generation || !this.runtime.canSend()) {
2828
- this.logger.debug(`Campaign ${campaign.id} not displayed after its delay: the visitor or consent changed`);
3014
+ if (this.claimed.has(id) || this.displayedMessages.has(id))
2829
3015
  return;
2830
- }
2831
- if (this.displayedMessages.has(campaign.id) || !this.checkFrequencyCap(campaign))
2832
- return;
2833
- commitDisplay();
2834
- this.trackImpression(campaign.id, 'displayed').catch(() => undefined);
3016
+ this.claimed.add(id);
3017
+ show()
3018
+ .catch((error) => this.logger.error('In-app display failed:', error))
3019
+ .finally(() => this.claimed.delete(id));
2835
3020
  }, delayMs);
2836
3021
  this.pendingDelays.set(campaign.id, timer);
2837
3022
  return;
2838
3023
  }
2839
- commitDisplay();
2840
- await this.trackImpression(campaign.id, 'displayed');
3024
+ await show();
2841
3025
  }
2842
3026
  finally {
2843
3027
  this.claimed.delete(id);
@@ -3278,7 +3462,6 @@
3278
3462
  }
3279
3463
  }
3280
3464
  targetContainer.appendChild(container);
3281
- this.callHost('onMessageDisplay', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDisplay) === null || _b === void 0 ? void 0 : _b.call(_a, message); });
3282
3465
  this.logger.info(`Displayed campaign: ${campaign.name} (${campaign.id})`);
3283
3466
  }
3284
3467
  hostViewportWidth() {
@@ -3430,6 +3613,17 @@
3430
3613
  clearTimeout(pending);
3431
3614
  this.pendingDelays.delete(campaignId);
3432
3615
  }
3616
+ let report = true;
3617
+ const dwell = this.awaitingDwell.get(campaignId);
3618
+ if (dwell) {
3619
+ if (USER_DISMISS_ACTIONS.has(action) && dwell.generation === this.generation) {
3620
+ this.confirmDisplay(campaignId).catch(() => undefined);
3621
+ }
3622
+ else {
3623
+ this.dropDwell(campaignId);
3624
+ report = false;
3625
+ }
3626
+ }
3433
3627
  const container = document.getElementById(`joryio-inapp-${campaignId}`);
3434
3628
  const backdrop = document.getElementById(`joryio-backdrop-${campaignId}`);
3435
3629
  const style = document.getElementById(`joryio-style-${campaignId}`);
@@ -3451,7 +3645,8 @@
3451
3645
  }
3452
3646
  const message = this.displayedMessages.get(campaignId);
3453
3647
  if (message) {
3454
- await this.trackImpression(campaignId, 'dismissed').catch(() => undefined);
3648
+ if (report)
3649
+ await this.trackImpression(campaignId, 'dismissed').catch(() => undefined);
3455
3650
  this.callHost('onMessageDismiss', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDismiss) === null || _b === void 0 ? void 0 : _b.call(_a, message, action); });
3456
3651
  this.displayedMessages.delete(campaignId);
3457
3652
  }
@@ -3504,6 +3699,12 @@
3504
3699
  return;
3505
3700
  if (!this.runtime.canSend())
3506
3701
  return;
3702
+ const dwell = type !== 'displayed' ? this.awaitingDwell.get(campaignId) : undefined;
3703
+ if (dwell) {
3704
+ if (dwell.generation !== this.generation)
3705
+ return;
3706
+ this.confirmDisplay(campaignId).catch(() => undefined);
3707
+ }
3507
3708
  const sessionId = this.session.getSessionId();
3508
3709
  const data = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ campaignId, variantId: campaign.variantId }, this.subject()), { sessionId, deviceType: 'web', locale: navigator.language, userAgent: navigator.userAgent }), (campaign.pendingId ? { pendingId: campaign.pendingId } : {})), (campaign.deliveryToken ? { deliveryToken: campaign.deliveryToken } : {})), extra);
3509
3710
  if (type === 'displayed') {
@@ -3665,6 +3866,7 @@
3665
3866
  }
3666
3867
  this.campaigns.clear();
3667
3868
  this.displayedMessages.clear();
3869
+ this.awaitingDwell.forEach((_entry, campaignId) => this.dropDwell(campaignId));
3668
3870
  this.watchedAttributes.clear();
3669
3871
  this.iframes.clear();
3670
3872
  this.frameMaxWidths.clear();
@@ -3706,6 +3908,8 @@
3706
3908
  }
3707
3909
  this.campaigns.clear();
3708
3910
  this.displayedMessages.clear();
3911
+ this.awaitingDwell.forEach((_entry, campaignId) => this.dropDwell(campaignId));
3912
+ this.removePageLifecycle();
3709
3913
  this.impressionTimestamps.clear();
3710
3914
  this.watchedAttributes.clear();
3711
3915
  this.iframes.clear();
@@ -7324,6 +7528,7 @@
7324
7528
  }
7325
7529
 
7326
7530
  exports.EcommerceTracker = EcommerceTracker;
7531
+ exports.IN_APP_DISPLAY_DWELL_MS = IN_APP_DISPLAY_DWELL_MS;
7327
7532
  exports.JoryioSDK = JoryioSDK;
7328
7533
  exports.PushManager = PushManager;
7329
7534
  exports.SDK_VERSION = SDK_VERSION;
@@ -7334,4 +7539,20 @@
7334
7539
 
7335
7540
  Object.defineProperty(exports, '__esModule', { value: true });
7336
7541
 
7337
- }));
7542
+
7543
+ (function (root, ns) {
7544
+ if (typeof module === 'object' && module && typeof module.exports === 'object') { module.exports = ns; return; }
7545
+ if (root) { root.JoryioSDK = ns; }
7546
+ if (typeof define === 'function' && define.amd) {
7547
+ var script = typeof document !== 'undefined' ? document.currentScript : null;
7548
+ if (script && typeof script.getAttribute === 'function' && script.getAttribute('data-requiremodule')) {
7549
+ define(function () { return ns; });
7550
+ } else {
7551
+ define('joryio-web-sdk', [], function () { return ns; });
7552
+ }
7553
+ }
7554
+ })(typeof globalThis !== 'undefined' ? globalThis : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : null, exports);
7555
+
7556
+ return exports;
7557
+
7558
+ })({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joryio/web-sdk",
3
- "version": "1.3.2",
3
+ "version": "1.3.3-beta.2",
4
4
  "description": "Joryio Web Tracking SDK - Lightweight analytics and event tracking",
5
5
  "main": "dist/joryio.cjs.js",
6
6
  "module": "dist/joryio.mjs",
@@ -2,7 +2,7 @@
2
2
  * SDK Configuration Types and Defaults
3
3
  */
4
4
 
5
- export const SDK_VERSION = '1.3.2'; // keep in sync with package.json — the backend serves this in X-SDK-Version headers
5
+ export const SDK_VERSION = '1.3.3'; // keep in sync with package.json — the backend serves this in X-SDK-Version headers
6
6
  export const DEFAULT_API_ENDPOINT = 'https://api-eu1.joryio.com/api';
7
7
 
8
8
  /**