@joryio/web-sdk 1.3.2 → 1.3.3-beta.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/joryio.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Joryio Web SDK v1.3.2
2
+ * Joryio Web SDK v1.3.3-beta.1
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
- const SDK_VERSION = '1.3.2';
6
+ const SDK_VERSION = '1.3.3';
7
7
  const DEFAULT_API_ENDPOINT = 'https://api-eu1.joryio.com/api';
8
8
  function normalizeApiEndpoint(raw) {
9
9
  const trimmed = (raw || '').trim();
@@ -2281,6 +2281,11 @@ const IMPRESSIONS_STORAGE_KEY = 'inapp_impressions';
2281
2281
  const MAX_DISPLAYED_MESSAGES = 500;
2282
2282
  const MAX_IMPRESSION_TIMESTAMPS = 500;
2283
2283
  const MAX_WATCHED_ATTRIBUTES = 200;
2284
+ const IN_APP_DISPLAY_DWELL_MS = 1000;
2285
+ const PAGE_SETTLE_FALLBACK_MS = 50;
2286
+ const PAGE_SETTLE_IDLE_TIMEOUT_MS = 500;
2287
+ const PAGE_LOAD_MAX_WAIT_MS = 10000;
2288
+ const USER_DISMISS_ACTIONS = new Set(['close_button', 'backdrop', 'button', 'bridge_close']);
2284
2289
  function scopeCssRules(rules, scope) {
2285
2290
  var _a, _b;
2286
2291
  const scopeSelector = (selectorText) => selectorText
@@ -2390,6 +2395,10 @@ class InAppMessagingManager {
2390
2395
  this.generation = 0;
2391
2396
  this.syncAgain = false;
2392
2397
  this.claimed = new Set();
2398
+ this.awaitingDwell = new Map();
2399
+ this.pageLeaving = false;
2400
+ this.settleWaiters = new Set();
2401
+ this.pageListeners = [];
2393
2402
  this.iframes = new Map();
2394
2403
  this.frameMaxWidths = new Map();
2395
2404
  this.allowHtmlJsInAppMessages = false;
@@ -2413,6 +2422,176 @@ class InAppMessagingManager {
2413
2422
  this.auth = auth || null;
2414
2423
  this.loadImpressionTimestamps();
2415
2424
  this.installHostBridge();
2425
+ this.installPageLifecycle();
2426
+ }
2427
+ pageHidden() {
2428
+ if (typeof document === 'undefined')
2429
+ return false;
2430
+ return document.visibilityState === 'hidden' || document.hidden === true;
2431
+ }
2432
+ installPageLifecycle() {
2433
+ if (typeof window === 'undefined' || typeof window.addEventListener !== 'function')
2434
+ return;
2435
+ const on = (target, type, fn) => {
2436
+ if (!target || typeof target.addEventListener !== 'function')
2437
+ return;
2438
+ target.addEventListener(type, fn);
2439
+ this.pageListeners.push([target, type, fn]);
2440
+ };
2441
+ const leaving = () => {
2442
+ this.pageLeaving = true;
2443
+ this.pauseDwells();
2444
+ this.settleWaiters.forEach((cancel) => cancel());
2445
+ };
2446
+ const stayed = () => {
2447
+ if (!this.pageLeaving)
2448
+ return;
2449
+ this.pageLeaving = false;
2450
+ this.resumeDwells();
2451
+ };
2452
+ on(window, 'beforeunload', leaving);
2453
+ on(window, 'pagehide', leaving);
2454
+ on(window, 'pageshow', () => {
2455
+ this.pageLeaving = false;
2456
+ this.resumeDwells();
2457
+ });
2458
+ on(window, 'pointerdown', stayed);
2459
+ on(window, 'keydown', stayed);
2460
+ if (typeof document !== 'undefined') {
2461
+ on(document, 'visibilitychange', () => {
2462
+ if (this.pageHidden()) {
2463
+ this.pauseDwells();
2464
+ }
2465
+ else {
2466
+ this.pageLeaving = false;
2467
+ this.resumeDwells();
2468
+ }
2469
+ });
2470
+ }
2471
+ }
2472
+ removePageLifecycle() {
2473
+ var _a;
2474
+ for (const [target, type, fn] of this.pageListeners) {
2475
+ try {
2476
+ (_a = target.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(target, type, fn);
2477
+ }
2478
+ catch (_b) {
2479
+ }
2480
+ }
2481
+ this.pageListeners = [];
2482
+ }
2483
+ whenPageSettled() {
2484
+ if (typeof window === 'undefined' || typeof document === 'undefined')
2485
+ return Promise.resolve(true);
2486
+ if (this.pageLeaving)
2487
+ return Promise.resolve(false);
2488
+ return new Promise((resolve) => {
2489
+ let done = false;
2490
+ let loadTimer;
2491
+ let idleTimer;
2492
+ const w = window;
2493
+ const finish = (ok) => {
2494
+ if (done)
2495
+ return;
2496
+ done = true;
2497
+ this.settleWaiters.delete(cancel);
2498
+ if (loadTimer)
2499
+ clearTimeout(loadTimer);
2500
+ if (idleTimer)
2501
+ clearTimeout(idleTimer);
2502
+ if (typeof w.removeEventListener === 'function')
2503
+ w.removeEventListener('load', onLoad);
2504
+ resolve(ok && !this.pageLeaving);
2505
+ };
2506
+ const cancel = () => finish(false);
2507
+ const idle = () => {
2508
+ if (done)
2509
+ return;
2510
+ if (typeof w.requestIdleCallback === 'function') {
2511
+ w.requestIdleCallback(() => finish(true), { timeout: PAGE_SETTLE_IDLE_TIMEOUT_MS });
2512
+ }
2513
+ else {
2514
+ idleTimer = setTimeout(() => finish(true), PAGE_SETTLE_FALLBACK_MS);
2515
+ }
2516
+ };
2517
+ const onLoad = () => {
2518
+ if (loadTimer)
2519
+ clearTimeout(loadTimer);
2520
+ loadTimer = undefined;
2521
+ idle();
2522
+ };
2523
+ this.settleWaiters.add(cancel);
2524
+ const state = document.readyState;
2525
+ if (!state || state === 'complete' || typeof w.addEventListener !== 'function') {
2526
+ idle();
2527
+ }
2528
+ else {
2529
+ w.addEventListener('load', onLoad, { once: true });
2530
+ loadTimer = setTimeout(onLoad, PAGE_LOAD_MAX_WAIT_MS);
2531
+ }
2532
+ });
2533
+ }
2534
+ isMounted(campaignId) {
2535
+ if (!this.displayedMessages.has(campaignId) || typeof document === 'undefined')
2536
+ return false;
2537
+ const el = document.getElementById(`joryio-inapp-${campaignId}`);
2538
+ return !!el && el.isConnected !== false;
2539
+ }
2540
+ scheduleDwell(campaignId, generation) {
2541
+ const existing = this.awaitingDwell.get(campaignId);
2542
+ if (existing === null || existing === void 0 ? void 0 : existing.timer)
2543
+ clearTimeout(existing.timer);
2544
+ if (this.pageLeaving || this.pageHidden()) {
2545
+ this.awaitingDwell.set(campaignId, { timer: null, generation });
2546
+ return;
2547
+ }
2548
+ const timer = setTimeout(() => {
2549
+ const entry = this.awaitingDwell.get(campaignId);
2550
+ if (!entry || entry.timer !== timer)
2551
+ return;
2552
+ entry.timer = null;
2553
+ if (this.pageLeaving || this.pageHidden() || !this.isMounted(campaignId))
2554
+ return;
2555
+ if (entry.generation !== this.generation)
2556
+ return;
2557
+ this.confirmDisplay(campaignId).catch(() => undefined);
2558
+ }, IN_APP_DISPLAY_DWELL_MS);
2559
+ this.awaitingDwell.set(campaignId, { timer, generation });
2560
+ }
2561
+ pauseDwells() {
2562
+ this.awaitingDwell.forEach((entry) => {
2563
+ if (entry.timer)
2564
+ clearTimeout(entry.timer);
2565
+ entry.timer = null;
2566
+ });
2567
+ }
2568
+ resumeDwells() {
2569
+ if (this.pageLeaving || this.pageHidden())
2570
+ return;
2571
+ this.awaitingDwell.forEach((entry, campaignId) => {
2572
+ if (!entry.timer && this.isMounted(campaignId))
2573
+ this.scheduleDwell(campaignId, entry.generation);
2574
+ });
2575
+ }
2576
+ dropDwell(campaignId) {
2577
+ const entry = this.awaitingDwell.get(campaignId);
2578
+ if (entry === null || entry === void 0 ? void 0 : entry.timer)
2579
+ clearTimeout(entry.timer);
2580
+ this.awaitingDwell.delete(campaignId);
2581
+ }
2582
+ confirmDisplay(campaignId) {
2583
+ if (!this.awaitingDwell.has(campaignId))
2584
+ return Promise.resolve();
2585
+ this.dropDwell(campaignId);
2586
+ const timestamps = this.impressionTimestamps.get(campaignId) || [];
2587
+ timestamps.push(Date.now());
2588
+ this.boundedMapSet(this.impressionTimestamps, campaignId, timestamps, MAX_IMPRESSION_TIMESTAMPS);
2589
+ this.persistImpressionTimestamps();
2590
+ const reported = this.trackImpression(campaignId, 'displayed');
2591
+ const message = this.displayedMessages.get(campaignId);
2592
+ if (message)
2593
+ this.callHost('onMessageDisplay', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDisplay) === null || _b === void 0 ? void 0 : _b.call(_a, message); });
2594
+ return reported;
2416
2595
  }
2417
2596
  loadImpressionTimestamps() {
2418
2597
  if (!this.storage)
@@ -2548,6 +2727,7 @@ class InAppMessagingManager {
2548
2727
  this.syncRetried = false;
2549
2728
  this.pendingDelays.forEach((timer) => clearTimeout(timer));
2550
2729
  this.pendingDelays.clear();
2730
+ this.settleWaiters.forEach((cancel) => cancel());
2551
2731
  }
2552
2732
  startPeriodicSync() {
2553
2733
  if (this.periodicSyncTimer) {
@@ -2801,37 +2981,44 @@ class InAppMessagingManager {
2801
2981
  `(permission not 'default'); suppressing.`);
2802
2982
  return;
2803
2983
  }
2804
- const message = {
2805
- campaign,
2806
- renderedHtml,
2807
- displayedAt: new Date(),
2808
- };
2809
2984
  const commitDisplay = () => {
2985
+ const message = {
2986
+ campaign,
2987
+ renderedHtml,
2988
+ displayedAt: new Date(),
2989
+ };
2810
2990
  this.boundedMapSet(this.displayedMessages, campaign.id, message, MAX_DISPLAYED_MESSAGES);
2811
- const timestamps = this.impressionTimestamps.get(campaign.id) || [];
2812
- timestamps.push(Date.now());
2813
- this.boundedMapSet(this.impressionTimestamps, campaign.id, timestamps, MAX_IMPRESSION_TIMESTAMPS);
2814
- this.persistImpressionTimestamps();
2815
2991
  this.display(message);
2992
+ this.scheduleDwell(campaign.id, generation);
2993
+ };
2994
+ const show = async () => {
2995
+ if (!(await this.whenPageSettled())) {
2996
+ this.logger.debug(`Campaign ${campaign.id} not displayed: the page is unloading. It stays eligible.`);
2997
+ return;
2998
+ }
2999
+ if (generation !== this.generation || !this.runtime.canSend()) {
3000
+ this.logger.debug(`Campaign ${campaign.id} not displayed: the visitor or consent changed`);
3001
+ return;
3002
+ }
3003
+ if (this.displayedMessages.has(campaign.id) || !this.checkFrequencyCap(campaign))
3004
+ return;
3005
+ commitDisplay();
2816
3006
  };
2817
3007
  const delayMs = Math.max(0, Number(campaign.delay) || 0) * 1000;
2818
3008
  if (delayMs > 0) {
2819
3009
  const timer = setTimeout(() => {
2820
3010
  this.pendingDelays.delete(campaign.id);
2821
- if (generation !== this.generation || !this.runtime.canSend()) {
2822
- this.logger.debug(`Campaign ${campaign.id} not displayed after its delay: the visitor or consent changed`);
3011
+ if (this.claimed.has(id) || this.displayedMessages.has(id))
2823
3012
  return;
2824
- }
2825
- if (this.displayedMessages.has(campaign.id) || !this.checkFrequencyCap(campaign))
2826
- return;
2827
- commitDisplay();
2828
- this.trackImpression(campaign.id, 'displayed').catch(() => undefined);
3013
+ this.claimed.add(id);
3014
+ show()
3015
+ .catch((error) => this.logger.error('In-app display failed:', error))
3016
+ .finally(() => this.claimed.delete(id));
2829
3017
  }, delayMs);
2830
3018
  this.pendingDelays.set(campaign.id, timer);
2831
3019
  return;
2832
3020
  }
2833
- commitDisplay();
2834
- await this.trackImpression(campaign.id, 'displayed');
3021
+ await show();
2835
3022
  }
2836
3023
  finally {
2837
3024
  this.claimed.delete(id);
@@ -3272,7 +3459,6 @@ class InAppMessagingManager {
3272
3459
  }
3273
3460
  }
3274
3461
  targetContainer.appendChild(container);
3275
- this.callHost('onMessageDisplay', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDisplay) === null || _b === void 0 ? void 0 : _b.call(_a, message); });
3276
3462
  this.logger.info(`Displayed campaign: ${campaign.name} (${campaign.id})`);
3277
3463
  }
