@joryio/web-sdk 1.4.0-beta.8 → 1.4.0-beta.9
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/CHANGELOG.md +6 -0
- package/dist/core/config.d.ts +1 -1
- package/dist/core/page-context.d.ts +13 -0
- package/dist/core/page-context.d.ts.map +1 -1
- package/dist/core/page-edits.d.ts +14 -1
- package/dist/core/page-edits.d.ts.map +1 -1
- package/dist/core/placements.d.ts +34 -0
- package/dist/core/placements.d.ts.map +1 -1
- package/dist/core/sdk.d.ts +5 -0
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/show-when.d.ts +42 -0
- package/dist/core/show-when.d.ts.map +1 -0
- package/dist/index.d.mts +25 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/joryio-cards-ui.min.js +1 -1
- package/dist/joryio-cards-ui.min.js.map +1 -1
- package/dist/joryio-editor.min.js +1 -1
- package/dist/joryio-editor.min.js.map +1 -1
- package/dist/joryio-sw.js +1 -1
- package/dist/joryio.cjs.js +571 -26
- package/dist/joryio.min.js +2 -2
- package/dist/joryio.min.js.map +1 -1
- package/dist/joryio.mjs +571 -27
- package/dist/joryio.umd.js +571 -26
- package/package.json +1 -1
- package/src/core/config.ts +1 -1
- package/src/core/page-context.ts +84 -1
- package/src/core/page-edits.ts +87 -10
- package/src/core/placements.ts +373 -6
- package/src/core/sdk.ts +75 -0
- package/src/core/show-when.ts +159 -0
- package/src/index.ts +4 -0
package/src/core/sdk.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { PageEditManager } from './page-edits';
|
|
7
7
|
import { EditorGate, editorFrame, editorRequested } from './editor-gate';
|
|
8
8
|
import { PageContextTracker } from './page-context';
|
|
9
|
+
import { ShowWhenMoments, type RecheckEvent } from './show-when';
|
|
9
10
|
import { InitConfig, ResolvedConfig, resolveConfig, SDK_VERSION } from './config';
|
|
10
11
|
import { StorageManager } from './storage';
|
|
11
12
|
import { SessionManager, SESSION_ID_KEY } from './session';
|
|
@@ -269,6 +270,10 @@ export class JoryioSDK {
|
|
|
269
270
|
// Created with the instance, so setLinkHandler works before init().
|
|
270
271
|
private links = new LinkRouter((error) => this.logger?.error('Your link handler threw; the link opens as without one:', error));
|
|
271
272
|
private pageContext!: PageContextTracker;
|
|
273
|
+
/** Show when (D465): this page view's moments and this visit's event names, for placements and page edits. */
|
|
274
|
+
private showWhen!: ShowWhenMoments;
|
|
275
|
+
/** Show when re-checks waiting for their event to reach the server (see recheckShowWhen). */
|
|
276
|
+
private pendingRechecks: Array<{ event: RecheckEvent; keys: string[]; edits: boolean; who: string; at: number }> = [];
|
|
272
277
|
private pageEdits!: PageEditManager;
|
|
273
278
|
private editor: EditorGate | null = null;
|
|
274
279
|
/** Feature flags + remote config (flags.ts). */
|
|
@@ -558,6 +563,11 @@ export class JoryioSDK {
|
|
|
558
563
|
// UTM page rules (D444 Phase 4 follow-up): the UTM this SDK already recorded, when capture is on.
|
|
559
564
|
this.pageContext.setUtmSource(() => (this.config.captureUTM && this.utm ? this.utm.getUTMData() : null));
|
|
560
565
|
this.inapp.setPageContext(this.pageContext);
|
|
566
|
+
this.showWhen = new ShowWhenMoments(this.pageContext, () => this.session.getSessionId(), this.config.sdkKey);
|
|
567
|
+
// A re-check belongs to its page view: a route change drops the waiting ones.
|
|
568
|
+
this.pageContext.onSignal((s) => {
|
|
569
|
+
if (s.type === 'page_change') this.pendingRechecks = [];
|
|
570
|
+
});
|
|
561
571
|
|
|
562
572
|
this.placements = new PlacementManager(
|
|
563
573
|
this.config.apiEndpoint,
|
|
@@ -582,6 +592,7 @@ export class JoryioSDK {
|
|
|
582
592
|
this.network,
|
|
583
593
|
);
|
|
584
594
|
this.placements.setPageContext(this.pageContext);
|
|
595
|
+
this.placements.setShowWhen(this.showWhen);
|
|
585
596
|
|
|
586
597
|
// Page edits (D444 Phase 4): selector edits from page-edit campaigns.
|
|
587
598
|
this.pageEdits = new PageEditManager(
|
|
@@ -598,6 +609,7 @@ export class JoryioSDK {
|
|
|
598
609
|
this.network,
|
|
599
610
|
);
|
|
600
611
|
this.pageEdits.setPageContext(this.pageContext);
|
|
612
|
+
this.pageEdits.setShowWhen(this.showWhen);
|
|
601
613
|
|
|
602
614
|
this.cards = new CardManager(
|
|
603
615
|
this.config.apiEndpoint,
|
|
@@ -681,6 +693,8 @@ export class JoryioSDK {
|
|
|
681
693
|
try {
|
|
682
694
|
return await this.flushEvents(events, identify);
|
|
683
695
|
} finally {
|
|
696
|
+
// A batch settled: re-checks whose event has now left the queue go (after the queue's own bookkeeping).
|
|
697
|
+
if (this.pendingRechecks.length) setTimeout(() => this.runRechecks(), 0);
|
|
684
698
|
// A logged-out user's token lives as long as their leftovers (D-a).
|
|
685
699
|
this.scheduleRetiredTokenPrune();
|
|
686
700
|
}
|
|
@@ -1081,6 +1095,7 @@ export class JoryioSDK {
|
|
|
1081
1095
|
this.network.switchUser(userId);
|
|
1082
1096
|
this.identity.adopt(anonymousId, userId);
|
|
1083
1097
|
this.placements.invalidate();
|
|
1098
|
+
this.pendingRechecks = []; // never run a re-check queued for another person or consent
|
|
1084
1099
|
// Another person: the anonymous visitor's card state goes too.
|
|
1085
1100
|
this.cards.invalidate(true);
|
|
1086
1101
|
this.pageEdits?.invalidate();
|
|
@@ -1475,6 +1490,7 @@ export class JoryioSDK {
|
|
|
1475
1490
|
// Placement answers too: a grant turns generic answers personal, a
|
|
1476
1491
|
// refusal turns personal answers generic.
|
|
1477
1492
|
this.placements?.invalidate();
|
|
1493
|
+
this.pendingRechecks = []; // never run a re-check queued for another person or consent
|
|
1478
1494
|
// The card feed is asked for only with consent: a grant fetches it, a refusal empties it.
|
|
1479
1495
|
this.cards?.invalidate(false);
|
|
1480
1496
|
// Consent changed: the stored site settings go too (as every per-workspace store).
|
|
@@ -1875,6 +1891,9 @@ export class JoryioSDK {
|
|
|
1875
1891
|
try {
|
|
1876
1892
|
this.inapp?.onEventTracked(eventName, eventProperties);
|
|
1877
1893
|
this.placements?.onEventTracked(eventName, eventProperties);
|
|
1894
|
+
// Show when: content held for this event shows now, and the answers that depend on it are asked again.
|
|
1895
|
+
this.showWhen?.noteEvent(eventName);
|
|
1896
|
+
this.recheckShowWhen(eventName, processedEvent);
|
|
1878
1897
|
} catch (error) {
|
|
1879
1898
|
this.logger.error('In-app trigger evaluation failed:', error);
|
|
1880
1899
|
}
|
|
@@ -1901,6 +1920,57 @@ export class JoryioSDK {
|
|
|
1901
1920
|
}
|
|
1902
1921
|
}
|
|
1903
1922
|
|
|
1923
|
+
/**
|
|
1924
|
+
* Show when (D465): `eventName` was tracked and an answer depends on it
|
|
1925
|
+
* (`recheckOn`, or held content waiting on it). The re-check never reaches
|
|
1926
|
+
* the server before the event: the queue is flushed (after any flush in
|
|
1927
|
+
* progress), and those placement keys and this page's edits are asked again
|
|
1928
|
+
* only once the event has left the queue - delivered - past the 30 s
|
|
1929
|
+
* placement limit, once per (key or page, event) per page view. A failed
|
|
1930
|
+
* flush keeps it waiting; it runs once, after the next flush that delivers
|
|
1931
|
+
* the event. The request carries the event (`recheckEvent`), so the server
|
|
1932
|
+
* counts it even before its own write of the event lands.
|
|
1933
|
+
*/
|
|
1934
|
+
private recheckShowWhen(eventName: string, queued: QueuedEvent): void {
|
|
1935
|
+
const keys = this.placements?.recheckKeysFor(eventName) || [];
|
|
1936
|
+
const edits = !!this.pageEdits?.claimRecheck(eventName);
|
|
1937
|
+
if (!keys.length && !edits) return;
|
|
1938
|
+
const event: RecheckEvent = { name: eventName, eventId: queued.eventId, properties: { ...(queued.properties || {}) } };
|
|
1939
|
+
// Stamped with who it is for (identity, consent, page); at most 20, the oldest dropped.
|
|
1940
|
+
this.pendingRechecks.push({ event, keys, edits, who: this.recheckOwner(), at: Date.now() });
|
|
1941
|
+
if (this.pendingRechecks.length > 20) this.pendingRechecks.shift();
|
|
1942
|
+
void this.queue
|
|
1943
|
+
.flush(false, true)
|
|
1944
|
+
.catch(() => undefined)
|
|
1945
|
+
.then(() => this.runRechecks());
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
/** Who a re-check is for: the visitor, the consent and the page it was queued on. */
|
|
1949
|
+
private recheckOwner(): string {
|
|
1950
|
+
return [this.identity.getUserId() || '', this.identity.getAnonymousId(), this.placementConsent() || '', this.pageContext?.current().url || ''].join('\u0000');
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
/**
|
|
1954
|
+
* Run the waiting re-checks whose event is no longer queued or in flight.
|
|
1955
|
+
* One queued for another person, consent or page, or older than 60 s, is
|
|
1956
|
+
* dropped - its event (and properties) never ride on someone else's request.
|
|
1957
|
+
*/
|
|
1958
|
+
private runRechecks(): void {
|
|
1959
|
+
if (!this.pendingRechecks.length || !this.isInitialized) return;
|
|
1960
|
+
const owner = this.recheckOwner();
|
|
1961
|
+
const now = Date.now();
|
|
1962
|
+
this.pendingRechecks = this.pendingRechecks.filter((r) => r.who === owner && now - r.at <= 60_000);
|
|
1963
|
+
const waiting = new Set([...this.queue.getQueuedEvents(), ...this.queue.getInflightEvents()].map((e) => e.eventId));
|
|
1964
|
+
const due = this.pendingRechecks.filter((r) => !waiting.has(r.event.eventId));
|
|
1965
|
+
if (!due.length) return;
|
|
1966
|
+
this.pendingRechecks = this.pendingRechecks.filter((r) => waiting.has(r.event.eventId));
|
|
1967
|
+
if (!this.canSend()) return;
|
|
1968
|
+
for (const r of due) {
|
|
1969
|
+
if (r.keys.length) void this.placements.recheck(r.keys, r.event);
|
|
1970
|
+
if (r.edits) this.pageEdits.recheck(r.event);
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1904
1974
|
/**
|
|
1905
1975
|
* Identify a user.
|
|
1906
1976
|
*
|
|
@@ -1954,6 +2024,7 @@ export class JoryioSDK {
|
|
|
1954
2024
|
if (previousUserId !== userId) {
|
|
1955
2025
|
this.inapp.invalidate();
|
|
1956
2026
|
this.placements.invalidate();
|
|
2027
|
+
this.pendingRechecks = []; // never run a re-check queued for another person or consent
|
|
1957
2028
|
this.cards.invalidate(true);
|
|
1958
2029
|
this.pageEdits?.invalidate();
|
|
1959
2030
|
this.flags.onIdentityChanged();
|
|
@@ -2521,6 +2592,7 @@ export class JoryioSDK {
|
|
|
2521
2592
|
this.flags.onIdentityChanged();
|
|
2522
2593
|
this.network.setCurrentUserId(this.identity.getUserId());
|
|
2523
2594
|
this.placements.invalidate();
|
|
2595
|
+
this.pendingRechecks = []; // never run a re-check queued for another person or consent
|
|
2524
2596
|
this.cards.invalidate(true);
|
|
2525
2597
|
// reset / resetTo: the stored site settings go too (identify keeps them - the same for every visitor).
|
|
2526
2598
|
this.pageEdits?.invalidate({ siteSettings: true });
|
|
@@ -2633,6 +2705,7 @@ export class JoryioSDK {
|
|
|
2633
2705
|
this.flags.onIdentityChanged();
|
|
2634
2706
|
this.network.setCurrentUserId(null);
|
|
2635
2707
|
this.placements.invalidate();
|
|
2708
|
+
this.pendingRechecks = []; // never run a re-check queued for another person or consent
|
|
2636
2709
|
this.cards?.invalidate(true);
|
|
2637
2710
|
this.pageEdits?.invalidate({ siteSettings: true });
|
|
2638
2711
|
this.utm.reset();
|
|
@@ -3693,6 +3766,8 @@ export class JoryioSDK {
|
|
|
3693
3766
|
this.cards.destroy();
|
|
3694
3767
|
this.pageEdits?.destroy();
|
|
3695
3768
|
this.editor?.stop();
|
|
3769
|
+
this.showWhen?.destroy();
|
|
3770
|
+
this.pendingRechecks = [];
|
|
3696
3771
|
this.pageContext?.destroy();
|
|
3697
3772
|
this.flags.destroy();
|
|
3698
3773
|
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Show when" (BACKLOG D465): the moment a Placement or Web personalization
|
|
3
|
+
* campaign's content is revealed in a page view. The content is fetched as
|
|
4
|
+
* before and HELD; it is revealed at its moment and then stays for that page
|
|
5
|
+
* view. A route change (SPA) starts the moment again.
|
|
6
|
+
*
|
|
7
|
+
* { kind: 'load' } the page loads (default; also: no field)
|
|
8
|
+
* { kind: 'seconds', seconds: 1..3600 } visible seconds on the page (like `time_on_page`)
|
|
9
|
+
* { kind: 'scroll', percent: 1..100 } share of the page scrolled (like `scroll_depth`)
|
|
10
|
+
* { kind: 'exit' } exit intent (like `exit_intent`): the pointer leaves through the top; touch: a fast scroll up
|
|
11
|
+
* { kind: 'event', event: '<name>' } the event was tracked in this visit (SDK session)
|
|
12
|
+
*
|
|
13
|
+
* The page moments are the PageContextTracker's own progress (the same clock
|
|
14
|
+
* the overlay page triggers use, reset on a route change); the visit's event
|
|
15
|
+
* names are kept here, in memory and sessionStorage, per SDK session id.
|
|
16
|
+
*
|
|
17
|
+
* Names and wire values are the backend's (common/show-when.ts) and the same
|
|
18
|
+
* on every Joryio SDK (pinned by sdk-show-when-parity.spec.ts).
|
|
19
|
+
*/
|
|
20
|
+
import { bucketHash } from '../utils/bucket-hash';
|
|
21
|
+
import type { PageContextTracker } from './page-context';
|
|
22
|
+
|
|
23
|
+
export const SHOW_WHEN_KINDS = ['load', 'seconds', 'scroll', 'exit', 'event'] as const;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The just-tracked event a show-when re-check request carries
|
|
27
|
+
* (`recheckEvent`): the server counts it as arrived in the audience
|
|
28
|
+
* conditions, so the re-check never races the event's own write.
|
|
29
|
+
*/
|
|
30
|
+
export interface RecheckEvent {
|
|
31
|
+
name: string;
|
|
32
|
+
eventId: string;
|
|
33
|
+
properties: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ShowWhenKind = (typeof SHOW_WHEN_KINDS)[number];
|
|
37
|
+
export type ShowWhen =
|
|
38
|
+
| { kind: 'load' }
|
|
39
|
+
| { kind: 'seconds'; seconds: number }
|
|
40
|
+
| { kind: 'scroll'; percent: number }
|
|
41
|
+
| { kind: 'exit' }
|
|
42
|
+
| { kind: 'event'; event: string };
|
|
43
|
+
|
|
44
|
+
const intIn = (v: unknown, min: number, max: number): v is number => typeof v === 'number' && Number.isInteger(v) && v >= min && v <= max;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The served value as this SDK honours it: absent = `load`; null = unknown
|
|
48
|
+
* kind or invalid value - such content is never shown (never "on load").
|
|
49
|
+
*/
|
|
50
|
+
export function parseShowWhen(raw: unknown): ShowWhen | null {
|
|
51
|
+
if (raw === undefined || raw === null) return { kind: 'load' };
|
|
52
|
+
if (typeof raw !== 'object') return null;
|
|
53
|
+
const o = raw as Record<string, unknown>;
|
|
54
|
+
switch (o.kind) {
|
|
55
|
+
case 'load':
|
|
56
|
+
case 'exit':
|
|
57
|
+
return { kind: o.kind };
|
|
58
|
+
case 'seconds':
|
|
59
|
+
return intIn(o.seconds, 1, 3600) ? { kind: 'seconds', seconds: o.seconds } : null;
|
|
60
|
+
case 'scroll':
|
|
61
|
+
return intIn(o.percent, 1, 100) ? { kind: 'scroll', percent: o.percent } : null;
|
|
62
|
+
case 'event': {
|
|
63
|
+
const e = typeof o.event === 'string' ? o.event.trim() : '';
|
|
64
|
+
return e && e.length <= 255 ? { kind: 'event', event: e } : null;
|
|
65
|
+
}
|
|
66
|
+
default:
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** True for content that waits for a moment (valid or not): it is held, or never shown. */
|
|
72
|
+
export const isDeferred = (raw: unknown): boolean => parseShowWhen(raw)?.kind !== 'load';
|
|
73
|
+
|
|
74
|
+
/** Event names that make the SDK ask again (the answer's `recheckOn`): strings of 1-255 characters. */
|
|
75
|
+
export function eventNames(raw: unknown, max = 50): string[] {
|
|
76
|
+
return Array.isArray(raw) ? raw.filter((e): e is string => typeof e === 'string' && e.length > 0 && e.length <= 255).slice(0, max) : [];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const MAX_EVENTS = 200;
|
|
80
|
+
|
|
81
|
+
/** The moments of this page view and the event names of this visit. One per SDK. */
|
|
82
|
+
export class ShowWhenMoments {
|
|
83
|
+
private events = new Set<string>();
|
|
84
|
+
private sid: string | null | undefined = undefined;
|
|
85
|
+
private listeners = new Set<() => void>();
|
|
86
|
+
private off: (() => void) | null;
|
|
87
|
+
private readonly store: string;
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
private page: Pick<PageContextTracker, 'progress' | 'onSignal'> | null,
|
|
91
|
+
private sessionId: () => string | null,
|
|
92
|
+
sdkKey: string,
|
|
93
|
+
) {
|
|
94
|
+
this.store = `jry_show_when_${bucketHash(sdkKey).toString(36)}`;
|
|
95
|
+
// Every page signal (a second of visible time, a deeper scroll, exit intent, a route change) may bring a moment.
|
|
96
|
+
this.off = page ? page.onSignal(() => this.emit()) : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Has this moment come in the current page view? `load` always; null (invalid) never. */
|
|
100
|
+
met(sw: ShowWhen | null): boolean {
|
|
101
|
+
if (!sw) return false;
|
|
102
|
+
if (sw.kind === 'load') return true;
|
|
103
|
+
if (sw.kind === 'event') return this.visitEvents().has(sw.event);
|
|
104
|
+
const p = this.page?.progress();
|
|
105
|
+
if (!p) return false;
|
|
106
|
+
return sw.kind === 'seconds' ? p.seconds >= sw.seconds : sw.kind === 'scroll' ? p.scrollPercent >= sw.percent : p.exitIntent;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** An event was tracked: remember its name for this visit and re-check every held content. */
|
|
110
|
+
noteEvent(name: string): void {
|
|
111
|
+
const events = this.visitEvents();
|
|
112
|
+
if (!events.has(name) && events.size < MAX_EVENTS) {
|
|
113
|
+
events.add(name);
|
|
114
|
+
try {
|
|
115
|
+
sessionStorage.setItem(this.store, JSON.stringify({ sid: this.sid, names: Array.from(events) }));
|
|
116
|
+
} catch {
|
|
117
|
+
/* storage blocked: memory only, this page */
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
this.emit();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Called whenever a moment may have come. */
|
|
124
|
+
onChange(cb: () => void): () => void {
|
|
125
|
+
this.listeners.add(cb);
|
|
126
|
+
return () => this.listeners.delete(cb);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
destroy(): void {
|
|
130
|
+
this.off?.();
|
|
131
|
+
this.listeners.clear();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The event names tracked in the current SDK session (a new session starts empty). */
|
|
135
|
+
private visitEvents(): Set<string> {
|
|
136
|
+
const sid = this.sessionId();
|
|
137
|
+
if (sid !== this.sid) {
|
|
138
|
+
this.sid = sid;
|
|
139
|
+
this.events = new Set();
|
|
140
|
+
try {
|
|
141
|
+
const raw = JSON.parse(sessionStorage.getItem(this.store) || 'null');
|
|
142
|
+
if (raw && sid && raw.sid === sid) this.events = new Set(eventNames(raw.names, MAX_EVENTS));
|
|
143
|
+
} catch {
|
|
144
|
+
/* ignore */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return this.events;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private emit(): void {
|
|
151
|
+
for (const cb of Array.from(this.listeners)) {
|
|
152
|
+
try {
|
|
153
|
+
cb();
|
|
154
|
+
} catch {
|
|
155
|
+
/* a listener never breaks the others */
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -86,6 +86,10 @@ export type {
|
|
|
86
86
|
PlacementsUpdatedCallback,
|
|
87
87
|
} from './core/placements';
|
|
88
88
|
|
|
89
|
+
// Show when: the moment a placement item or page edit is revealed (the same names and values on every Joryio SDK).
|
|
90
|
+
export { SHOW_WHEN_KINDS } from './core/show-when';
|
|
91
|
+
export type { ShowWhen, ShowWhenKind } from './core/show-when';
|
|
92
|
+
|
|
89
93
|
// Links: the site-wide link handler (the same names and source values on every Joryio SDK).
|
|
90
94
|
export type { JoryioLink, JoryioLinkSource, JoryioLinkHandler } from './core/links';
|
|
91
95
|
|