@splithub/core 0.2.3 → 0.4.0
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/README.md +1 -1
- package/dist/{chunk-LRAW5YQY.js → chunk-7CE2FI5Y.js} +515 -63
- package/dist/chunk-7CE2FI5Y.js.map +1 -0
- package/dist/index.cjs +520 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +73 -5
- package/dist/index.d.ts +73 -5
- package/dist/index.js +19 -1
- package/dist/index.js.map +1 -1
- package/dist/{types-DiW3b8D4.d.cts → types-DKx9MXeC.d.cts} +122 -13
- package/dist/{types-DiW3b8D4.d.ts → types-DKx9MXeC.d.ts} +122 -13
- package/dist/vite.cjs +10 -1
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-LRAW5YQY.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,7 @@ __export(index_exports, {
|
|
|
23
23
|
CLOUD_ENDPOINT: () => CLOUD_ENDPOINT,
|
|
24
24
|
ConfigError: () => ConfigError,
|
|
25
25
|
EMPTY_OVERRIDES: () => EMPTY_OVERRIDES,
|
|
26
|
+
EVENT_NAME_PATTERN: () => EVENT_NAME_PATTERN,
|
|
26
27
|
EventQueue: () => EventQueue,
|
|
27
28
|
HOLDOUT_KEY: () => HOLDOUT_KEY,
|
|
28
29
|
KEYED_FIELDS: () => KEYED_FIELDS,
|
|
@@ -30,6 +31,7 @@ __export(index_exports, {
|
|
|
30
31
|
MVT_MAX_COMBINATIONS: () => MVT_MAX_COMBINATIONS,
|
|
31
32
|
MVT_MAX_FACTORS: () => MVT_MAX_FACTORS,
|
|
32
33
|
MVT_MAX_LEVELS: () => MVT_MAX_LEVELS,
|
|
34
|
+
PROPERTY_KEY_PATTERN: () => PROPERTY_KEY_PATTERN,
|
|
33
35
|
REMOTE_TTL_MS: () => REMOTE_TTL_MS,
|
|
34
36
|
REMOTE_WAIT_MS: () => REMOTE_WAIT_MS,
|
|
35
37
|
SDK_VERSION: () => SDK_VERSION,
|
|
@@ -49,11 +51,13 @@ __export(index_exports, {
|
|
|
49
51
|
defineConfig: () => defineConfig,
|
|
50
52
|
devNote: () => devNote,
|
|
51
53
|
evaluateCondition: () => evaluateCondition,
|
|
54
|
+
eventKey: () => eventKey,
|
|
52
55
|
explainAssignment: () => explainAssignment,
|
|
53
56
|
fetchRemoteOverrides: () => fetchRemoteOverrides,
|
|
54
57
|
getExperiment: () => getExperiment,
|
|
55
58
|
getOrCreateSessionId: () => getOrCreateSessionId,
|
|
56
59
|
getOrCreateVisitorId: () => getOrCreateVisitorId,
|
|
60
|
+
goalKeyFor: () => goalKeyFor,
|
|
57
61
|
hasVisitorId: () => hasVisitorId,
|
|
58
62
|
hashToUnit: () => hashToUnit,
|
|
59
63
|
holdoutExperiment: () => holdoutExperiment,
|
|
@@ -76,16 +80,21 @@ __export(index_exports, {
|
|
|
76
80
|
murmur3: () => murmur3,
|
|
77
81
|
mvtVariants: () => mvtVariants,
|
|
78
82
|
newId: () => newId,
|
|
83
|
+
newPropertyId: () => newPropertyId,
|
|
79
84
|
newSessionId: () => newSessionId,
|
|
80
85
|
newVisitorId: () => newVisitorId,
|
|
81
86
|
normalizePath: () => normalizePath,
|
|
87
|
+
normalizeProperty: () => normalizeProperty,
|
|
82
88
|
onDebug: () => onDebug,
|
|
83
89
|
parseCookies: () => parseCookies,
|
|
84
90
|
parseForceParam: () => parseForceParam,
|
|
85
91
|
parseUserAgent: () => parseUserAgent,
|
|
92
|
+
pickProperty: () => pickProperty,
|
|
86
93
|
pickVariant: () => pickVariant,
|
|
87
94
|
readRemoteCache: () => readRemoteCache,
|
|
88
95
|
redirectTarget: () => redirectTarget,
|
|
96
|
+
referrerOrigin: () => referrerOrigin,
|
|
97
|
+
resetAnalyticsClaims: () => resetAnalyticsClaims,
|
|
89
98
|
resetDevNotes: () => resetDevNotes,
|
|
90
99
|
resolveOverridesUrl: () => resolveOverridesUrl,
|
|
91
100
|
runFlow: () => runFlow,
|
|
@@ -492,13 +501,13 @@ function normalizeVariants(expKey, input) {
|
|
|
492
501
|
}
|
|
493
502
|
var HOLDOUT_KEY = "__holdout";
|
|
494
503
|
var LAYER_BUCKETS = 100;
|
|
495
|
-
function normalizeMetrics(key, input, goals) {
|
|
504
|
+
function normalizeMetrics(key, input, primary, goals) {
|
|
496
505
|
const m = input.metrics;
|
|
497
506
|
if (!m) return void 0;
|
|
498
|
-
if (!
|
|
499
|
-
const seen = /* @__PURE__ */ new Set([
|
|
500
|
-
const check = (
|
|
501
|
-
|
|
507
|
+
if (!primary) throw new ConfigError(`experiment "${key}": metrics need a primary "goal" too.`);
|
|
508
|
+
const seen = /* @__PURE__ */ new Set([primary]);
|
|
509
|
+
const check = (name, where) => {
|
|
510
|
+
const goal = goalFor(name, goals, `experiment "${key}": metrics.${where}`);
|
|
502
511
|
if (seen.has(goal)) throw new ConfigError(`experiment "${key}": goal "${goal}" is listed twice (primary goal and metrics count once each).`);
|
|
503
512
|
seen.add(goal);
|
|
504
513
|
return goal;
|
|
@@ -539,9 +548,9 @@ function normalizeHoldout(input, goals) {
|
|
|
539
548
|
const h = input.holdout;
|
|
540
549
|
if (!h) return null;
|
|
541
550
|
if (typeof h.percent !== "number" || !Number.isFinite(h.percent) || h.percent < 0 || h.percent > 50) throw new ConfigError("holdout.percent must be a number between 0 and 50.");
|
|
542
|
-
|
|
551
|
+
const goal = h.goal ? goalFor(h.goal, goals, "holdout.goal") : null;
|
|
543
552
|
if (h.percent === 0) return null;
|
|
544
|
-
return { percent: h.percent, seed: h.seed ?? HOLDOUT_KEY, goal:
|
|
553
|
+
return { percent: h.percent, seed: h.seed ?? HOLDOUT_KEY, goal: goal ?? goals[0]?.key ?? null };
|
|
545
554
|
}
|
|
546
555
|
function normalizeExperiment(key, input, goals, layers = {}) {
|
|
547
556
|
assertKey("experiment", key);
|
|
@@ -549,9 +558,7 @@ function normalizeExperiment(key, input, goals, layers = {}) {
|
|
|
549
558
|
if (traffic < 0 || traffic > 1) {
|
|
550
559
|
throw new ConfigError(`experiment "${key}": traffic must be between 0 and 1.`);
|
|
551
560
|
}
|
|
552
|
-
|
|
553
|
-
throw new ConfigError(`experiment "${key}": goal "${input.goal}" is not declared in "goals".`);
|
|
554
|
-
}
|
|
561
|
+
const goal = input.goal ? goalFor(input.goal, goals, `experiment "${key}"`) : null;
|
|
555
562
|
if (input.kind === "redirect" && !input.match) {
|
|
556
563
|
throw new ConfigError(`experiment "${key}": redirect experiments need "match".`);
|
|
557
564
|
}
|
|
@@ -564,11 +571,14 @@ function normalizeExperiment(key, input, goals, layers = {}) {
|
|
|
564
571
|
if (input.goodSample !== void 0 && !(Number.isInteger(input.goodSample) && input.goodSample >= 100 && input.goodSample <= 1e7)) {
|
|
565
572
|
throw new ConfigError(`experiment "${key}": goodSample must be a whole number from 100 to 10,000,000.`);
|
|
566
573
|
}
|
|
574
|
+
if (input.name !== void 0 && !(typeof input.name === "string" && input.name.trim() && input.name.trim().length <= 80)) {
|
|
575
|
+
throw new ConfigError(`experiment "${key}": name must be text of 1 to 80 characters.`);
|
|
576
|
+
}
|
|
567
577
|
for (const [k, v] of Object.entries(input.schedule ?? {})) {
|
|
568
578
|
if (v !== void 0 && Number.isNaN(Date.parse(v))) throw new ConfigError(`experiment "${key}": schedule.${k} must be an ISO date.`);
|
|
569
579
|
}
|
|
570
580
|
const variants = normalizeVariants(key, input);
|
|
571
|
-
const metrics = normalizeMetrics(key, input, goals);
|
|
581
|
+
const metrics = normalizeMetrics(key, input, goal, goals);
|
|
572
582
|
const layer = normalizeLayer(key, input, layers);
|
|
573
583
|
if (input.flow) {
|
|
574
584
|
const problem = validateFlow(input.flow, variants.map((v) => v.key));
|
|
@@ -579,9 +589,10 @@ function normalizeExperiment(key, input, goals, layers = {}) {
|
|
|
579
589
|
kind: input.kind,
|
|
580
590
|
status: input.status,
|
|
581
591
|
traffic,
|
|
582
|
-
goal
|
|
592
|
+
goal,
|
|
583
593
|
seed: input.seed ?? key,
|
|
584
594
|
description: input.description ?? null,
|
|
595
|
+
...input.name !== void 0 ? { name: input.name.trim() } : {},
|
|
585
596
|
match: input.kind === "redirect" ? input.match : null,
|
|
586
597
|
variants,
|
|
587
598
|
...input.kind === "mvt" ? { factors: mvtVariants(key, input.factors).factors } : {},
|
|
@@ -594,6 +605,67 @@ function normalizeExperiment(key, input, goals, layers = {}) {
|
|
|
594
605
|
...input.schedule && (input.schedule.startAt || input.schedule.endAt) ? { schedule: { startAt: input.schedule.startAt ?? null, endAt: input.schedule.endAt ?? null } } : {}
|
|
595
606
|
};
|
|
596
607
|
}
|
|
608
|
+
function goalExtras(key, g) {
|
|
609
|
+
if (g.name !== void 0 && !(typeof g.name === "string" && g.name.trim() && g.name.trim().length <= 80)) throw new ConfigError(`goal "${key}": name must be text of 1 to 80 characters.`);
|
|
610
|
+
if (g.value !== void 0 && !(typeof g.value === "number" && Number.isFinite(g.value) && g.value >= 0)) throw new ConfigError(`goal "${key}": value must be a number of 0 or more.`);
|
|
611
|
+
const event = g.type === "event" ? g.event : void 0;
|
|
612
|
+
if (event !== void 0 && !EVENT_NAME_PATTERN.test(event)) throw new ConfigError(`goal "${key}": event "${event}" is not a valid event name.`);
|
|
613
|
+
return { ...g.name !== void 0 ? { name: g.name.trim() } : {}, ...g.value !== void 0 ? { value: g.value } : {}, ...event !== void 0 && event !== key ? { event } : {} };
|
|
614
|
+
}
|
|
615
|
+
var PROPERTY_KEY_PATTERN = KEY_PATTERN;
|
|
616
|
+
var ID_ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";
|
|
617
|
+
function newPropertyId() {
|
|
618
|
+
const bytes = new Uint8Array(6);
|
|
619
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
620
|
+
return `sh-${Array.from(bytes, (b) => ID_ALPHABET[b % ID_ALPHABET.length]).join("")}`;
|
|
621
|
+
}
|
|
622
|
+
var EVENT_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9 _.:/-]{0,63}$/;
|
|
623
|
+
function eventKey(name) {
|
|
624
|
+
return String(name).trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z0-9]+/, "").slice(0, 64).replace(/-+$/, "");
|
|
625
|
+
}
|
|
626
|
+
function goalKeyFor(goals, name) {
|
|
627
|
+
if (goals.some((g) => g.key === name)) return name;
|
|
628
|
+
const byEvent = goals.find((g) => g.event === name);
|
|
629
|
+
if (byEvent) return byEvent.key;
|
|
630
|
+
const slug = eventKey(name);
|
|
631
|
+
return KEY_PATTERN.test(slug) ? slug : null;
|
|
632
|
+
}
|
|
633
|
+
function goalFor(name, goals, where) {
|
|
634
|
+
if (typeof name !== "string" || !(KEY_PATTERN.test(name) || EVENT_NAME_PATTERN.test(name))) {
|
|
635
|
+
throw new ConfigError(`${where}: goal "${String(name)}" is not a valid event name (letters, digits, spaces and "_ . : / -", up to 64 characters).`);
|
|
636
|
+
}
|
|
637
|
+
const key = goalKeyFor(goals, name);
|
|
638
|
+
if (!key) throw new ConfigError(`${where}: goal "${name}" has no usable key; use letters or digits in the name.`);
|
|
639
|
+
if (!goals.some((g) => g.key === key)) {
|
|
640
|
+
goals.push({ key, type: "event", path: null, selector: null, revenue: false, description: null, ...name !== key ? { event: name } : {} });
|
|
641
|
+
}
|
|
642
|
+
return key;
|
|
643
|
+
}
|
|
644
|
+
function normalizeProperty(key, input, goals) {
|
|
645
|
+
assertKey("property", key);
|
|
646
|
+
const p = input ?? {};
|
|
647
|
+
const status = p.status ?? "running";
|
|
648
|
+
if (status !== "running" && status !== "paused") throw new ConfigError(`property "${key}": status must be "running" or "paused".`);
|
|
649
|
+
if (p.name !== void 0 && !(typeof p.name === "string" && p.name.trim() && p.name.trim().length <= 80)) {
|
|
650
|
+
throw new ConfigError(`property "${key}": name must be text of 1 to 80 characters.`);
|
|
651
|
+
}
|
|
652
|
+
const list2 = p.goals ?? [];
|
|
653
|
+
if (!Array.isArray(list2)) throw new ConfigError(`property "${key}": goals must be a list of goal keys.`);
|
|
654
|
+
for (const g of list2) {
|
|
655
|
+
if (!goals.some((d) => d.key === g)) throw new ConfigError(`property "${key}": goal "${String(g)}" is not declared in "goals".`);
|
|
656
|
+
}
|
|
657
|
+
return {
|
|
658
|
+
key,
|
|
659
|
+
status,
|
|
660
|
+
...p.name !== void 0 ? { name: p.name.trim() } : {},
|
|
661
|
+
description: p.description ?? null,
|
|
662
|
+
goals: [...new Set(list2)]
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
function pickProperty(config, key) {
|
|
666
|
+
if (!key) return null;
|
|
667
|
+
return (config.properties ?? []).find((p) => p.key === key) ?? null;
|
|
668
|
+
}
|
|
597
669
|
function defineConfig(input) {
|
|
598
670
|
if (!input || typeof input !== "object") throw new ConfigError("config must be an object.");
|
|
599
671
|
if (!input.project) throw new ConfigError('"project" is required.');
|
|
@@ -609,7 +681,8 @@ function defineConfig(input) {
|
|
|
609
681
|
path: g.type === "pageview" ? g.path : null,
|
|
610
682
|
selector: g.type === "click" ? g.selector : null,
|
|
611
683
|
revenue: g.type === "event" ? Boolean(g.revenue) : false,
|
|
612
|
-
description: g.description ?? null
|
|
684
|
+
description: g.description ?? null,
|
|
685
|
+
...goalExtras(key, g)
|
|
613
686
|
};
|
|
614
687
|
});
|
|
615
688
|
const layers = {};
|
|
@@ -622,6 +695,7 @@ function defineConfig(input) {
|
|
|
622
695
|
);
|
|
623
696
|
assertNoOverlap(experiments);
|
|
624
697
|
const holdout = normalizeHoldout(input, goals);
|
|
698
|
+
const properties = Object.entries(input.properties ?? {}).map(([key, p]) => normalizeProperty(key, p, goals));
|
|
625
699
|
return {
|
|
626
700
|
project: input.project,
|
|
627
701
|
storage: input.storage,
|
|
@@ -629,6 +703,7 @@ function defineConfig(input) {
|
|
|
629
703
|
runDraftsInDev: input.runDraftsInDev === true,
|
|
630
704
|
goals,
|
|
631
705
|
experiments,
|
|
706
|
+
...properties.length ? { properties } : {},
|
|
632
707
|
...Object.keys(layers).length ? { layers } : {},
|
|
633
708
|
...holdout ? { holdout } : {},
|
|
634
709
|
__splithub: 2
|
|
@@ -652,7 +727,7 @@ function holdoutExperiment(config) {
|
|
|
652
727
|
traffic: 1,
|
|
653
728
|
goal: h.goal,
|
|
654
729
|
seed: h.seed,
|
|
655
|
-
description: `Holdout: ${h.percent}% of visitors see no
|
|
730
|
+
description: `Holdout: ${h.percent}% of visitors see no experiment at all`,
|
|
656
731
|
match: null,
|
|
657
732
|
variants: [
|
|
658
733
|
{ key: "holdout", weight: share },
|
|
@@ -796,11 +871,11 @@ function explainAssignment(experiment, a) {
|
|
|
796
871
|
return;
|
|
797
872
|
}
|
|
798
873
|
if (a.reason === "holdout") {
|
|
799
|
-
devNote("holdout", `This visitor is in the holdout, so every
|
|
874
|
+
devNote("holdout", `This visitor is in the holdout, so every experiment shows its control. Open ?sh_force=${key}:${experiment.variants[1]?.key ?? "variant"} to see a variant.`);
|
|
800
875
|
return;
|
|
801
876
|
}
|
|
802
877
|
if (a.reason === "layer") {
|
|
803
|
-
devNote(`${key}:layer`, `"${key}" shares layer "${experiment.layer?.key}" with other
|
|
878
|
+
devNote(`${key}:layer`, `"${key}" shares layer "${experiment.layer?.key}" with other experiments and this visitor's bucket belongs to another one, so they see the control.`);
|
|
804
879
|
return;
|
|
805
880
|
}
|
|
806
881
|
if (a.reason !== "not_running") return;
|
|
@@ -814,6 +889,29 @@ function explainAssignment(experiment, a) {
|
|
|
814
889
|
}
|
|
815
890
|
}
|
|
816
891
|
|
|
892
|
+
// src/shared.ts
|
|
893
|
+
var REGISTRY = /* @__PURE__ */ Symbol.for("splithub.analytics");
|
|
894
|
+
function registry() {
|
|
895
|
+
if (typeof window === "undefined") return null;
|
|
896
|
+
const g = globalThis;
|
|
897
|
+
return g[REGISTRY] ?? (g[REGISTRY] = /* @__PURE__ */ new Map());
|
|
898
|
+
}
|
|
899
|
+
function claimAnalytics(key, owner) {
|
|
900
|
+
const r = registry();
|
|
901
|
+
if (!r) return true;
|
|
902
|
+
const held = r.get(key);
|
|
903
|
+
if (held && held !== owner) return false;
|
|
904
|
+
r.set(key, owner);
|
|
905
|
+
return true;
|
|
906
|
+
}
|
|
907
|
+
function releaseAnalytics(key, owner) {
|
|
908
|
+
const r = registry();
|
|
909
|
+
if (r?.get(key) === owner) r.delete(key);
|
|
910
|
+
}
|
|
911
|
+
function resetAnalyticsClaims() {
|
|
912
|
+
registry()?.clear();
|
|
913
|
+
}
|
|
914
|
+
|
|
817
915
|
// src/ids.ts
|
|
818
916
|
var ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
819
917
|
function randomBytes(n) {
|
|
@@ -840,6 +938,38 @@ function newSessionId() {
|
|
|
840
938
|
return newId("s_");
|
|
841
939
|
}
|
|
842
940
|
|
|
941
|
+
// src/paths.ts
|
|
942
|
+
function normalizePath(path) {
|
|
943
|
+
return path.length > 1 && path.endsWith("/") ? path.replace(/\/+$/, "") || "/" : path;
|
|
944
|
+
}
|
|
945
|
+
function matchPath(pattern, pathname) {
|
|
946
|
+
if (pattern.endsWith("*")) return pathname.startsWith(pattern.slice(0, -1));
|
|
947
|
+
return normalizePath(pattern) === normalizePath(pathname);
|
|
948
|
+
}
|
|
949
|
+
function redirectTarget(to, currentHref) {
|
|
950
|
+
const current = new URL(currentHref);
|
|
951
|
+
const target = new URL(to, current);
|
|
952
|
+
const own = new Set(target.searchParams.keys());
|
|
953
|
+
current.searchParams.forEach((value, key) => {
|
|
954
|
+
if (!own.has(key)) target.searchParams.append(key, value);
|
|
955
|
+
});
|
|
956
|
+
if (!target.hash) target.hash = current.hash;
|
|
957
|
+
return target;
|
|
958
|
+
}
|
|
959
|
+
function referrerOrigin(referrer, siteHost) {
|
|
960
|
+
if (!referrer) return "";
|
|
961
|
+
let url;
|
|
962
|
+
try {
|
|
963
|
+
url = new URL(referrer);
|
|
964
|
+
} catch {
|
|
965
|
+
return void 0;
|
|
966
|
+
}
|
|
967
|
+
const host = url.hostname.toLowerCase().replace(/^www\./, "");
|
|
968
|
+
const site = (siteHost ?? "").toLowerCase().replace(/:\d+$/, "").replace(/^www\./, "");
|
|
969
|
+
if (!host || host === site) return void 0;
|
|
970
|
+
return `${url.protocol}//${url.host}`.slice(0, 300);
|
|
971
|
+
}
|
|
972
|
+
|
|
843
973
|
// src/visitor.ts
|
|
844
974
|
var ONE_YEAR = 60 * 60 * 24 * 365;
|
|
845
975
|
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
@@ -897,24 +1027,28 @@ function sessionExposureSet(key = "sh_exposed") {
|
|
|
897
1027
|
function persistExposureSet(set, key = "sh_exposed") {
|
|
898
1028
|
safeStorage("sessionStorage")?.setItem(key, JSON.stringify([...set]));
|
|
899
1029
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
1030
|
+
function readStored(kind, key) {
|
|
1031
|
+
try {
|
|
1032
|
+
return safeStorage(kind)?.getItem(key) ?? null;
|
|
1033
|
+
} catch {
|
|
1034
|
+
return null;
|
|
1035
|
+
}
|
|
904
1036
|
}
|
|
905
|
-
function
|
|
906
|
-
|
|
907
|
-
|
|
1037
|
+
function writeStored(kind, key, value) {
|
|
1038
|
+
try {
|
|
1039
|
+
const s = safeStorage(kind);
|
|
1040
|
+
if (value === null) s?.removeItem(key);
|
|
1041
|
+
else s?.setItem(key, value);
|
|
1042
|
+
} catch {
|
|
1043
|
+
}
|
|
908
1044
|
}
|
|
909
|
-
function
|
|
910
|
-
|
|
911
|
-
const
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
if (!target.hash) target.hash = current.hash;
|
|
917
|
-
return target;
|
|
1045
|
+
function sessionReferrer(key = "sh_ref") {
|
|
1046
|
+
if (!isBrowser()) return void 0;
|
|
1047
|
+
const stored = readStored("sessionStorage", key);
|
|
1048
|
+
if (stored !== null) return stored;
|
|
1049
|
+
const ref = referrerOrigin(document.referrer, location.host);
|
|
1050
|
+
if (ref !== void 0) writeStored("sessionStorage", key, ref);
|
|
1051
|
+
return ref;
|
|
918
1052
|
}
|
|
919
1053
|
|
|
920
1054
|
// src/redirect.ts
|
|
@@ -1145,13 +1279,22 @@ function applyRemoteOverrides(config, overrides) {
|
|
|
1145
1279
|
const addedGoals = (overrides.goals ?? []).filter((g) => g && typeof g.key === "string" && !knownGoals.has(g.key));
|
|
1146
1280
|
const holdout = !config.holdout && validHoldout(overrides.holdout) ? overrides.holdout : null;
|
|
1147
1281
|
const layers = overrides.layers && typeof overrides.layers === "object" ? { ...overrides.layers, ...config.layers ?? {} } : null;
|
|
1148
|
-
|
|
1282
|
+
const propertyPatches = overrides.properties ?? {};
|
|
1283
|
+
const knownProps = new Set((config.properties ?? []).map((p) => p.key));
|
|
1284
|
+
const addedProps = (overrides.addedProperties ?? []).filter((p) => p && typeof p.key === "string" && !knownProps.has(p.key));
|
|
1285
|
+
const propsChanged = Object.keys(propertyPatches).length > 0 || addedProps.length > 0;
|
|
1286
|
+
if (Object.keys(patches).length === 0 && added.length === 0 && addedGoals.length === 0 && !holdout && !layers && !propsChanged) return config;
|
|
1287
|
+
const properties = propsChanged ? [...config.properties ?? [], ...addedProps.map((p) => ({ ...p, goals: Array.isArray(p.goals) ? p.goals : [], origin: "dashboard" }))].map((p) => {
|
|
1288
|
+
const status = propertyPatches[p.key]?.status;
|
|
1289
|
+
return status === "running" || status === "paused" ? { ...p, status } : p;
|
|
1290
|
+
}) : config.properties;
|
|
1149
1291
|
const merged = [...config.experiments, ...added.map((e) => ({ ...e, origin: "dashboard" }))];
|
|
1150
1292
|
return {
|
|
1151
1293
|
...config,
|
|
1152
1294
|
...holdout ? { holdout } : {},
|
|
1153
1295
|
...layers ? { layers } : {},
|
|
1154
1296
|
goals: addedGoals.length ? [...config.goals, ...addedGoals] : config.goals,
|
|
1297
|
+
...properties ? { properties } : {},
|
|
1155
1298
|
experiments: merged.map((e) => {
|
|
1156
1299
|
const o = patches[e.key];
|
|
1157
1300
|
return o ? applyToExperiment(e, o) : e;
|
|
@@ -1210,6 +1353,11 @@ var SDK_VERSION = "0.1.0";
|
|
|
1210
1353
|
var REMOTE_WAIT_MS = 400;
|
|
1211
1354
|
var DEV_NOTE = "Development sends nothing to the cloud, so your own visits never reach the results. Deployed builds send as usual.";
|
|
1212
1355
|
var CLOUD_ENDPOINT = "https://go.splithub.io/v1/events";
|
|
1356
|
+
var PROPERTY_WAIT_MS = 5e3;
|
|
1357
|
+
var MAX_PROPS_BYTES = 4096;
|
|
1358
|
+
var MAX_ENGAGE_MS = 36e5;
|
|
1359
|
+
var UID_KEY = "sh_uid";
|
|
1360
|
+
var UID_SENT_KEY = "sh_uid_sent";
|
|
1213
1361
|
function resolveEndpoint(config) {
|
|
1214
1362
|
if (config.storage.mode === "self") return { endpoint: config.storage.endpoint };
|
|
1215
1363
|
return { endpoint: config.storage.endpoint ?? CLOUD_ENDPOINT, key: config.storage.key };
|
|
@@ -1224,6 +1372,7 @@ function createClient(config, options = {}) {
|
|
|
1224
1372
|
let effective = config;
|
|
1225
1373
|
let geo;
|
|
1226
1374
|
let waitFor = null;
|
|
1375
|
+
let arriving = options.remote?.arriving ?? null;
|
|
1227
1376
|
if (options.remote?.overrides) {
|
|
1228
1377
|
effective = applyRemoteOverrides(config, options.remote.overrides);
|
|
1229
1378
|
geo = options.remote.overrides.ctx;
|
|
@@ -1241,6 +1390,7 @@ function createClient(config, options = {}) {
|
|
|
1241
1390
|
if (fresh) writeRemoteCache(publicKey, fresh);
|
|
1242
1391
|
return fresh;
|
|
1243
1392
|
});
|
|
1393
|
+
arriving = fetched;
|
|
1244
1394
|
const waitMs = options.remote?.waitMs ?? REMOTE_WAIT_MS;
|
|
1245
1395
|
if (!cached && waitMs > 0 && options.remote?.overrides === void 0) waitFor = withTimeout(fetched, waitMs);
|
|
1246
1396
|
else {
|
|
@@ -1254,7 +1404,7 @@ function createClient(config, options = {}) {
|
|
|
1254
1404
|
const hadVisitor = browser && !options.visitorId ? hasVisitorId(config.visitorKey) : void 0;
|
|
1255
1405
|
const visitorId = options.visitorId ?? (browser ? getOrCreateVisitorId(config.visitorKey) : "server");
|
|
1256
1406
|
const noVisitor = !browser && !options.visitorId;
|
|
1257
|
-
const sessionId = options.sessionId ?? (browser ? getOrCreateSessionId() : "server");
|
|
1407
|
+
const sessionId = options.sessionId ?? (browser ? getOrCreateSessionId() : options.visitorId ? `srv_${murmur3(options.visitorId).toString(36)}` : "server");
|
|
1258
1408
|
const contextFor = (g) => {
|
|
1259
1409
|
if (!browser) return { ...g ?? {}, ...options.context ?? {} };
|
|
1260
1410
|
const forPage = g?.timezone ? { ...g, timezone: void 0 } : g;
|
|
@@ -1274,7 +1424,15 @@ function createClient(config, options = {}) {
|
|
|
1274
1424
|
}
|
|
1275
1425
|
const disabled = options.disabled ?? (!browser && !options.transport);
|
|
1276
1426
|
const cloud = config.storage.mode === "cloud";
|
|
1277
|
-
const cloudIdle = () => cloud && !config.experiments.some((e) => e.status === "running");
|
|
1427
|
+
const cloudIdle = () => cloud && !config.experiments.some((e) => e.status === "running") && !(config.properties ?? []).some((p) => p.status === "running");
|
|
1428
|
+
const wanted = typeof options.property === "string" ? options.property.trim().toLowerCase() || null : null;
|
|
1429
|
+
let propConfig = config;
|
|
1430
|
+
const known = () => wanted !== null && pickProperty(propConfig, wanted) !== null;
|
|
1431
|
+
const property = () => {
|
|
1432
|
+
if (!wanted) return null;
|
|
1433
|
+
const p = pickProperty(propConfig, wanted);
|
|
1434
|
+
return p && p.status === "running" ? p.key : null;
|
|
1435
|
+
};
|
|
1278
1436
|
const devLocal = cloud && isDevEnvironment();
|
|
1279
1437
|
const held = /* @__PURE__ */ new Set();
|
|
1280
1438
|
const hold = (key, kind, message) => {
|
|
@@ -1294,6 +1452,8 @@ function createClient(config, options = {}) {
|
|
|
1294
1452
|
});
|
|
1295
1453
|
const exposed = browser ? sessionExposureSet() : /* @__PURE__ */ new Set();
|
|
1296
1454
|
const currentUrl = () => browser ? location.pathname + location.search : void 0;
|
|
1455
|
+
const entryRef = browser ? sessionReferrer() : void 0;
|
|
1456
|
+
const refField = entryRef === void 0 ? {} : { ref: entryRef };
|
|
1297
1457
|
const markHoldout = (ts) => {
|
|
1298
1458
|
const group = holdoutGroup(config, visitorId);
|
|
1299
1459
|
if (!group || noVisitor) return;
|
|
@@ -1305,7 +1465,7 @@ function createClient(config, options = {}) {
|
|
|
1305
1465
|
}
|
|
1306
1466
|
exposed.add(dedupKey);
|
|
1307
1467
|
if (browser) persistExposureSet(exposed);
|
|
1308
|
-
const event = { id: newId("e_"), type: "exposure", exp: HOLDOUT_KEY, var: group, vid: visitorId, sid: sessionId, ts, url: currentUrl() };
|
|
1468
|
+
const event = { id: newId("e_"), type: "exposure", exp: HOLDOUT_KEY, var: group, vid: visitorId, sid: sessionId, ts, url: currentUrl(), ...refField };
|
|
1309
1469
|
debugLog("exposure", `${HOLDOUT_KEY} / ${group} recorded`, event);
|
|
1310
1470
|
queue.push(event);
|
|
1311
1471
|
};
|
|
@@ -1325,15 +1485,14 @@ function createClient(config, options = {}) {
|
|
|
1325
1485
|
if (exposed.has(dedupKey)) return;
|
|
1326
1486
|
exposed.add(dedupKey);
|
|
1327
1487
|
if (browser) persistExposureSet(exposed);
|
|
1328
|
-
const event = { id: newId("e_"), type: "exposure", exp: experimentKey, var: a.variant.key, vid: visitorId, sid: sessionId, ts, url: currentUrl() };
|
|
1488
|
+
const event = { id: newId("e_"), type: "exposure", exp: experimentKey, var: a.variant.key, vid: visitorId, sid: sessionId, ts, url: currentUrl(), ...refField };
|
|
1329
1489
|
debugLog("exposure", `${experimentKey} / ${a.variant.key} recorded`, event);
|
|
1330
1490
|
queue.push(event);
|
|
1331
1491
|
};
|
|
1332
|
-
const sendGoal = (
|
|
1492
|
+
const sendGoal = (name, props, ts) => {
|
|
1333
1493
|
if (isBot2()) return;
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
}
|
|
1494
|
+
const goal = goalKeyFor(config.goals, name);
|
|
1495
|
+
if (!goal) return;
|
|
1337
1496
|
if (typeof __SPLITHUB_LITE__ === "undefined" && devLocal) {
|
|
1338
1497
|
devNote("dev-events", DEV_NOTE);
|
|
1339
1498
|
hold(`g:${goal}`, "goal", `${goal} not sent: development never sends to the cloud`);
|
|
@@ -1347,21 +1506,231 @@ function createClient(config, options = {}) {
|
|
|
1347
1506
|
debugLog("goal", `${goal}${props ? " " + JSON.stringify(props) : ""}`, event);
|
|
1348
1507
|
queue.push(event);
|
|
1349
1508
|
};
|
|
1509
|
+
let propertyFetch = null;
|
|
1510
|
+
const propertyWaiting = [];
|
|
1511
|
+
const warnUnknownProperty = () => {
|
|
1512
|
+
if (wanted && !known() && (isDevEnvironment() || isDebug())) {
|
|
1513
|
+
warn(`property "${wanted}" is not in splithub.config.ts or the dashboard, so no pageviews or events are sent. Check the id (it looks like sh-x1h78w).`);
|
|
1514
|
+
}
|
|
1515
|
+
};
|
|
1516
|
+
const withProperty = (send) => {
|
|
1517
|
+
if (!wanted) return;
|
|
1518
|
+
if (!known() && propertyFetch) {
|
|
1519
|
+
propertyWaiting.push(send);
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
send();
|
|
1523
|
+
};
|
|
1524
|
+
const hashRoute = () => {
|
|
1525
|
+
if (!browser || options.trackHash === false) return null;
|
|
1526
|
+
const hash = location.hash;
|
|
1527
|
+
const route = hash.startsWith("#/") ? hash.slice(1) : hash.startsWith("#!/") ? hash.slice(2) : null;
|
|
1528
|
+
if (route === null || options.trackHash !== true && location.pathname !== "/") return null;
|
|
1529
|
+
return route.includes("?") || !location.search ? route : route + location.search;
|
|
1530
|
+
};
|
|
1531
|
+
const pageUrl = () => hashRoute() ?? location.pathname + location.search;
|
|
1532
|
+
const pagePath = () => hashRoute()?.split("?")[0] ?? location.pathname;
|
|
1533
|
+
let engage = null;
|
|
1534
|
+
const pageVisible = () => typeof document === "undefined" || document.visibilityState !== "hidden";
|
|
1535
|
+
const scrollDepth = () => {
|
|
1536
|
+
if (!browser) return 0;
|
|
1537
|
+
const doc = document.documentElement;
|
|
1538
|
+
const height = Math.max(doc.scrollHeight, document.body?.scrollHeight ?? 0);
|
|
1539
|
+
const seen = (window.scrollY || doc.scrollTop || 0) + window.innerHeight;
|
|
1540
|
+
if (!height || seen >= height) return 100;
|
|
1541
|
+
return Math.max(0, Math.min(100, Math.round(seen / height * 100)));
|
|
1542
|
+
};
|
|
1543
|
+
const emitEngage = (final) => {
|
|
1544
|
+
const e = engage;
|
|
1545
|
+
if (!e) return false;
|
|
1546
|
+
const now = Date.now();
|
|
1547
|
+
const ms = Math.min(MAX_ENGAGE_MS, Math.round(e.ms + (e.since !== null ? now - e.since : 0)));
|
|
1548
|
+
e.ms = 0;
|
|
1549
|
+
e.since = !final && pageVisible() ? now : null;
|
|
1550
|
+
if (final) engage = null;
|
|
1551
|
+
if (ms <= 0 || isBot2()) return false;
|
|
1552
|
+
const event = { id: newId("n_"), type: "engage", prop: e.prop, vid: visitorId, sid: sessionId, ts: now, url: e.url, ms, scroll: e.scroll };
|
|
1553
|
+
debugLog("pageview", `${e.prop}: ${e.url} engaged ${Math.round(ms / 1e3)} s, scrolled ${e.scroll}%`, event);
|
|
1554
|
+
queue.push(event);
|
|
1555
|
+
return true;
|
|
1556
|
+
};
|
|
1557
|
+
let sentIdentity = browser ? readStored("sessionStorage", UID_SENT_KEY) : null;
|
|
1558
|
+
const sendIdentify = (uid, traits, ts) => {
|
|
1559
|
+
if (!wanted || isBot2() || noVisitor) return;
|
|
1560
|
+
const mark = `${uid}
|
|
1561
|
+
${traits ? JSON.stringify(traits) : ""}`;
|
|
1562
|
+
withProperty(() => {
|
|
1563
|
+
const prop = property();
|
|
1564
|
+
if (!prop || isBot2() || sentIdentity === mark || browser && readStored("sessionStorage", UID_SENT_KEY) === mark) return;
|
|
1565
|
+
if (typeof __SPLITHUB_LITE__ === "undefined" && devLocal) {
|
|
1566
|
+
hold(`i:${prop}`, "pageview", `${prop}: identify not sent: development never sends to the cloud`);
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
sentIdentity = mark;
|
|
1570
|
+
if (browser) writeStored("sessionStorage", UID_SENT_KEY, mark);
|
|
1571
|
+
const event = { id: newId("i_"), type: "identify", prop, vid: visitorId, sid: sessionId, ts, uid, ...traits ? { traits } : {} };
|
|
1572
|
+
debugLog("info", `${prop}: identify ${uid}`, event);
|
|
1573
|
+
queue.push(event);
|
|
1574
|
+
});
|
|
1575
|
+
};
|
|
1576
|
+
const resendIdentity = (ts) => {
|
|
1577
|
+
const uid = browser ? readStored("localStorage", UID_KEY) : null;
|
|
1578
|
+
if (uid && !(sentIdentity ?? "").startsWith(`${uid}
|
|
1579
|
+
`)) sendIdentify(uid, void 0, ts);
|
|
1580
|
+
};
|
|
1581
|
+
const sendPageGoals = (path, ts) => {
|
|
1582
|
+
if (isBot2()) return;
|
|
1583
|
+
for (const goal of config.goals) {
|
|
1584
|
+
if (goal.type === "pageview" && goal.path && matchPath(goal.path, path)) {
|
|
1585
|
+
debugLog("pageview", `${path} matches goal ${goal.key}`);
|
|
1586
|
+
sendGoal(goal.key, void 0, ts);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
};
|
|
1590
|
+
const endpointKey = resolveEndpoint(config).endpoint;
|
|
1591
|
+
const owner = {};
|
|
1592
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
1593
|
+
const collects = (prop) => {
|
|
1594
|
+
if (claimAnalytics(`${endpointKey}|${prop}`, owner)) {
|
|
1595
|
+
claimed.add(prop);
|
|
1596
|
+
return true;
|
|
1597
|
+
}
|
|
1598
|
+
if (typeof __SPLITHUB_LITE__ === "undefined") devNote(`shared:${prop}`, `Another SplitHub script on this page already collects pageviews for "${prop}", so this one leaves them to it. Keep one install per property: the SDK or sh.js.`);
|
|
1599
|
+
hold(`s:${prop}`, "pageview", `${prop}: pageviews left to the other SplitHub script on this page`);
|
|
1600
|
+
return false;
|
|
1601
|
+
};
|
|
1602
|
+
let lastPage = null;
|
|
1603
|
+
const sendPage = (path, ts) => {
|
|
1604
|
+
if (isBot2()) return;
|
|
1605
|
+
if (!wanted || noVisitor) {
|
|
1606
|
+
sendPageGoals(path, ts);
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
const url = browser && path === pagePath() ? pageUrl() : path;
|
|
1610
|
+
const title = browser && typeof document !== "undefined" ? document.title.slice(0, 200) : "";
|
|
1611
|
+
const ref = browser && typeof document !== "undefined" ? document.referrer.slice(0, 512) : "";
|
|
1612
|
+
withProperty(() => {
|
|
1613
|
+
const prop = property();
|
|
1614
|
+
if (isBot2()) return;
|
|
1615
|
+
if (!prop) {
|
|
1616
|
+
sendPageGoals(path, ts);
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
if (lastPage && lastPage.url === url && ts - lastPage.ts < 1e3) return;
|
|
1620
|
+
lastPage = { url, ts };
|
|
1621
|
+
if (typeof __SPLITHUB_LITE__ === "undefined" && devLocal) {
|
|
1622
|
+
devNote("dev-events", DEV_NOTE);
|
|
1623
|
+
hold(`p:${prop}`, "pageview", `${prop}: pageviews not sent: development never sends to the cloud`);
|
|
1624
|
+
return;
|
|
1625
|
+
}
|
|
1626
|
+
if (!collects(prop)) return;
|
|
1627
|
+
emitEngage(true);
|
|
1628
|
+
const event = { id: newId("p_"), type: "page", prop, vid: visitorId, sid: sessionId, ts, url, ...title ? { title } : {}, ...ref ? { ref } : {} };
|
|
1629
|
+
debugLog("pageview", `${prop}: ${url}`, event);
|
|
1630
|
+
queue.push(event);
|
|
1631
|
+
if (browser) engage = { prop, url, ms: 0, since: pageVisible() ? ts : null, scroll: scrollDepth() };
|
|
1632
|
+
resendIdentity(ts);
|
|
1633
|
+
});
|
|
1634
|
+
};
|
|
1635
|
+
const sendTracked = (name, props, ts) => {
|
|
1636
|
+
if (isBot2()) return;
|
|
1637
|
+
if (!wanted || noVisitor) {
|
|
1638
|
+
sendGoal(name, props, ts);
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
if (!EVENT_NAME_PATTERN.test(name)) {
|
|
1642
|
+
warn(`event name "${name}" is invalid: letters, digits, spaces and "_ . : / -", up to 64 characters.`);
|
|
1643
|
+
sendGoal(name, props, ts);
|
|
1644
|
+
return;
|
|
1645
|
+
}
|
|
1646
|
+
const url = currentUrl();
|
|
1647
|
+
withProperty(() => {
|
|
1648
|
+
const prop = property();
|
|
1649
|
+
if (isBot2()) return;
|
|
1650
|
+
if (!prop) {
|
|
1651
|
+
sendGoal(name, props, ts);
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
if (typeof __SPLITHUB_LITE__ === "undefined" && devLocal) {
|
|
1655
|
+
hold(`v:${prop}:${name}`, "goal", `${prop}: event ${name} not sent: development never sends to the cloud`);
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1658
|
+
const event = { id: newId("v_"), type: "event", prop, name, vid: visitorId, sid: sessionId, ts, url, ...props ? { props } : {} };
|
|
1659
|
+
debugLog("goal", `${prop}: event ${name}${props ? " " + JSON.stringify(props) : ""}`, event);
|
|
1660
|
+
queue.push(event);
|
|
1661
|
+
});
|
|
1662
|
+
};
|
|
1663
|
+
const emit = (name, props) => {
|
|
1664
|
+
if (disabled || isBot2()) return;
|
|
1665
|
+
const ts = Date.now();
|
|
1666
|
+
props = fitProps(props, `event "${name}"`);
|
|
1667
|
+
if (pending) void ready.then(() => sendTracked(name, props, ts));
|
|
1668
|
+
else sendTracked(name, props, ts);
|
|
1669
|
+
};
|
|
1670
|
+
let unwatch = null;
|
|
1671
|
+
const watch = () => {
|
|
1672
|
+
if (unwatch || !browser || options.trackNavigation === false) return;
|
|
1673
|
+
let last = pageUrl();
|
|
1674
|
+
unwatch = onNavigation(() => {
|
|
1675
|
+
const now = pageUrl();
|
|
1676
|
+
if (now === last) return;
|
|
1677
|
+
last = now;
|
|
1678
|
+
client.pageview();
|
|
1679
|
+
});
|
|
1680
|
+
};
|
|
1681
|
+
let unbindPage = null;
|
|
1682
|
+
const bindPage = () => {
|
|
1683
|
+
if (unbindPage || !browser || !wanted || disabled) return;
|
|
1684
|
+
const onScroll = () => {
|
|
1685
|
+
if (engage) engage.scroll = Math.max(engage.scroll, scrollDepth());
|
|
1686
|
+
};
|
|
1687
|
+
const leave = () => {
|
|
1688
|
+
if (emitEngage(false)) void queue.flush(true);
|
|
1689
|
+
if (engage) engage.since = null;
|
|
1690
|
+
};
|
|
1691
|
+
const onVisibility = () => {
|
|
1692
|
+
if (document.visibilityState === "hidden") leave();
|
|
1693
|
+
else if (engage && engage.since === null) engage.since = Date.now();
|
|
1694
|
+
};
|
|
1695
|
+
const onShow = () => {
|
|
1696
|
+
if (engage && engage.since === null && pageVisible()) engage.since = Date.now();
|
|
1697
|
+
};
|
|
1698
|
+
addEventListener("scroll", onScroll, { passive: true });
|
|
1699
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
1700
|
+
addEventListener("pagehide", leave);
|
|
1701
|
+
addEventListener("pageshow", onShow);
|
|
1702
|
+
unbindPage = () => {
|
|
1703
|
+
removeEventListener("scroll", onScroll);
|
|
1704
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
1705
|
+
removeEventListener("pagehide", leave);
|
|
1706
|
+
removeEventListener("pageshow", onShow);
|
|
1707
|
+
};
|
|
1708
|
+
};
|
|
1350
1709
|
const ready = waitFor ? waitFor.then((fresh) => {
|
|
1351
1710
|
if (fresh) {
|
|
1352
1711
|
config = applyRemoteOverrides(base, fresh);
|
|
1712
|
+
propConfig = config;
|
|
1353
1713
|
context = contextFor(fresh.ctx);
|
|
1354
1714
|
debugLog("remote", `first visit: overrides v${fresh.version} arrived, experiments decided`, { experiments: fresh.experiments });
|
|
1355
1715
|
} else {
|
|
1356
1716
|
debugLog("remote", "first visit: overrides did not arrive in time; experiments follow splithub.config.ts");
|
|
1357
1717
|
}
|
|
1358
1718
|
pending = false;
|
|
1359
|
-
for (const w of waiting)
|
|
1360
|
-
for (const w of waiting) if ("goal" in w) sendGoal(w.goal, w.props, w.ts);
|
|
1719
|
+
for (const w of waiting) sendExposure(w.exp, w.ts);
|
|
1361
1720
|
waiting.length = 0;
|
|
1362
1721
|
for (const listener of [...listeners]) listener();
|
|
1363
1722
|
listeners.clear();
|
|
1364
1723
|
}) : Promise.resolve();
|
|
1724
|
+
if (wanted && !known()) {
|
|
1725
|
+
if (arriving) {
|
|
1726
|
+
propertyFetch = withTimeout(arriving, PROPERTY_WAIT_MS).then((fresh) => {
|
|
1727
|
+
if (fresh) propConfig = applyRemoteOverrides(base, fresh);
|
|
1728
|
+
propertyFetch = null;
|
|
1729
|
+
warnUnknownProperty();
|
|
1730
|
+
for (const send of propertyWaiting.splice(0)) send();
|
|
1731
|
+
});
|
|
1732
|
+
} else warnUnknownProperty();
|
|
1733
|
+
}
|
|
1365
1734
|
const decided = /* @__PURE__ */ new Set();
|
|
1366
1735
|
const client = {
|
|
1367
1736
|
get config() {
|
|
@@ -1396,7 +1765,7 @@ function createClient(config, options = {}) {
|
|
|
1396
1765
|
exposure(experimentKey) {
|
|
1397
1766
|
if (disabled) return;
|
|
1398
1767
|
if (pending) {
|
|
1399
|
-
if (!waiting.some((w) =>
|
|
1768
|
+
if (!waiting.some((w) => w.exp === experimentKey)) waiting.push({ exp: experimentKey, ts: Date.now() });
|
|
1400
1769
|
return;
|
|
1401
1770
|
}
|
|
1402
1771
|
sendExposure(experimentKey, Date.now());
|
|
@@ -1421,28 +1790,37 @@ function createClient(config, options = {}) {
|
|
|
1421
1790
|
client.exposure(experimentKey);
|
|
1422
1791
|
return (a.variant ?? experiment.variants[0]).value;
|
|
1423
1792
|
},
|
|
1424
|
-
track(
|
|
1425
|
-
|
|
1426
|
-
if (pending) waiting.push({ goal, ...props ? { props } : {}, ts: Date.now() });
|
|
1427
|
-
else sendGoal(goal, props, Date.now());
|
|
1793
|
+
track(name, props) {
|
|
1794
|
+
emit(name, props);
|
|
1428
1795
|
},
|
|
1429
1796
|
pageview(path) {
|
|
1430
|
-
const current = path ?? (browser ?
|
|
1797
|
+
const current = path ?? (browser ? pagePath() : void 0);
|
|
1431
1798
|
if (!current) return;
|
|
1799
|
+
if (path === void 0) watch();
|
|
1800
|
+
bindPage();
|
|
1801
|
+
if (disabled) return;
|
|
1432
1802
|
if (pending) {
|
|
1433
1803
|
const ts = Date.now();
|
|
1434
|
-
void ready.then(() =>
|
|
1435
|
-
if (disabled) return;
|
|
1436
|
-
for (const goal of config.goals) if (goal.type === "pageview" && goal.path && matchPath(goal.path, current)) sendGoal(goal.key, void 0, ts);
|
|
1437
|
-
});
|
|
1804
|
+
void ready.then(() => sendPage(current, ts));
|
|
1438
1805
|
return;
|
|
1439
1806
|
}
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1807
|
+
sendPage(current, Date.now());
|
|
1808
|
+
},
|
|
1809
|
+
identify(uid, traits) {
|
|
1810
|
+
if (uid === null) {
|
|
1811
|
+
writeStored("localStorage", UID_KEY, null);
|
|
1812
|
+
writeStored("sessionStorage", UID_SENT_KEY, null);
|
|
1813
|
+
sentIdentity = null;
|
|
1814
|
+
return;
|
|
1815
|
+
}
|
|
1816
|
+
const id = typeof uid === "string" ? uid.trim() : "";
|
|
1817
|
+
if (!id || id.length > 128) {
|
|
1818
|
+
warn("identify() needs a user id of 1 to 128 characters.");
|
|
1819
|
+
return;
|
|
1445
1820
|
}
|
|
1821
|
+
if (browser) writeStored("localStorage", UID_KEY, id);
|
|
1822
|
+
if (disabled || isBot2()) return;
|
|
1823
|
+
sendIdentify(id, cleanTraits(traits), Date.now());
|
|
1446
1824
|
},
|
|
1447
1825
|
autoGoals(root) {
|
|
1448
1826
|
if (!browser) return () => void 0;
|
|
@@ -1465,7 +1843,15 @@ function createClient(config, options = {}) {
|
|
|
1465
1843
|
flush: () => queue.flush(false),
|
|
1466
1844
|
// Unbinds the page listeners; pending events are kept and a later event binds again,
|
|
1467
1845
|
// so a provider may destroy on unmount even under StrictMode's double mount.
|
|
1468
|
-
destroy: () =>
|
|
1846
|
+
destroy: () => {
|
|
1847
|
+
queue.destroy();
|
|
1848
|
+
unwatch?.();
|
|
1849
|
+
unwatch = null;
|
|
1850
|
+
unbindPage?.();
|
|
1851
|
+
unbindPage = null;
|
|
1852
|
+
for (const prop of claimed) releaseAnalytics(`${endpointKey}|${prop}`, owner);
|
|
1853
|
+
claimed.clear();
|
|
1854
|
+
}
|
|
1469
1855
|
};
|
|
1470
1856
|
if (browser && isDebug()) {
|
|
1471
1857
|
window.__splithub = client;
|
|
@@ -1473,6 +1859,25 @@ function createClient(config, options = {}) {
|
|
|
1473
1859
|
}
|
|
1474
1860
|
return client;
|
|
1475
1861
|
}
|
|
1862
|
+
var navListeners = /* @__PURE__ */ new Set();
|
|
1863
|
+
var historyWrapped = false;
|
|
1864
|
+
function onNavigation(listener) {
|
|
1865
|
+
if (!historyWrapped && typeof history !== "undefined") {
|
|
1866
|
+
historyWrapped = true;
|
|
1867
|
+
for (const method of ["pushState", "replaceState"]) {
|
|
1868
|
+
const original = history[method];
|
|
1869
|
+
history[method] = function(...args) {
|
|
1870
|
+
const out = original.apply(this, args);
|
|
1871
|
+
setTimeout(() => navListeners.forEach((l) => l()), 0);
|
|
1872
|
+
return out;
|
|
1873
|
+
};
|
|
1874
|
+
}
|
|
1875
|
+
addEventListener("popstate", () => navListeners.forEach((l) => l()));
|
|
1876
|
+
addEventListener("hashchange", () => navListeners.forEach((l) => l()));
|
|
1877
|
+
}
|
|
1878
|
+
navListeners.add(listener);
|
|
1879
|
+
return () => void navListeners.delete(listener);
|
|
1880
|
+
}
|
|
1476
1881
|
function stripUndefined(o) {
|
|
1477
1882
|
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0));
|
|
1478
1883
|
}
|
|
@@ -1491,6 +1896,53 @@ function withTimeout(p, ms) {
|
|
|
1491
1896
|
);
|
|
1492
1897
|
});
|
|
1493
1898
|
}
|
|
1899
|
+
function fitProps(props, what) {
|
|
1900
|
+
if (!props || typeof props !== "object") return void 0;
|
|
1901
|
+
let json;
|
|
1902
|
+
try {
|
|
1903
|
+
json = JSON.stringify(props);
|
|
1904
|
+
} catch {
|
|
1905
|
+
warn(`${what}: props cannot be sent as JSON, so they were left out.`);
|
|
1906
|
+
return void 0;
|
|
1907
|
+
}
|
|
1908
|
+
if (json.length <= MAX_PROPS_BYTES) return props;
|
|
1909
|
+
const out = {};
|
|
1910
|
+
let size = 2;
|
|
1911
|
+
const entries = Object.entries(props).sort(([a], [b]) => a === "revenue" ? -1 : b === "revenue" ? 1 : 0);
|
|
1912
|
+
for (const [key, value] of entries) {
|
|
1913
|
+
if (value !== null && typeof value === "object") continue;
|
|
1914
|
+
const part = JSON.stringify({ [key]: value }).length - 1;
|
|
1915
|
+
if (size + part > MAX_PROPS_BYTES) continue;
|
|
1916
|
+
out[key] = value;
|
|
1917
|
+
size += part;
|
|
1918
|
+
}
|
|
1919
|
+
const kept = Object.keys(out);
|
|
1920
|
+
warn(`${what}: props are larger than 4 KB, so ${kept.length ? `only ${kept.join(", ")} went out` : "none went out"}.`);
|
|
1921
|
+
return kept.length ? out : void 0;
|
|
1922
|
+
}
|
|
1923
|
+
function cleanTraits(traits) {
|
|
1924
|
+
if (!traits || typeof traits !== "object") return void 0;
|
|
1925
|
+
const out = {};
|
|
1926
|
+
let size = 2;
|
|
1927
|
+
let skipped = false;
|
|
1928
|
+
for (const [key, value] of Object.entries(traits)) {
|
|
1929
|
+
const plain = typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
1930
|
+
if (!plain || key.length > 64) {
|
|
1931
|
+
if (value !== void 0) skipped = true;
|
|
1932
|
+
continue;
|
|
1933
|
+
}
|
|
1934
|
+
const v = typeof value === "string" ? value.slice(0, 256) : value;
|
|
1935
|
+
const part = JSON.stringify({ [key]: v }).length - 1;
|
|
1936
|
+
if (size + part > MAX_PROPS_BYTES) {
|
|
1937
|
+
skipped = true;
|
|
1938
|
+
continue;
|
|
1939
|
+
}
|
|
1940
|
+
out[key] = v;
|
|
1941
|
+
size += part;
|
|
1942
|
+
}
|
|
1943
|
+
if (skipped) warn("identify(): traits keep text, numbers and true/false up to 4 KB; the rest was left out.");
|
|
1944
|
+
return Object.keys(out).length ? out : void 0;
|
|
1945
|
+
}
|
|
1494
1946
|
var warned = /* @__PURE__ */ new Set();
|
|
1495
1947
|
function warn(message) {
|
|
1496
1948
|
if (warned.has(message)) return;
|
|
@@ -1505,6 +1957,7 @@ function warnUnknown(key) {
|
|
|
1505
1957
|
CLOUD_ENDPOINT,
|
|
1506
1958
|
ConfigError,
|
|
1507
1959
|
EMPTY_OVERRIDES,
|
|
1960
|
+
EVENT_NAME_PATTERN,
|
|
1508
1961
|
EventQueue,
|
|
1509
1962
|
HOLDOUT_KEY,
|
|
1510
1963
|
KEYED_FIELDS,
|
|
@@ -1512,6 +1965,7 @@ function warnUnknown(key) {
|
|
|
1512
1965
|
MVT_MAX_COMBINATIONS,
|
|
1513
1966
|
MVT_MAX_FACTORS,
|
|
1514
1967
|
MVT_MAX_LEVELS,
|
|
1968
|
+
PROPERTY_KEY_PATTERN,
|
|
1515
1969
|
REMOTE_TTL_MS,
|
|
1516
1970
|
REMOTE_WAIT_MS,
|
|
1517
1971
|
SDK_VERSION,
|
|
@@ -1531,11 +1985,13 @@ function warnUnknown(key) {
|
|
|
1531
1985
|
defineConfig,
|
|
1532
1986
|
devNote,
|
|
1533
1987
|
evaluateCondition,
|
|
1988
|
+
eventKey,
|
|
1534
1989
|
explainAssignment,
|
|
1535
1990
|
fetchRemoteOverrides,
|
|
1536
1991
|
getExperiment,
|
|
1537
1992
|
getOrCreateSessionId,
|
|
1538
1993
|
getOrCreateVisitorId,
|
|
1994
|
+
goalKeyFor,
|
|
1539
1995
|
hasVisitorId,
|
|
1540
1996
|
hashToUnit,
|
|
1541
1997
|
holdoutExperiment,
|
|
@@ -1558,16 +2014,21 @@ function warnUnknown(key) {
|
|
|
1558
2014
|
murmur3,
|
|
1559
2015
|
mvtVariants,
|
|
1560
2016
|
newId,
|
|
2017
|
+
newPropertyId,
|
|
1561
2018
|
newSessionId,
|
|
1562
2019
|
newVisitorId,
|
|
1563
2020
|
normalizePath,
|
|
2021
|
+
normalizeProperty,
|
|
1564
2022
|
onDebug,
|
|
1565
2023
|
parseCookies,
|
|
1566
2024
|
parseForceParam,
|
|
1567
2025
|
parseUserAgent,
|
|
2026
|
+
pickProperty,
|
|
1568
2027
|
pickVariant,
|
|
1569
2028
|
readRemoteCache,
|
|
1570
2029
|
redirectTarget,
|
|
2030
|
+
referrerOrigin,
|
|
2031
|
+
resetAnalyticsClaims,
|
|
1571
2032
|
resetDevNotes,
|
|
1572
2033
|
resolveOverridesUrl,
|
|
1573
2034
|
runFlow,
|