|
| 1 | +/* A/B tests on the hub through Iterglow (Pseudo-Lab's experiment platform). |
| 2 | + Off until data/experiments.json has "enabled": true, the project's public SDK key, and for each |
| 3 | + experiment the flag key and the sdk_v1 experiment id an Iterglow admin created. While off, the SDK |
| 4 | + is never loaded and nothing leaves the page. |
| 5 | +
|
| 6 | + Other scripts ask for a variant and report the moment the variant could be seen: |
| 7 | + const v = await window.hubExperiment.variant("pseudo-greeting", 1200); // "control", "treatment", ... or null |
| 8 | + window.hubExperiment.expose("pseudo-greeting"); |
| 9 | + null means "no experiment": keep the page's normal behaviour. Goal events are the hub's own |
| 10 | + hubTrack events, mapped per experiment in experiments.json, and count only after an exposure. */ |
| 11 | +(() => { |
| 12 | + "use strict"; |
| 13 | + const state = new Map(); // id -> { decision, exposure } |
| 14 | + let ready = null, sdk = null, cfg = null; |
| 15 | + |
| 16 | + function start() { |
| 17 | + if (ready) return ready; |
| 18 | + ready = fetch("data/experiments.json") |
| 19 | + .then((r) => (r.ok ? r.json() : null)) |
| 20 | + .then(async (c) => { |
| 21 | + if (!c || !c.enabled || !/^pk_/.test(c.apiKey || "") || !/^https:\/\//.test(c.baseUrl || "")) return null; |
| 22 | + cfg = c; |
| 23 | + const { IterglowSDK } = await import("../vendor/iterglow-sdk-0.3.0.js"); |
| 24 | + sdk = new IterglowSDK({ apiKey: c.apiKey, baseUrl: c.baseUrl, eventModel: "sdk_v1" }); |
| 25 | + return sdk; |
| 26 | + }) |
| 27 | + .catch(() => null); |
| 28 | + return ready; |
| 29 | + } |
| 30 | + const find = (id) => (cfg && (cfg.experiments || []).find((x) => x.id === id && x.flagKey && x.experimentId)) || null; |
| 31 | + |
| 32 | + /* the variant for this visitor, or null when the experiment is off, unknown, slow or failing */ |
| 33 | + async function variant(id, timeout = 1200) { |
| 34 | + const timer = new Promise((res) => setTimeout(() => res(null), timeout)); |
| 35 | + const ask = (async () => { |
| 36 | + if (!(await start())) return null; |
| 37 | + const x = find(id); |
| 38 | + if (!x) return null; |
| 39 | + if (state.has(id)) return state.get(id).decision.variant; |
| 40 | + const d = await sdk.decide(x.flagKey, { experimentId: x.experimentId }); |
| 41 | + if (!d || d.key !== x.flagKey || d.type !== "flag" || !d.variant || !d.tracking_context) return null; |
| 42 | + state.set(id, { decision: d, exposure: null }); |
| 43 | + return d.variant; |
| 44 | + })().catch(() => null); |
| 45 | + const v = await Promise.race([ask, timer]); |
| 46 | + /* a decision that arrives after the timeout was never shown, so it is dropped rather than exposed */ |
| 47 | + if (v === null && state.has(id) && !state.get(id).exposure) state.delete(id); |
| 48 | + return v; |
| 49 | + } |
| 50 | + |
| 51 | + /* call when the variant could actually be seen, in every arm, control included */ |
| 52 | + async function expose(id) { |
| 53 | + const s = state.get(id); |
| 54 | + if (!s || s.exposure) return; |
| 55 | + try { s.exposure = await sdk.expose(s.decision); } catch (e) { /* not recorded; goals stay unattributed */ } |
| 56 | + } |
| 57 | + |
| 58 | + /* goals: the hub's own events, forwarded once an experiment has been exposed */ |
| 59 | + function goal(name) { |
| 60 | + if (!cfg || !sdk) return; |
| 61 | + state.forEach((s, id) => { |
| 62 | + const x = find(id), g = x && x.goals && x.goals[name]; |
| 63 | + if (!g || !s.exposure) return; |
| 64 | + sdk.track(g, { hub_event: name }, { tracking_context: s.exposure, eventId: crypto.randomUUID() }).catch(() => {}); |
| 65 | + }); |
| 66 | + } |
| 67 | + const wrap = () => { |
| 68 | + const orig = window.hubTrack; |
| 69 | + if (typeof orig !== "function" || orig.__exp) return; |
| 70 | + const wrapped = (name, params) => { orig(name, params); goal(name); }; |
| 71 | + wrapped.__exp = true; |
| 72 | + window.hubTrack = wrapped; |
| 73 | + }; |
| 74 | + if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", wrap); else wrap(); |
| 75 | + |
| 76 | + window.hubExperiment = { variant, expose }; |
| 77 | +})(); |
0 commit comments