@buildinams/tracking 0.0.0-stage → 0.2.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 +267 -2
- package/dist/chunk-KLN5K6PR.js +6 -0
- package/dist/chunk-KLN5K6PR.js.map +1 -0
- package/dist/compatibility-Dc1JmTeG.d.ts +35 -0
- package/dist/consent/cookiebot/index.d.ts +5 -0
- package/dist/consent/cookiebot/index.js +27 -0
- package/dist/consent/cookiebot/index.js.map +1 -0
- package/dist/consent/index.d.ts +18 -0
- package/dist/consent/index.js +3 -0
- package/dist/consent/index.js.map +1 -0
- package/dist/consent/onetrust/index.d.ts +9 -0
- package/dist/consent/onetrust/index.js +35 -0
- package/dist/consent/onetrust/index.js.map +1 -0
- package/dist/consent/shopify/index.d.ts +13 -0
- package/dist/consent/shopify/index.js +93 -0
- package/dist/consent/shopify/index.js.map +1 -0
- package/dist/destinations/ga4/index.d.ts +16 -0
- package/dist/destinations/ga4/index.js +15 -0
- package/dist/destinations/ga4/index.js.map +1 -0
- package/dist/destinations/index.d.ts +9 -0
- package/dist/destinations/index.js +17 -0
- package/dist/destinations/index.js.map +1 -0
- package/dist/destinations/shopify/index.d.ts +12 -0
- package/dist/destinations/shopify/index.js +18 -0
- package/dist/destinations/shopify/index.js.map +1 -0
- package/dist/events/index.d.ts +16 -0
- package/dist/events/index.js +77 -0
- package/dist/events/index.js.map +1 -0
- package/dist/flow/index.d.ts +16 -0
- package/dist/flow/index.js +82 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/page/index.d.ts +27 -0
- package/dist/page/index.js +56 -0
- package/dist/page/index.js.map +1 -0
- package/dist/sources/cart/index.d.ts +27 -0
- package/dist/sources/cart/index.js +38 -0
- package/dist/sources/cart/index.js.map +1 -0
- package/dist/sources/cart/shopify/index.d.ts +28 -0
- package/dist/sources/cart/shopify/index.js +16 -0
- package/dist/sources/cart/shopify/index.js.map +1 -0
- package/dist/sources/dom/index.d.ts +202 -0
- package/dist/sources/dom/index.js +597 -0
- package/dist/sources/dom/index.js.map +1 -0
- package/dist/sources/index.d.ts +6 -0
- package/dist/sources/index.js +3 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/types-BfJsv8AV.d.ts +30 -0
- package/dist/types-DfPz7mBl.d.ts +49 -0
- package/docs/decisions/001-vendor-neutral-event-pipeline.md +33 -0
- package/docs/events.md +187 -0
- package/package.json +141 -5
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import { IS_DEVELOPMENT } from '../../chunk-KLN5K6PR.js';
|
|
2
|
+
|
|
3
|
+
// src/sources/dom/annotations.ts
|
|
4
|
+
var ANNOTATION_ATTRIBUTE = "data-e";
|
|
5
|
+
var DATA_ATTRIBUTE = "data-e-data";
|
|
6
|
+
var ROLES = ["location", "list", "item", "promotion", "form"];
|
|
7
|
+
var wordSelector = (word) => `[${ANNOTATION_ATTRIBUTE}~="${word}"]`;
|
|
8
|
+
var roleSelector = (role) => wordSelector(role);
|
|
9
|
+
var flagSelector = (flag) => wordSelector(flag);
|
|
10
|
+
var ANY_ROLE = ROLES.map(roleSelector).join(", ");
|
|
11
|
+
var hasWord = (element, word) => (element.getAttribute(ANNOTATION_ATTRIBUTE) ?? "").split(/\s+/).includes(word);
|
|
12
|
+
var splitParts = (value) => {
|
|
13
|
+
const parts = [];
|
|
14
|
+
let part = "";
|
|
15
|
+
for (let index = 0; index < value.length; index++) {
|
|
16
|
+
const char = value[index];
|
|
17
|
+
const next = value[index + 1];
|
|
18
|
+
if (char === "\\" && (next === ";" || next === "\\")) {
|
|
19
|
+
part += next;
|
|
20
|
+
index++;
|
|
21
|
+
} else if (char === ";") {
|
|
22
|
+
parts.push(part);
|
|
23
|
+
part = "";
|
|
24
|
+
} else part += char;
|
|
25
|
+
}
|
|
26
|
+
parts.push(part);
|
|
27
|
+
return parts;
|
|
28
|
+
};
|
|
29
|
+
var parse = (value) => splitParts(value ?? "").map((part) => part.trim()).filter(Boolean).map((part) => {
|
|
30
|
+
const colon = part.indexOf(":");
|
|
31
|
+
return colon === -1 ? { key: part, value: void 0 } : {
|
|
32
|
+
key: part.slice(0, colon).trim(),
|
|
33
|
+
value: part.slice(colon + 1).trim()
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
var readData = (element) => {
|
|
37
|
+
const data = {};
|
|
38
|
+
for (const { key, value } of parse(element.getAttribute(DATA_ATTRIBUTE)))
|
|
39
|
+
if (value !== void 0) data[key] = value;
|
|
40
|
+
const fields = element.querySelectorAll(
|
|
41
|
+
`[${DATA_ATTRIBUTE}]:not([${ANNOTATION_ATTRIBUTE}])`
|
|
42
|
+
);
|
|
43
|
+
for (const field of fields) {
|
|
44
|
+
if (field.parentElement?.closest(ANY_ROLE) !== element) continue;
|
|
45
|
+
for (const { key, value } of parse(field.getAttribute(DATA_ATTRIBUTE)))
|
|
46
|
+
data[key] = value ?? field.textContent?.trim() ?? "";
|
|
47
|
+
}
|
|
48
|
+
return data;
|
|
49
|
+
};
|
|
50
|
+
var readAnnotated = (element) => {
|
|
51
|
+
const composed = {};
|
|
52
|
+
for (const role of ROLES) {
|
|
53
|
+
if (hasWord(element, role)) continue;
|
|
54
|
+
const surrounding2 = element.parentElement?.closest(roleSelector(role));
|
|
55
|
+
if (surrounding2) composed[role] = { data: readData(surrounding2) };
|
|
56
|
+
}
|
|
57
|
+
return { data: readData(element), composed };
|
|
58
|
+
};
|
|
59
|
+
var surrounding = (element, role) => element.parentElement?.closest(roleSelector(role)) ?? void 0;
|
|
60
|
+
var indexInList = (item, list) => [...list.querySelectorAll(roleSelector("item"))].filter((candidate) => surrounding(candidate, "list") === list).indexOf(item);
|
|
61
|
+
var INVALID_KEY = /[\s:;\\]/;
|
|
62
|
+
var escapeValue = (value) => value.replaceAll("\\", "\\\\").replaceAll(";", "\\;");
|
|
63
|
+
var annotate = (words, data) => {
|
|
64
|
+
const fields = [];
|
|
65
|
+
for (const [key, value] of Object.entries(data ?? {})) {
|
|
66
|
+
if (INVALID_KEY.test(key)) {
|
|
67
|
+
if (IS_DEVELOPMENT)
|
|
68
|
+
console.warn(
|
|
69
|
+
`[tracking] Annotation key "${key}" left out: keys can't contain whitespace, ":", ";" or "\\".`
|
|
70
|
+
);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (value != null) fields.push(`${key}:${escapeValue(String(value))}`);
|
|
74
|
+
}
|
|
75
|
+
const annotation = {
|
|
76
|
+
"data-e": (typeof words === "string" ? [words] : words).join(" ")
|
|
77
|
+
};
|
|
78
|
+
if (fields.length) annotation["data-e-data"] = fields.join(";");
|
|
79
|
+
return annotation;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/sources/dom/opt-in.ts
|
|
83
|
+
var AUTO_ATTRIBUTE = "data-e-auto";
|
|
84
|
+
var INTERACTIVE = 'a[href], button, [role="button"], input[type="submit"], input[type="button"]';
|
|
85
|
+
var isOptedIn = (element, { flag, kind, fallback }) => {
|
|
86
|
+
if (flag && hasWord(element, flag)) return true;
|
|
87
|
+
const scope = element.closest(`[${AUTO_ATTRIBUTE}]`);
|
|
88
|
+
if (!scope) return fallback;
|
|
89
|
+
return (scope.getAttribute(AUTO_ATTRIBUTE) ?? "").split(/\s+/).includes(kind);
|
|
90
|
+
};
|
|
91
|
+
var readText = (element) => (element.getAttribute("aria-label") ?? (element instanceof HTMLInputElement ? element.value : element.textContent) ?? "").replace(/\s+/g, " ").trim().slice(0, 100);
|
|
92
|
+
var compact = (data) => Object.fromEntries(
|
|
93
|
+
Object.entries(data).filter(
|
|
94
|
+
([, value]) => value !== void 0 && value !== ""
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// src/sources/dom/clicks.ts
|
|
99
|
+
var CLICKABLE = `${INTERACTIVE}, ${flagSelector("click")}`;
|
|
100
|
+
var DOWNLOAD_EXTENSIONS = [
|
|
101
|
+
"pdf",
|
|
102
|
+
"xls",
|
|
103
|
+
"xlsx",
|
|
104
|
+
"doc",
|
|
105
|
+
"docx",
|
|
106
|
+
"txt",
|
|
107
|
+
"rtf",
|
|
108
|
+
"csv",
|
|
109
|
+
"exe",
|
|
110
|
+
"key",
|
|
111
|
+
"pps",
|
|
112
|
+
"ppt",
|
|
113
|
+
"pptx",
|
|
114
|
+
"7z",
|
|
115
|
+
"pkg",
|
|
116
|
+
"rar",
|
|
117
|
+
"gz",
|
|
118
|
+
"zip",
|
|
119
|
+
"avi",
|
|
120
|
+
"mov",
|
|
121
|
+
"mp4",
|
|
122
|
+
"mpg",
|
|
123
|
+
"mpeg",
|
|
124
|
+
"wmv",
|
|
125
|
+
"mid",
|
|
126
|
+
"midi",
|
|
127
|
+
"mp3",
|
|
128
|
+
"wav",
|
|
129
|
+
"wma"
|
|
130
|
+
];
|
|
131
|
+
var readLink = (link, {
|
|
132
|
+
internalHosts,
|
|
133
|
+
downloadExtensions
|
|
134
|
+
}) => {
|
|
135
|
+
const url = new URL(link.href, location.href);
|
|
136
|
+
const web = url.protocol === "http:" || url.protocol === "https:";
|
|
137
|
+
const fileName = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
138
|
+
const extension = fileName.includes(".") ? fileName.split(".").pop().toLowerCase() : "";
|
|
139
|
+
const isDownload = link.hasAttribute("download") || downloadExtensions.includes(extension);
|
|
140
|
+
return compact({
|
|
141
|
+
url: link.href,
|
|
142
|
+
text: readText(link),
|
|
143
|
+
outbound: web && url.hostname !== location.hostname && !internalHosts.includes(url.hostname),
|
|
144
|
+
download: isDownload ? { name: link.getAttribute("download") || fileName, extension } : void 0
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
var clicks = ({
|
|
148
|
+
auto = {},
|
|
149
|
+
internalHosts = [],
|
|
150
|
+
downloadExtensions = DOWNLOAD_EXTENSIONS
|
|
151
|
+
} = {}) => ({
|
|
152
|
+
events: ["link:click", "button:click"],
|
|
153
|
+
start({ publish }) {
|
|
154
|
+
const onClick = (event) => {
|
|
155
|
+
if (!(event.target instanceof Element)) return;
|
|
156
|
+
const element = event.target.closest(CLICKABLE);
|
|
157
|
+
if (!element) return;
|
|
158
|
+
const isLink = element instanceof HTMLAnchorElement && !!element.href;
|
|
159
|
+
const tracked = isOptedIn(element, {
|
|
160
|
+
flag: "click",
|
|
161
|
+
kind: isLink ? "links" : "buttons",
|
|
162
|
+
fallback: (isLink ? auto.links : auto.buttons) ?? false
|
|
163
|
+
});
|
|
164
|
+
if (!tracked) return;
|
|
165
|
+
const { data, composed } = readAnnotated(element);
|
|
166
|
+
if (isLink)
|
|
167
|
+
publish("link:click", {
|
|
168
|
+
data: {
|
|
169
|
+
...readLink(element, { internalHosts, downloadExtensions }),
|
|
170
|
+
...data
|
|
171
|
+
},
|
|
172
|
+
composed
|
|
173
|
+
});
|
|
174
|
+
else
|
|
175
|
+
publish("button:click", {
|
|
176
|
+
data: { text: readText(element), ...data },
|
|
177
|
+
composed
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
document.addEventListener("click", onClick, { capture: true });
|
|
181
|
+
return () => document.removeEventListener("click", onClick, { capture: true });
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// src/sources/dom/create-dom-source.ts
|
|
186
|
+
var createDomSource = (broker, detectors, { currentPage }) => {
|
|
187
|
+
const flush = () => {
|
|
188
|
+
for (const detector of detectors) detector.flush?.();
|
|
189
|
+
};
|
|
190
|
+
const context = {
|
|
191
|
+
publish: (name, { data, composed }) => broker.publish(name, data, composed)
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
start() {
|
|
195
|
+
const onVisibilityChange = () => {
|
|
196
|
+
if (document.visibilityState === "hidden") flush();
|
|
197
|
+
};
|
|
198
|
+
const onLinkClick = (event) => {
|
|
199
|
+
if (event.target instanceof Element && event.target.closest("a[href]"))
|
|
200
|
+
flush();
|
|
201
|
+
};
|
|
202
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
203
|
+
document.addEventListener("click", onLinkClick, { capture: true });
|
|
204
|
+
const stops = detectors.map((detector) => detector.start(context));
|
|
205
|
+
const unsubscribe = currentPage.subscribe({
|
|
206
|
+
leave: flush,
|
|
207
|
+
enter: () => {
|
|
208
|
+
for (const detector of detectors) detector.reset?.();
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
return () => {
|
|
212
|
+
unsubscribe();
|
|
213
|
+
flush();
|
|
214
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
215
|
+
document.removeEventListener("click", onLinkClick, { capture: true });
|
|
216
|
+
for (const stop of stops) stop();
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
flush
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/sources/dom/detail-views.ts
|
|
224
|
+
var SELECTOR = `${roleSelector("item")}${flagSelector("detail")}`;
|
|
225
|
+
var detailViews = () => {
|
|
226
|
+
let reported = /* @__PURE__ */ new WeakMap();
|
|
227
|
+
return {
|
|
228
|
+
events: ["item:view"],
|
|
229
|
+
start({ publish }) {
|
|
230
|
+
let scheduled = false;
|
|
231
|
+
const scan = () => {
|
|
232
|
+
scheduled = false;
|
|
233
|
+
for (const element of document.querySelectorAll(SELECTOR)) {
|
|
234
|
+
const annotated = readAnnotated(element);
|
|
235
|
+
const key = JSON.stringify(annotated.data);
|
|
236
|
+
if (reported.get(element) === key) continue;
|
|
237
|
+
reported.set(element, key);
|
|
238
|
+
publish("item:view", annotated);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const schedule = () => {
|
|
242
|
+
if (scheduled) return;
|
|
243
|
+
scheduled = true;
|
|
244
|
+
queueMicrotask(scan);
|
|
245
|
+
};
|
|
246
|
+
const mutations = new MutationObserver(schedule);
|
|
247
|
+
mutations.observe(document.documentElement, {
|
|
248
|
+
childList: true,
|
|
249
|
+
subtree: true,
|
|
250
|
+
characterData: true,
|
|
251
|
+
attributes: true,
|
|
252
|
+
attributeFilter: [DATA_ATTRIBUTE, ANNOTATION_ATTRIBUTE]
|
|
253
|
+
});
|
|
254
|
+
scan();
|
|
255
|
+
return () => {
|
|
256
|
+
mutations.disconnect();
|
|
257
|
+
reported = /* @__PURE__ */ new WeakMap();
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/sources/dom/forms.ts
|
|
264
|
+
var forms = ({ auto = false } = {}) => {
|
|
265
|
+
let started = /* @__PURE__ */ new WeakSet();
|
|
266
|
+
const resolve = (target) => {
|
|
267
|
+
if (!(target instanceof Element)) return void 0;
|
|
268
|
+
const annotated = target.closest(roleSelector("form"));
|
|
269
|
+
const form = target.closest("form");
|
|
270
|
+
const element = annotated ?? form;
|
|
271
|
+
if (!element) return void 0;
|
|
272
|
+
if (!annotated && !isOptedIn(element, { kind: "forms", fallback: auto }))
|
|
273
|
+
return void 0;
|
|
274
|
+
const { data, composed } = readAnnotated(element);
|
|
275
|
+
const base = compact({
|
|
276
|
+
id: form?.id,
|
|
277
|
+
name: form?.getAttribute("name") ?? void 0,
|
|
278
|
+
destination: form?.action
|
|
279
|
+
});
|
|
280
|
+
return { element, data: { ...base, ...data }, composed };
|
|
281
|
+
};
|
|
282
|
+
return {
|
|
283
|
+
events: ["form:start", "form:submit"],
|
|
284
|
+
start({ publish }) {
|
|
285
|
+
const onInteraction = (event) => {
|
|
286
|
+
const form = resolve(event.target);
|
|
287
|
+
if (!form || started.has(form.element)) return;
|
|
288
|
+
started.add(form.element);
|
|
289
|
+
publish("form:start", form);
|
|
290
|
+
};
|
|
291
|
+
const onSubmit = (event) => {
|
|
292
|
+
const form = resolve(event.target);
|
|
293
|
+
if (!form) return;
|
|
294
|
+
const submitText = event.submitter ? readText(event.submitter) : void 0;
|
|
295
|
+
publish("form:submit", {
|
|
296
|
+
data: { ...compact({ submitText }), ...form.data },
|
|
297
|
+
composed: form.composed
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
const options = { capture: true };
|
|
301
|
+
document.addEventListener("input", onInteraction, options);
|
|
302
|
+
document.addEventListener("change", onInteraction, options);
|
|
303
|
+
document.addEventListener("submit", onSubmit, options);
|
|
304
|
+
return () => {
|
|
305
|
+
document.removeEventListener("input", onInteraction, options);
|
|
306
|
+
document.removeEventListener("change", onInteraction, options);
|
|
307
|
+
document.removeEventListener("submit", onSubmit, options);
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
reset() {
|
|
311
|
+
started = /* @__PURE__ */ new WeakSet();
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// src/sources/dom/impressions.ts
|
|
317
|
+
var OBSERVED = [roleSelector("promotion"), roleSelector("item")].join(", ");
|
|
318
|
+
var impressions = ({
|
|
319
|
+
threshold = 0.5,
|
|
320
|
+
delay = 1e3
|
|
321
|
+
} = {}) => {
|
|
322
|
+
let seen = /* @__PURE__ */ new WeakSet();
|
|
323
|
+
let batches = /* @__PURE__ */ new Map();
|
|
324
|
+
let timer;
|
|
325
|
+
let context;
|
|
326
|
+
let observer;
|
|
327
|
+
const flush = () => {
|
|
328
|
+
clearTimeout(timer);
|
|
329
|
+
timer = void 0;
|
|
330
|
+
const pending = batches;
|
|
331
|
+
batches = /* @__PURE__ */ new Map();
|
|
332
|
+
for (const [list, items] of pending) {
|
|
333
|
+
const { data, composed } = readAnnotated(list);
|
|
334
|
+
context?.publish("list:view", {
|
|
335
|
+
data: {
|
|
336
|
+
...data,
|
|
337
|
+
items: [...items.values()].sort((a, b) => a.index - b.index)
|
|
338
|
+
},
|
|
339
|
+
composed
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
const onVisible = (element) => {
|
|
344
|
+
if (element.matches(roleSelector("promotion"))) {
|
|
345
|
+
context?.publish("promotion:view", readAnnotated(element));
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const list = surrounding(element, "list");
|
|
349
|
+
if (!list) return;
|
|
350
|
+
let items = batches.get(list);
|
|
351
|
+
if (!items) batches.set(list, items = /* @__PURE__ */ new Map());
|
|
352
|
+
items.set(element, {
|
|
353
|
+
...readData(element),
|
|
354
|
+
index: indexInList(element, list)
|
|
355
|
+
});
|
|
356
|
+
timer ??= setTimeout(flush, delay);
|
|
357
|
+
};
|
|
358
|
+
const observeAll = (root) => {
|
|
359
|
+
if (root instanceof Element && root.matches(OBSERVED))
|
|
360
|
+
observer?.observe(root);
|
|
361
|
+
for (const element of root.querySelectorAll(OBSERVED))
|
|
362
|
+
observer?.observe(element);
|
|
363
|
+
};
|
|
364
|
+
return {
|
|
365
|
+
events: ["promotion:view", "list:view"],
|
|
366
|
+
start(nextContext) {
|
|
367
|
+
context = nextContext;
|
|
368
|
+
observer = new IntersectionObserver(
|
|
369
|
+
(entries) => {
|
|
370
|
+
for (const entry of entries) {
|
|
371
|
+
if (!entry.isIntersecting || seen.has(entry.target)) continue;
|
|
372
|
+
seen.add(entry.target);
|
|
373
|
+
onVisible(entry.target);
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
{ threshold }
|
|
377
|
+
);
|
|
378
|
+
const mutations = new MutationObserver((records) => {
|
|
379
|
+
for (const record of records)
|
|
380
|
+
for (const node of record.addedNodes)
|
|
381
|
+
if (node instanceof Element) observeAll(node);
|
|
382
|
+
});
|
|
383
|
+
observeAll(document);
|
|
384
|
+
mutations.observe(document.documentElement, {
|
|
385
|
+
childList: true,
|
|
386
|
+
subtree: true
|
|
387
|
+
});
|
|
388
|
+
return () => {
|
|
389
|
+
flush();
|
|
390
|
+
mutations.disconnect();
|
|
391
|
+
observer?.disconnect();
|
|
392
|
+
observer = void 0;
|
|
393
|
+
context = void 0;
|
|
394
|
+
};
|
|
395
|
+
},
|
|
396
|
+
flush,
|
|
397
|
+
reset() {
|
|
398
|
+
seen = /* @__PURE__ */ new WeakSet();
|
|
399
|
+
observer?.disconnect();
|
|
400
|
+
observeAll(document);
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
// src/sources/dom/page-views.ts
|
|
406
|
+
var view = ({ publish }) => {
|
|
407
|
+
const data = {};
|
|
408
|
+
for (const element of document.querySelectorAll(flagSelector("page")))
|
|
409
|
+
Object.assign(data, readData(element));
|
|
410
|
+
publish("page:view", { data, composed: {} });
|
|
411
|
+
};
|
|
412
|
+
var pageViews = () => {
|
|
413
|
+
let started;
|
|
414
|
+
return {
|
|
415
|
+
events: ["page:view"],
|
|
416
|
+
start(context) {
|
|
417
|
+
started = context;
|
|
418
|
+
view(context);
|
|
419
|
+
return () => {
|
|
420
|
+
started = void 0;
|
|
421
|
+
};
|
|
422
|
+
},
|
|
423
|
+
reset() {
|
|
424
|
+
if (started) view(started);
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// src/sources/dom/pages.ts
|
|
430
|
+
var withoutTrailingSlash = (path) => path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
431
|
+
var matches = (matcher, url) => {
|
|
432
|
+
if (typeof matcher === "function") return matcher(url);
|
|
433
|
+
if (matcher instanceof RegExp) return matcher.test(url.pathname);
|
|
434
|
+
if (matcher.endsWith("/*"))
|
|
435
|
+
return url.pathname.startsWith(matcher.slice(0, -1));
|
|
436
|
+
return withoutTrailingSlash(url.pathname) === withoutTrailingSlash(matcher);
|
|
437
|
+
};
|
|
438
|
+
var restrict = (detector, isAllowed) => {
|
|
439
|
+
let active = false;
|
|
440
|
+
const decide = () => {
|
|
441
|
+
active = isAllowed(new URL(location.href));
|
|
442
|
+
};
|
|
443
|
+
return {
|
|
444
|
+
events: detector.events,
|
|
445
|
+
start(context) {
|
|
446
|
+
decide();
|
|
447
|
+
return detector.start({
|
|
448
|
+
publish: (name, event) => {
|
|
449
|
+
if (active) context.publish(name, event);
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
},
|
|
453
|
+
flush: () => detector.flush?.(),
|
|
454
|
+
// Decided first: a Detector may publish from `reset()`, e.g. `pageViews()`.
|
|
455
|
+
reset() {
|
|
456
|
+
decide();
|
|
457
|
+
detector.reset?.();
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
var only = (detector, paths) => restrict(detector, (url) => paths.some((path) => matches(path, url)));
|
|
462
|
+
var except = (detector, paths) => restrict(detector, (url) => !paths.some((path) => matches(path, url)));
|
|
463
|
+
|
|
464
|
+
// src/sources/dom/scroll.ts
|
|
465
|
+
var scroll = ({
|
|
466
|
+
thresholds = [25, 50, 75, 100]
|
|
467
|
+
} = {}) => {
|
|
468
|
+
let reached = /* @__PURE__ */ new Set();
|
|
469
|
+
return {
|
|
470
|
+
events: ["page:scroll"],
|
|
471
|
+
start({ publish }) {
|
|
472
|
+
const onScroll = () => {
|
|
473
|
+
const { scrollHeight } = document.documentElement;
|
|
474
|
+
if (!scrollHeight) return;
|
|
475
|
+
const depth = (window.scrollY + window.innerHeight) / scrollHeight * 100;
|
|
476
|
+
for (const threshold of thresholds) {
|
|
477
|
+
if (depth < threshold || reached.has(threshold)) continue;
|
|
478
|
+
reached.add(threshold);
|
|
479
|
+
publish("page:scroll", {
|
|
480
|
+
data: { depth: String(threshold) },
|
|
481
|
+
composed: {}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
window.addEventListener("scroll", onScroll, { passive: true });
|
|
486
|
+
return () => window.removeEventListener("scroll", onScroll);
|
|
487
|
+
},
|
|
488
|
+
reset() {
|
|
489
|
+
reached = /* @__PURE__ */ new Set();
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// src/sources/dom/selections.ts
|
|
495
|
+
var selections = () => ({
|
|
496
|
+
events: ["item:select", "promotion:select"],
|
|
497
|
+
start({ publish }) {
|
|
498
|
+
const onClick = (event) => {
|
|
499
|
+
if (!(event.target instanceof Element)) return;
|
|
500
|
+
const target = event.target.closest(INTERACTIVE);
|
|
501
|
+
if (!target) return;
|
|
502
|
+
const item = target.closest(roleSelector("item"));
|
|
503
|
+
if (item && hasWord(item, "detail")) return;
|
|
504
|
+
if (item) {
|
|
505
|
+
const { data, composed } = readAnnotated(item);
|
|
506
|
+
const list = surrounding(item, "list");
|
|
507
|
+
publish("item:select", {
|
|
508
|
+
data: list ? { ...data, index: indexInList(item, list) } : data,
|
|
509
|
+
composed
|
|
510
|
+
});
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const promotion = target.closest(roleSelector("promotion"));
|
|
514
|
+
if (promotion) publish("promotion:select", readAnnotated(promotion));
|
|
515
|
+
};
|
|
516
|
+
document.addEventListener("click", onClick, { capture: true });
|
|
517
|
+
return () => document.removeEventListener("click", onClick, { capture: true });
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
// src/sources/dom/videos.ts
|
|
522
|
+
var videos = ({
|
|
523
|
+
auto = false,
|
|
524
|
+
thresholds = [10, 25, 50, 75]
|
|
525
|
+
} = {}) => {
|
|
526
|
+
let playbacks = /* @__PURE__ */ new WeakMap();
|
|
527
|
+
const read = (video, percent) => {
|
|
528
|
+
const { data, composed } = readAnnotated(video);
|
|
529
|
+
const duration = Number.isFinite(video.duration) ? video.duration : 0;
|
|
530
|
+
const base = compact({
|
|
531
|
+
url: video.currentSrc || video.src,
|
|
532
|
+
title: video.title || video.getAttribute("aria-label") || void 0,
|
|
533
|
+
duration: Math.round(duration),
|
|
534
|
+
currentTime: Math.round(video.currentTime),
|
|
535
|
+
percent: percent ?? (duration ? Math.round(video.currentTime / duration * 100) : 0)
|
|
536
|
+
});
|
|
537
|
+
return { data: { ...base, ...data }, composed };
|
|
538
|
+
};
|
|
539
|
+
const tracked = (target) => target instanceof HTMLVideoElement && isOptedIn(target, {
|
|
540
|
+
flag: "video",
|
|
541
|
+
kind: "videos",
|
|
542
|
+
fallback: auto
|
|
543
|
+
}) ? target : void 0;
|
|
544
|
+
const playback = (video) => {
|
|
545
|
+
let state = playbacks.get(video);
|
|
546
|
+
if (!state)
|
|
547
|
+
playbacks.set(video, state = { started: false, reached: /* @__PURE__ */ new Set() });
|
|
548
|
+
return state;
|
|
549
|
+
};
|
|
550
|
+
return {
|
|
551
|
+
events: ["video:start", "video:progress", "video:complete"],
|
|
552
|
+
start({ publish }) {
|
|
553
|
+
const onPlay = (event) => {
|
|
554
|
+
const video = tracked(event.target);
|
|
555
|
+
if (!video) return;
|
|
556
|
+
const state = playback(video);
|
|
557
|
+
if (state.started) return;
|
|
558
|
+
state.started = true;
|
|
559
|
+
publish("video:start", read(video));
|
|
560
|
+
};
|
|
561
|
+
const onTimeUpdate = (event) => {
|
|
562
|
+
const video = tracked(event.target);
|
|
563
|
+
if (!video || !Number.isFinite(video.duration) || !video.duration)
|
|
564
|
+
return;
|
|
565
|
+
const state = playback(video);
|
|
566
|
+
const percent = video.currentTime / video.duration * 100;
|
|
567
|
+
for (const threshold of thresholds) {
|
|
568
|
+
if (percent < threshold || state.reached.has(threshold)) continue;
|
|
569
|
+
state.reached.add(threshold);
|
|
570
|
+
publish("video:progress", read(video, threshold));
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
const onEnded = (event) => {
|
|
574
|
+
const video = tracked(event.target);
|
|
575
|
+
if (!video) return;
|
|
576
|
+
publish("video:complete", read(video, 100));
|
|
577
|
+
playbacks.delete(video);
|
|
578
|
+
};
|
|
579
|
+
const options = { capture: true };
|
|
580
|
+
document.addEventListener("play", onPlay, options);
|
|
581
|
+
document.addEventListener("timeupdate", onTimeUpdate, options);
|
|
582
|
+
document.addEventListener("ended", onEnded, options);
|
|
583
|
+
return () => {
|
|
584
|
+
document.removeEventListener("play", onPlay, options);
|
|
585
|
+
document.removeEventListener("timeupdate", onTimeUpdate, options);
|
|
586
|
+
document.removeEventListener("ended", onEnded, options);
|
|
587
|
+
};
|
|
588
|
+
},
|
|
589
|
+
reset() {
|
|
590
|
+
playbacks = /* @__PURE__ */ new WeakMap();
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
export { annotate, clicks, createDomSource, detailViews, except, forms, impressions, only, pageViews, scroll, selections, videos };
|
|
596
|
+
//# sourceMappingURL=index.js.map
|
|
597
|
+
//# sourceMappingURL=index.js.map
|