3278
3464
  hostViewportWidth() {
@@ -3424,6 +3610,17 @@ class InAppMessagingManager {
3424
3610
  clearTimeout(pending);
3425
3611
  this.pendingDelays.delete(campaignId);
3426
3612
  }
3613
+ let report = true;
3614
+ const dwell = this.awaitingDwell.get(campaignId);
3615
+ if (dwell) {
3616
+ if (USER_DISMISS_ACTIONS.has(action) && dwell.generation === this.generation) {
3617
+ this.confirmDisplay(campaignId).catch(() => undefined);
3618
+ }
3619
+ else {
3620
+ this.dropDwell(campaignId);
3621
+ report = false;
3622
+ }
3623
+ }
3427
3624
  const container = document.getElementById(`joryio-inapp-${campaignId}`);
3428
3625
  const backdrop = document.getElementById(`joryio-backdrop-${campaignId}`);
3429
3626
  const style = document.getElementById(`joryio-style-${campaignId}`);
@@ -3445,7 +3642,8 @@ class InAppMessagingManager {
3445
3642
  }
3446
3643
  const message = this.displayedMessages.get(campaignId);
3447
3644
  if (message) {
3448
- await this.trackImpression(campaignId, 'dismissed').catch(() => undefined);
3645
+ if (report)
3646
+ await this.trackImpression(campaignId, 'dismissed').catch(() => undefined);
3449
3647
  this.callHost('onMessageDismiss', () => { var _a, _b; return (_b = (_a = this.displayOptions).onDismiss) === null || _b === void 0 ? void 0 : _b.call(_a, message, action); });
3450
3648
  this.displayedMessages.delete(campaignId);
3451
3649
  }
@@ -3498,6 +3696,12 @@ class InAppMessagingManager {
3498
3696
  return;
3499
3697
  if (!this.runtime.canSend())
3500
3698
  return;
3699
+ const dwell = type !== 'displayed' ? this.awaitingDwell.get(campaignId) : undefined;
3700
+ if (dwell) {
3701
+ if (dwell.generation !== this.generation)
3702
+ return;
3703
+ this.confirmDisplay(campaignId).catch(() => undefined);
3704
+ }
3501
3705
  const sessionId = this.session.getSessionId();
3502
3706
  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);
3503
3707
  if (type === 'displayed') {
@@ -3659,6 +3863,7 @@ class InAppMessagingManager {
3659
3863
  }
3660
3864
  this.campaigns.clear();
3661
3865
  this.displayedMessages.clear();
3866
+ this.awaitingDwell.forEach((_entry, campaignId) => this.dropDwell(campaignId));
3662
3867
  this.watchedAttributes.clear();
3663
3868
  this.iframes.clear();
3664
3869
  this.frameMaxWidths.clear();
@@ -3700,6 +3905,8 @@ class InAppMessagingManager {
3700
3905
  }
3701
3906
  this.campaigns.clear();
3702
3907
  this.displayedMessages.clear();
3908
+ this.awaitingDwell.forEach((_entry, campaignId) => this.dropDwell(campaignId));
3909
+ this.removePageLifecycle();
3703
3910
  this.impressionTimestamps.clear();
3704
3911
  this.watchedAttributes.clear();
3705
3912
  this.iframes.clear();
@@ -7317,4 +7524,4 @@ if (typeof window !== 'undefined') {
7317
7524
  installSnippetQueue(window);
7318
7525
  }
7319
7526
 
7320
- export { EcommerceTracker, JoryioSDK, PushManager, SDK_VERSION, SubscriptionStatus, User, createEcommerceTracker, JoryioSDK as default, installSnippetQueue };
7527
+ export { EcommerceTracker, IN_APP_DISPLAY_DWELL_MS, JoryioSDK, PushManager, SDK_VERSION, SubscriptionStatus, User, createEcommerceTracker, JoryioSDK as default, installSnippetQueue };