@adobe/helix-rum-enhancer 2.50.0 → 2.51.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/src/index.js CHANGED
@@ -9,469 +9,463 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- (function () {
13
- 'use strict';
14
-
15
- const h = (s, a) => [...s].reduce((p, c) => p + c.charCodeAt(0), a) % 1371;
16
- const fflags = {
17
- has: f => fflags[f].includes(h(window.origin, 1)) || /localhost/.test(window.origin),
18
- enabled: (f, c) => fflags.has(f) && c(),
19
- disabled: (f, c) => !fflags.has(f) && c(),
20
- eagercwv: [683],
21
- example: [543, 770, 1136],
22
- allresources: [543, 1139, 339],
23
- a11y: [557, 781, 897, 955, 959]
24
- };
25
-
26
- const KNOWN_PROPERTIES = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB', 'ua'];
27
- const DEFAULT_TRACKING_EVENTS = ['click', 'cwv', 'form', 'viewblock', 'viewmedia', 'loadresource', 'utm', 'paid', 'email', 'consent'];
28
- fflags.enabled('example', () => DEFAULT_TRACKING_EVENTS.push('example'));
29
-
30
- const {
31
- href
32
- } = window.location;
33
- const urlSanitizers = {
34
- full: (url = href) => new URL(url).toString(),
35
- origin: (url = href) => new URL(url).origin,
36
- path: (url = href) => {
37
- const u = new URL(url);
38
- return `${u.origin}${u.pathname}`;
12
+ (function(){'use strict';const h = (s, a) => [...s].reduce((p, c) => p + c.charCodeAt(0), a) % 1371;
13
+ const fflags = {
14
+ has: f => fflags[f].includes(h(window.origin, 1)) || /localhost/.test(window.origin),
15
+ enabled: (f, c) => fflags.has(f) && c(),
16
+ disabled: (f, c) => !fflags.has(f) && c(),
17
+ eagercwv: [683],
18
+ example: [543, 770, 1136],
19
+ allresources: [543, 1139, 339],
20
+ a11y: [557, 781, 897, 955, 959],
21
+ softnav: [273]
22
+ };const KNOWN_PROPERTIES = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB', 'ua'];
23
+ const DEFAULT_TRACKING_EVENTS = ['click', 'cwv', 'form', 'viewblock', 'viewmedia', 'loadresource', 'utm', 'paid', 'email', 'consent'];
24
+ fflags.enabled('example', () => DEFAULT_TRACKING_EVENTS.push('example'));const {
25
+ href
26
+ } = window.location;
27
+ const urlSanitizers = {
28
+ full: (url = href) => new URL(url).toString(),
29
+ origin: (url = href) => new URL(url).origin,
30
+ path: (url = href) => {
31
+ const u = new URL(url);
32
+ return `${u.origin}${u.pathname}`;
33
+ }
34
+ };const getTargetValue = el => el.getAttribute('data-rum-target') || el.getAttribute('href') || el.currentSrc || el.getAttribute('src') || el.dataset.action || el.action;
35
+ const targetSelector = el => {
36
+ try {
37
+ if (!el) {
38
+ return undefined;
39
39
  }
40
- };
41
-
42
- const getTargetValue = el => el.getAttribute('data-rum-target') || el.getAttribute('href') || el.currentSrc || el.getAttribute('src') || el.dataset.action || el.action;
43
- const targetSelector = el => {
44
- try {
45
- if (!el) {
46
- return undefined;
47
- }
48
- let v = getTargetValue(el);
49
- if (!v && el.tagName !== 'A' && el.closest('a')) {
50
- v = getTargetValue(el.closest('a'));
51
- }
52
- if (v && !v.startsWith('https://')) {
53
- v = new URL(v, window.location).href;
54
- }
55
- return v;
56
- } catch (error) {
57
- return null;
40
+ let v = getTargetValue(el);
41
+ if (!v && el.tagName !== 'A' && el.closest('a')) {
42
+ v = getTargetValue(el.closest('a'));
58
43
  }
59
- };
60
- const untrustedClickPayload = event => {
61
- if (event && event.isTrusted === false) {
62
- const ua = navigator.userAgent;
63
- return {
64
- ua: ua.includes('+http') ? ua : `${ua} +http://event.untrusted`
65
- };
44
+ if (v && !v.startsWith('https://')) {
45
+ v = new URL(v, window.location).href;
66
46
  }
67
- return {};
68
- };
69
- function walk(el, check) {
70
- if (!el || el === document.body || el === document.documentElement) {
71
- return undefined;
47
+ return v;
48
+ } catch (error) {
49
+ return null;
50
+ }
51
+ };
52
+ const untrustedClickPayload = event => {
53
+ if (event && event.isTrusted === false) {
54
+ const ua = navigator.userAgent;
55
+ return {
56
+ ua: ua.includes('+http') ? ua : `${ua} +http://event.untrusted`
57
+ };
58
+ }
59
+ return {};
60
+ };
61
+ function walk(el, check) {
62
+ if (!el || el === document.body || el === document.documentElement) {
63
+ return undefined;
64
+ }
65
+ return check(el) || walk(el.parentElement || el.parentNode && el.parentNode.host, check);
66
+ }
67
+ function isDialog(el) {
68
+ const cs = window.getComputedStyle(el);
69
+ return el.tagName === 'DIALOG' || ['dialog', 'alertdialog'].find(r => el.getAttribute('role') === r) || el.getAttribute('aria-modal') === 'true' || cs && cs.position === 'fixed' && cs.zIndex > 100;
70
+ }
71
+ function isButton(el) {
72
+ if (el.tagName === 'BUTTON' || el.tagName === 'INPUT' && el.getAttribute('type') === 'button') {
73
+ return true;
74
+ }
75
+ if (el.tagName === 'A') {
76
+ return Array.from(el.classList).some(cls => cls.match(/button|cta/));
77
+ }
78
+ return el.getAttribute('role') === 'button';
79
+ }
80
+ function srcContext(el) {
81
+ const form = el.closest('form');
82
+ if (form) {
83
+ const id = form.getAttribute('id');
84
+ if (id) {
85
+ return `form#${CSS.escape(id)}`;
72
86
  }
73
- return check(el) || walk(el.parentElement || el.parentNode && el.parentNode.host, check);
87
+ return `form${form.classList.length > 0 ? `.${CSS.escape(form.classList[0])}` : ''}`;
74
88
  }
75
- function isDialog(el) {
76
- const cs = window.getComputedStyle(el);
77
- return el.tagName === 'DIALOG' || ['dialog', 'alertdialog'].find(r => el.getAttribute('role') === r) || el.getAttribute('aria-modal') === 'true' || cs && cs.position === 'fixed' && cs.zIndex > 100;
89
+ const block = el.closest('.block[data-block-name]');
90
+ return block && `.${block.getAttribute('data-block-name')}` || walk(el, isDialog) && 'dialog' || walk(el, e => e.tagName && e.tagName.includes('-') && e.tagName.toLowerCase()) || ['nav', 'header', 'footer', 'aside'].find(t => el.closest(t)) || walk(el, e => e.id && `#${CSS.escape(e.id)}`);
91
+ }
92
+ function srcElement(el) {
93
+ const f = el.closest('form');
94
+ if (f && Array.from(f.elements).includes(el)) {
95
+ return el.tagName.toLowerCase() + (['INPUT', 'BUTTON'].includes(el.tagName) ? `[type='${el.getAttribute('type') || ''}']` : '');
78
96
  }
79
- function isButton(el) {
80
- if (el.tagName === 'BUTTON' || el.tagName === 'INPUT' && el.getAttribute('type') === 'button') {
81
- return true;
97
+ return walk(el, isButton) && 'button' || el.tagName.toLowerCase().match(/^(a|img|video|form)$/) && el.tagName.toLowerCase();
98
+ }
99
+ function srcId(el) {
100
+ return el.id && `#${CSS.escape(el.id)}` || el.getAttribute('data-block-name') && `.${el.getAttribute('data-block-name')}` || el.classList.length > 0 && `.${CSS.escape(el.classList[0])}`;
101
+ }
102
+ const sourceSelector = el => {
103
+ try {
104
+ if (!el || el === document.body || el === document.documentElement) {
105
+ return undefined;
82
106
  }
83
- if (el.tagName === 'A') {
84
- return Array.from(el.classList).some(cls => cls.match(/button|cta/));
107
+ if (el.getAttribute('data-rum-source')) {
108
+ return el.getAttribute('data-rum-source');
85
109
  }
86
- return el.getAttribute('role') === 'button';
110
+ const ctx = srcContext(el.parentElement) || '';
111
+ const name = srcElement(el) || '';
112
+ const id = srcId(el) || '';
113
+ return `${ctx} ${name}${id}`.trim() || `"${el.textContent.substring(0, 10)}"`;
114
+ } catch (error) {
115
+ return null;
87
116
  }
88
- function srcContext(el) {
89
- const form = el.closest('form');
90
- if (form) {
91
- const id = form.getAttribute('id');
92
- if (id) {
93
- return `form#${CSS.escape(id)}`;
94
- }
95
- return `form${form.classList.length > 0 ? `.${CSS.escape(form.classList[0])}` : ''}`;
96
- }
97
- const block = el.closest('.block[data-block-name]');
98
- return block && `.${block.getAttribute('data-block-name')}` || walk(el, isDialog) && 'dialog' || walk(el, e => e.tagName && e.tagName.includes('-') && e.tagName.toLowerCase()) || ['nav', 'header', 'footer', 'aside'].find(t => el.closest(t)) || walk(el, e => e.id && `#${CSS.escape(e.id)}`);
117
+ };const {
118
+ sampleRUM,
119
+ queue,
120
+ isSelected
121
+ } = window.hlx && window.hlx.rum ? window.hlx.rum : {};
122
+ const createMO = cb => window.MutationObserver ? new MutationObserver(cb) : {};
123
+ const [blocksMO, mediaMO] = [blocksMCB, mediaMCB].map(createMO);
124
+ const hasCookie = key => document.cookie.split(';').some(c => c.trim().startsWith(`${key}=`));
125
+ const pluginBase = new URL(document.currentScript.src).href.replace(/index(\.map)?\.js/, 'plugins');
126
+ const CONSENT_PROVIDERS = [{
127
+ name: 'onetrust',
128
+ detect: () => hasCookie('OptanonAlertBoxClosed') || document.querySelector('#onetrust-banner-sdk, #onetrust-pc-sdk')
129
+ }, {
130
+ name: 'trustarc',
131
+ detect: () => ['notice_gdpr_prefs', 'notice_preferences'].some(hasCookie) || document.querySelector('#truste-consent-track')
132
+ }, {
133
+ name: 'usercentrics',
134
+ detect: () => window.localStorage.getItem('uc_gcm') || document.querySelector('#usercentrics-root')
135
+ }];
136
+ const getConsent = () => CONSENT_PROVIDERS.find(({
137
+ detect
138
+ }) => detect());
139
+ const bodyChildMO = {
140
+ target: document.body,
141
+ options: {
142
+ attributes: false,
143
+ childList: true,
144
+ subtree: false
99
145
  }
100
- function srcElement(el) {
101
- const f = el.closest('form');
102
- if (f && Array.from(f.elements).includes(el)) {
103
- return el.tagName.toLowerCase() + (['INPUT', 'BUTTON'].includes(el.tagName) ? `[type='${el.getAttribute('type') || ''}']` : '');
104
- }
105
- return walk(el, isButton) && 'button' || el.tagName.toLowerCase().match(/^(a|img|video|form)$/) && el.tagName.toLowerCase();
146
+ };
147
+ const PLUGINS = {
148
+ cwv: `${pluginBase}/cwv.js`,
149
+ a11y: `${pluginBase}/a11y.js`,
150
+ form: {
151
+ url: `${pluginBase}/form.js`,
152
+ when: () => document.querySelector('form'),
153
+ isBlockDependent: true,
154
+ moParams: bodyChildMO
155
+ },
156
+ redirect: {
157
+ url: `${pluginBase}/redirect.js`,
158
+ when: ({
159
+ perfEntry: pe,
160
+ urlParameters: usp
161
+ }) => pe && (usp.get('redirect_from') || pe.redirectCount > 0 || pe.fetchStart > 50)
162
+ },
163
+ video: {
164
+ url: `${pluginBase}/video.js`,
165
+ when: () => document.querySelector('video'),
166
+ isBlockDependent: true
167
+ },
168
+ webcomponent: {
169
+ url: `${pluginBase}/webcomponent.js`,
170
+ when: () => [...document.querySelectorAll('*')].some(el => el.tagName && el.tagName.includes('-')),
171
+ isBlockDependent: true
172
+ },
173
+ softnav: {
174
+ url: `${pluginBase}/softnav.js`,
175
+ when: ({
176
+ fflags: f
177
+ }) => f.has('softnav')
178
+ },
179
+ martech: {
180
+ url: `${pluginBase}/martech.js`,
181
+ when: ({
182
+ urlParameters
183
+ }) => urlParameters.size > 0
184
+ },
185
+ consent: {
186
+ when: () => getConsent(),
187
+ isBlockDependent: true,
188
+ moParams: bodyChildMO
106
189
  }
107
- function srcId(el) {
108
- return el.id && `#${CSS.escape(el.id)}` || el.getAttribute('data-block-name') && `.${el.getAttribute('data-block-name')}` || el.classList.length > 0 && `.${CSS.escape(el.classList[0])}`;
190
+ };
191
+ const MAX_EV = 1023;
192
+ const vmDedupe = new Set();
193
+ function getIntersectionObserver(checkpoint) {
194
+ if (!window.IntersectionObserver) {
195
+ return null;
109
196
  }
110
- const sourceSelector = el => {
197
+ const obs = new IntersectionObserver(entries => {
111
198
  try {
112
- if (!el || el === document.body || el === document.documentElement) {
113
- return undefined;
114
- }
115
- if (el.getAttribute('data-rum-source')) {
116
- return el.getAttribute('data-rum-source');
117
- }
118
- const ctx = srcContext(el.parentElement) || '';
119
- const name = srcElement(el) || '';
120
- const id = srcId(el) || '';
121
- return `${ctx} ${name}${id}`.trim() || `"${el.textContent.substring(0, 10)}"`;
122
- } catch (error) {
123
- return null;
124
- }
125
- };
126
-
127
- const {
128
- sampleRUM,
129
- queue,
130
- isSelected
131
- } = window.hlx && window.hlx.rum ? window.hlx.rum : {};
132
- const createMO = cb => window.MutationObserver ? new MutationObserver(cb) : {};
133
- const [blocksMO, mediaMO] = [blocksMCB, mediaMCB].map(createMO);
134
- const hasCookie = key => document.cookie.split(';').some(c => c.trim().startsWith(`${key}=`));
135
- const pluginBase = new URL(document.currentScript.src).href.replace(/index(\.map)?\.js/, 'plugins');
136
- const CONSENT_PROVIDERS = [{
137
- name: 'onetrust',
138
- detect: () => hasCookie('OptanonAlertBoxClosed') || document.querySelector('#onetrust-banner-sdk, #onetrust-pc-sdk')
139
- }, {
140
- name: 'trustarc',
141
- detect: () => ['notice_gdpr_prefs', 'notice_preferences'].some(hasCookie) || document.querySelector('#truste-consent-track')
142
- }, {
143
- name: 'usercentrics',
144
- detect: () => window.localStorage.getItem('uc_gcm') || document.querySelector('#usercentrics-root')
145
- }];
146
- const getConsent = () => CONSENT_PROVIDERS.find(({
147
- detect
148
- }) => detect());
149
- const bodyChildMO = {
150
- target: document.body,
151
- options: {
152
- attributes: false,
153
- childList: true,
154
- subtree: false
155
- }
156
- };
157
- const PLUGINS = {
158
- cwv: `${pluginBase}/cwv.js`,
159
- a11y: `${pluginBase}/a11y.js`,
160
- form: {
161
- url: `${pluginBase}/form.js`,
162
- when: () => document.querySelector('form'),
163
- isBlockDependent: true,
164
- moParams: bodyChildMO
165
- },
166
- redirect: {
167
- url: `${pluginBase}/redirect.js`,
168
- when: ({
169
- perfEntry: pe,
170
- urlParameters: usp
171
- }) => pe && (usp.get('redirect_from') || pe.redirectCount > 0 || pe.fetchStart > 50)
172
- },
173
- video: {
174
- url: `${pluginBase}/video.js`,
175
- when: () => document.querySelector('video'),
176
- isBlockDependent: true
177
- },
178
- webcomponent: {
179
- url: `${pluginBase}/webcomponent.js`,
180
- when: () => [...document.querySelectorAll('*')].some(el => el.tagName && el.tagName.includes('-')),
181
- isBlockDependent: true
182
- },
183
- martech: {
184
- url: `${pluginBase}/martech.js`,
185
- when: ({
186
- urlParameters
187
- }) => urlParameters.size > 0
188
- },
189
- consent: {
190
- when: () => getConsent(),
191
- isBlockDependent: true,
192
- moParams: bodyChildMO
193
- }
194
- };
195
- const MAX_EV = 1023;
196
- const vmDedupe = new Set();
197
- function getIntersectionObserver(checkpoint) {
198
- if (!window.IntersectionObserver) {
199
- return null;
200
- }
201
- const obs = new IntersectionObserver(entries => {
202
- try {
203
- entries.filter(e => e.isIntersecting).forEach(e => {
204
- obs.unobserve(e.target);
205
- const target = targetSelector(e.target);
206
- const source = sourceSelector(e.target);
207
- if (checkpoint === 'viewmedia') {
208
- const key = `${source}\0${target}`;
209
- if (vmDedupe.has(key)) {
210
- return;
211
- }
212
- if (vmDedupe.size < MAX_EV) {
213
- vmDedupe.add(key);
214
- }
199
+ entries.filter(e => e.isIntersecting).forEach(e => {
200
+ obs.unobserve(e.target);
201
+ const target = targetSelector(e.target);
202
+ const source = sourceSelector(e.target);
203
+ if (checkpoint === 'viewmedia') {
204
+ const key = `${source}\0${target}`;
205
+ if (vmDedupe.has(key)) {
206
+ return;
207
+ }
208
+ if (vmDedupe.size < MAX_EV) {
209
+ vmDedupe.add(key);
215
210
  }
216
- sampleRUM(checkpoint, {
217
- target,
218
- source
219
- });
211
+ }
212
+ sampleRUM(checkpoint, {
213
+ target,
214
+ source
220
215
  });
221
- } catch (error) {}
222
- });
223
- return obs;
224
- }
225
- const PLUGIN_PARAMS = {
226
- context: document.body,
227
- fflags,
228
- sampleRUM,
229
- sourceSelector,
230
- targetSelector,
231
- untrustedClickPayload,
232
- getIntersectionObserver,
233
- createMO
234
- };
235
- const pluginCache = new Map();
236
- function loadPlugin(key, params) {
237
- const plugin = PLUGINS[key];
238
- const usp = new URLSearchParams(window.location.search);
239
- if (!pluginCache.has(key) && plugin.when && !plugin.when({
240
- ...params,
241
- urlParameters: usp
242
- })) {
243
- if (plugin.moParams && !plugin.isBeingObserved) {
244
- createPluginMO(key, params, usp);
245
- }
246
- return null;
247
- }
248
- if (key === 'consent') {
249
- plugin.url = `${pluginBase}/${getConsent().name}.js`;
250
- }
251
- if (!pluginCache.has(key)) {
252
- pluginCache.set(key, import(`${plugin.url || plugin}`));
216
+ });
217
+ } catch (error) {}
218
+ });
219
+ return obs;
220
+ }
221
+ const PLUGIN_PARAMS = {
222
+ context: document.body,
223
+ fflags,
224
+ sampleRUM,
225
+ sourceSelector,
226
+ targetSelector,
227
+ untrustedClickPayload,
228
+ getIntersectionObserver,
229
+ createMO
230
+ };
231
+ const pluginCache = new Map();
232
+ function loadPlugin(key, params) {
233
+ const plugin = PLUGINS[key];
234
+ const usp = new URLSearchParams(window.location.search);
235
+ if (!pluginCache.has(key) && plugin.when && !plugin.when({
236
+ ...params,
237
+ urlParameters: usp
238
+ })) {
239
+ if (plugin.moParams && !plugin.isBeingObserved) {
240
+ createPluginMO(key, params, usp);
253
241
  }
254
- return pluginCache.get(key).then(p => p.default && p.default(params) || typeof p === 'function' && p(params)).catch(() => {});
242
+ return null;
255
243
  }
256
- function loadPlugins(filter = () => true, params = PLUGIN_PARAMS) {
257
- Object.entries(PLUGINS).filter(([, plugin]) => filter(plugin)).map(([key]) => loadPlugin(key, params));
244
+ if (key === 'consent') {
245
+ plugin.url = `${pluginBase}/${getConsent().name}.js`;
258
246
  }
259
- function createPluginMO(key, params, usp) {
260
- const plugin = PLUGINS[key];
261
- const obs = createMO(() => {
262
- if (plugin.when({
263
- urlParameters: usp
264
- })) {
265
- plugin.isBeingObserved = false;
266
- obs.disconnect();
267
- loadPlugin(key, params);
268
- }
269
- });
270
- if (obs instanceof MutationObserver) {
271
- plugin.isBeingObserved = true;
272
- obs.observe(plugin.moParams.target, plugin.moParams.options);
247
+ if (!pluginCache.has(key)) {
248
+ pluginCache.set(key, import(`${plugin.url || plugin}`));
249
+ }
250
+ return pluginCache.get(key).then(p => p.default && p.default(params) || typeof p === 'function' && p(params)).catch(() => {});
251
+ }
252
+ function loadPlugins(filter = () => true, params = PLUGIN_PARAMS) {
253
+ Object.entries(PLUGINS).filter(([, plugin]) => filter(plugin)).map(([key]) => loadPlugin(key, params));
254
+ }
255
+ function createPluginMO(key, params, usp) {
256
+ const plugin = PLUGINS[key];
257
+ const obs = createMO(() => {
258
+ if (plugin.when({
259
+ urlParameters: usp
260
+ })) {
261
+ plugin.isBeingObserved = false;
262
+ obs.disconnect();
263
+ loadPlugin(key, params);
273
264
  }
265
+ });
266
+ if (obs instanceof MutationObserver) {
267
+ plugin.isBeingObserved = true;
268
+ obs.observe(plugin.moParams.target, plugin.moParams.options);
274
269
  }
275
- let maxEvents = MAX_EV;
276
- function trackCheckpoint(checkpoint, data, t) {
277
- const {
270
+ }
271
+ let maxEvents = MAX_EV;
272
+ function trackCheckpoint(checkpoint, data, t) {
273
+ const {
274
+ weight,
275
+ id
276
+ } = window.hlx.rum;
277
+ if (isSelected && maxEvents) {
278
+ maxEvents -= 1;
279
+ const body = JSON.stringify({
278
280
  weight,
279
- id
280
- } = window.hlx.rum;
281
- if (isSelected && maxEvents) {
282
- maxEvents -= 1;
283
- const body = JSON.stringify({
284
- weight,
285
- id,
286
- referer: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'](),
287
- checkpoint,
288
- t,
289
- ...data
290
- }, KNOWN_PROPERTIES);
291
- const urlParams = window.RUM_PARAMS ? `?${new URLSearchParams(window.RUM_PARAMS).toString()}` : '';
292
- const {
293
- href: url,
294
- origin
295
- } = new URL(`.rum/${weight}${urlParams.length > 1 ? urlParams : ''}`, sampleRUM.collectBaseURL);
296
- if (window.location.origin === origin) {
297
- const headers = {
298
- type: 'application/json'
299
- };
300
- navigator.sendBeacon(url, new Blob([body], headers));
301
- } else {
302
- navigator.sendBeacon(url, body);
303
- }
304
- console.debug(`ping:${checkpoint}`, data);
281
+ id,
282
+ referer: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'](),
283
+ checkpoint,
284
+ t,
285
+ ...data
286
+ }, KNOWN_PROPERTIES);
287
+ const urlParams = window.RUM_PARAMS ? `?${new URLSearchParams(window.RUM_PARAMS).toString()}` : '';
288
+ const {
289
+ href: url,
290
+ origin
291
+ } = new URL(`.rum/${weight}${urlParams.length > 1 ? urlParams : ''}`, sampleRUM.collectBaseURL);
292
+ if (window.location.origin === origin) {
293
+ const headers = {
294
+ type: 'application/json'
295
+ };
296
+ navigator.sendBeacon(url, new Blob([body], headers));
297
+ } else {
298
+ navigator.sendBeacon(url, body);
305
299
  }
300
+ console.debug(`ping:${checkpoint}`, data);
306
301
  }
307
- function processQueue() {
308
- while (queue && queue.length) {
309
- const ck = queue.shift();
310
- trackCheckpoint(...ck);
311
- }
302
+ }
303
+ function processQueue() {
304
+ while (queue && queue.length) {
305
+ const ck = queue.shift();
306
+ trackCheckpoint(...ck);
312
307
  }
313
- function addNavigationTracking() {
314
- const navigate = (source, type, perfEntry) => {
315
- const payload = {
316
- source: source && urlSanitizers.path(source),
317
- target: document.visibilityState
318
- };
319
- if (document.prerendering) {
320
- document.addEventListener('prerenderingchange', () => {
321
- payload.target = 'prerendered';
322
- sampleRUM('navigate', payload);
323
- }, {
324
- once: true
325
- });
326
- if (type === 'navigate') {
327
- sampleRUM('prerender', payload);
328
- }
329
- } else if (type === 'reload' || source === window.location.href) {
330
- sampleRUM('reload', payload);
331
- } else if (type && type !== 'navigate') {
332
- sampleRUM(type, payload);
333
- } else if (source && window.location.origin === new URL(source).origin) {
308
+ }
309
+ function addNavigationTracking() {
310
+ const navigate = (source, type, perfEntry) => {
311
+ const payload = {
312
+ source: source && urlSanitizers.path(source),
313
+ target: document.visibilityState
314
+ };
315
+ if (document.prerendering) {
316
+ document.addEventListener('prerenderingchange', () => {
317
+ payload.target = 'prerendered';
334
318
  sampleRUM('navigate', payload);
335
- } else {
336
- sampleRUM('enter', payload);
337
- }
338
- loadPlugin('redirect', {
339
- ...PLUGIN_PARAMS,
340
- perfEntry
319
+ }, {
320
+ once: true
341
321
  });
342
- };
343
- const processed = new Set();
344
- new PerformanceObserver(list => list.getEntries().filter(({
345
- type
346
- }) => !processed.has(type)).map(e => [e, processed.add(e.type)]).map(([e]) => navigate(window.hlx.referrer || document.referrer, e.type, e))).observe({
347
- type: 'navigation',
348
- buffered: true
322
+ if (type === 'navigate') {
323
+ sampleRUM('prerender', payload);
324
+ }
325
+ } else if (type === 'reload' || source === window.location.href) {
326
+ sampleRUM('reload', payload);
327
+ } else if (type && type !== 'navigate') {
328
+ sampleRUM(type, payload);
329
+ } else if (source && window.location.origin === new URL(source).origin) {
330
+ sampleRUM('navigate', payload);
331
+ } else {
332
+ sampleRUM('enter', payload);
333
+ }
334
+ loadPlugin('redirect', {
335
+ ...PLUGIN_PARAMS,
336
+ perfEntry
349
337
  });
350
- }
351
- function addLoadResourceTracking() {
352
- const obs = new PerformanceObserver(list => {
353
- try {
354
- const entries = list.getEntries();
355
- entries.filter(e => !e.responseStatus || e.responseStatus < 400).filter(e => window.location.hostname === new URL(e.name).hostname || fflags.has('allresources')).filter(e => {
356
- const {
357
- pathname,
358
- hostname
359
- } = new URL(e.name);
360
- const extMatch = pathname.match(hostname !== window.location.hostname ? '.*(\\.html$|\\.json|\\.js|graphql|api)' : '.*(\\.plain\\.html$|\\.json|graphql|api)');
361
- const isDropIn = fflags.has('allresources') && (pathname.includes('__dropins__/storefront-') || pathname.includes('scripts/dropins/storefront-'));
362
- const isImage = fflags.has('allresources') && pathname.match(/\.(png|jpe?g|svg)$/i);
363
- return extMatch || isDropIn || isImage;
364
- }).forEach(e => {
365
- sampleRUM('loadresource', {
366
- source: e.name,
367
- target: Math.round(e.duration)
368
- });
338
+ };
339
+ const processed = new Set();
340
+ new PerformanceObserver(list => list.getEntries().filter(({
341
+ type
342
+ }) => !processed.has(type)).map(e => [e, processed.add(e.type)]).map(([e]) => navigate(window.hlx.referrer || document.referrer, e.type, e))).observe({
343
+ type: 'navigation',
344
+ buffered: true
345
+ });
346
+ }
347
+ function addLoadResourceTracking() {
348
+ const obs = new PerformanceObserver(list => {
349
+ try {
350
+ const entries = list.getEntries();
351
+ entries.filter(e => !e.responseStatus || e.responseStatus < 400).filter(e => window.location.hostname === new URL(e.name).hostname || fflags.has('allresources')).filter(e => {
352
+ const {
353
+ pathname,
354
+ hostname
355
+ } = new URL(e.name);
356
+ const extMatch = pathname.match(hostname !== window.location.hostname ? '.*(\\.html$|\\.json|\\.js|graphql|api)' : '.*(\\.plain\\.html$|\\.json|graphql|api)');
357
+ const isDropIn = fflags.has('allresources') && (pathname.includes('__dropins__/storefront-') || pathname.includes('scripts/dropins/storefront-'));
358
+ const isImage = fflags.has('allresources') && pathname.match(/\.(png|jpe?g|svg)$/i);
359
+ return extMatch || isDropIn || isImage;
360
+ }).forEach(e => {
361
+ sampleRUM('loadresource', {
362
+ source: e.name,
363
+ target: Math.round(e.duration)
369
364
  });
370
- entries.filter(e => e.responseStatus >= 400).filter(e => !new URL(e.name).pathname.match('.*(/\\.rum/1[0-9]{0,3})')).forEach(e => {
371
- sampleRUM('missingresource', {
372
- source: e.name,
373
- target: e.responseStatus
374
- });
365
+ });
366
+ entries.filter(e => e.responseStatus >= 400).filter(e => !new URL(e.name).pathname.match('.*(/\\.rum/1[0-9]{0,3})')).forEach(e => {
367
+ sampleRUM('missingresource', {
368
+ source: e.name,
369
+ target: e.responseStatus
375
370
  });
376
- } catch (error) {}
377
- });
378
- obs.observe({
379
- type: 'resource',
380
- buffered: true
381
- });
382
- }
383
- function activateBlocksMO() {
384
- if (!blocksMO || blocksMO.active) {
385
- return;
386
- }
387
- blocksMO.active = true;
388
- blocksMO.observe(document.body, {
389
- subtree: true,
390
- attributes: true,
391
- attributeFilter: ['data-block-status']
392
- });
393
- }
394
- function activateMediaMO() {
395
- if (!mediaMO || mediaMO.active) {
396
- return;
397
- }
398
- mediaMO.active = true;
399
- mediaMO.observe(document.body, {
400
- subtree: true,
401
- attributes: false,
402
- childList: true
403
- });
404
- }
405
- function addViewBlockTracking(el) {
406
- const blockObs = getIntersectionObserver('viewblock');
407
- if (blockObs) {
408
- const blocks = el.getAttribute('data-block-status') ? [el] : el.querySelectorAll('div[data-block-status="loaded"]');
409
- blocks.forEach(b => blockObs.observe(b));
410
- }
411
- }
412
- const observedMedia = new Set();
413
- function addViewMediaTracking(parent) {
414
- const mediaObs = getIntersectionObserver('viewmedia');
415
- if (mediaObs) {
416
- parent.querySelectorAll('img, video, audio, iframe').forEach(m => {
417
- if (!observedMedia.has(m)) {
418
- observedMedia.add(m);
419
- mediaObs.observe(m);
420
- }
421
371
  });
422
- }
372
+ } catch (error) {}
373
+ });
374
+ obs.observe({
375
+ type: 'resource',
376
+ buffered: true
377
+ });
378
+ }
379
+ function activateBlocksMO() {
380
+ if (!blocksMO || blocksMO.active) {
381
+ return;
423
382
  }
424
- function addObserver(ck, fn, block) {
425
- return DEFAULT_TRACKING_EVENTS.includes(ck) && fn(block);
383
+ blocksMO.active = true;
384
+ blocksMO.observe(document.body, {
385
+ subtree: true,
386
+ attributes: true,
387
+ attributeFilter: ['data-block-status']
388
+ });
389
+ }
390
+ function activateMediaMO() {
391
+ if (!mediaMO || mediaMO.active) {
392
+ return;
426
393
  }
427
- function blocksMCB(mutations) {
428
- mutations.filter(m => m.type === 'attributes' && m.attributeName === 'data-block-status').filter(m => m.target.dataset.blockStatus === 'loaded').forEach(m => {
429
- addObserver('form', el => loadPlugins(p => p.isBlockDependent, {
430
- ...PLUGIN_PARAMS,
431
- context: el
432
- }), m.target);
433
- addObserver('viewblock', addViewBlockTracking, m.target);
434
- });
394
+ mediaMO.active = true;
395
+ mediaMO.observe(document.body, {
396
+ subtree: true,
397
+ attributes: false,
398
+ childList: true
399
+ });
400
+ }
401
+ function addViewBlockTracking(el) {
402
+ const blockObs = getIntersectionObserver('viewblock');
403
+ if (blockObs) {
404
+ const blocks = el.getAttribute('data-block-status') ? [el] : el.querySelectorAll('div[data-block-status="loaded"]');
405
+ blocks.forEach(b => blockObs.observe(b));
435
406
  }
436
- function mediaMCB(mutations) {
437
- mutations.forEach(m => {
438
- addObserver('viewmedia', addViewMediaTracking, m.target);
439
- });
440
- }
441
- function addTrackingFromConfig() {
442
- activateBlocksMO();
443
- activateMediaMO();
444
- document.addEventListener('click', event => {
445
- if (event.optelHandled) {
446
- return;
407
+ }
408
+ const observedMedia = new Set();
409
+ function addViewMediaTracking(parent) {
410
+ const mediaObs = getIntersectionObserver('viewmedia');
411
+ if (mediaObs) {
412
+ parent.querySelectorAll('img, video, audio, iframe').forEach(m => {
413
+ if (!observedMedia.has(m)) {
414
+ observedMedia.add(m);
415
+ mediaObs.observe(m);
447
416
  }
448
- sampleRUM('click', {
449
- target: targetSelector(event.target),
450
- source: sourceSelector(event.target),
451
- ...untrustedClickPayload(event)
452
- });
453
417
  });
454
- addNavigationTracking();
455
- addLoadResourceTracking();
456
- addViewBlockTracking(document.body);
457
- addViewMediaTracking(document.body);
458
- loadPlugins();
459
- const target = navigator.language;
460
- const source = document.documentElement.lang;
461
- sampleRUM('language', {
462
- source,
463
- target
464
- });
465
- }
466
- function initEnhancer() {
467
- try {
468
- if (sampleRUM) {
469
- addTrackingFromConfig();
470
- window.hlx.rum.collector = trackCheckpoint;
471
- processQueue();
472
- }
473
- } catch (error) {}
474
418
  }
475
- initEnhancer();
476
-
477
- })();
419
+ }
420
+ function addObserver(ck, fn, block) {
421
+ return DEFAULT_TRACKING_EVENTS.includes(ck) && fn(block);
422
+ }
423
+ function blocksMCB(mutations) {
424
+ mutations.filter(m => m.type === 'attributes' && m.attributeName === 'data-block-status').filter(m => m.target.dataset.blockStatus === 'loaded').forEach(m => {
425
+ addObserver('form', el => loadPlugins(p => p.isBlockDependent, {
426
+ ...PLUGIN_PARAMS,
427
+ context: el
428
+ }), m.target);
429
+ addObserver('viewblock', addViewBlockTracking, m.target);
430
+ });
431
+ }
432
+ function mediaMCB(mutations) {
433
+ mutations.forEach(m => {
434
+ addObserver('viewmedia', addViewMediaTracking, m.target);
435
+ });
436
+ }
437
+ function addTrackingFromConfig() {
438
+ activateBlocksMO();
439
+ activateMediaMO();
440
+ document.addEventListener('click', event => {
441
+ if (event.optelHandled) {
442
+ return;
443
+ }
444
+ sampleRUM('click', {
445
+ target: targetSelector(event.target),
446
+ source: sourceSelector(event.target),
447
+ ...untrustedClickPayload(event)
448
+ });
449
+ });
450
+ addNavigationTracking();
451
+ addLoadResourceTracking();
452
+ addViewBlockTracking(document.body);
453
+ addViewMediaTracking(document.body);
454
+ loadPlugins();
455
+ const target = navigator.language;
456
+ const source = document.documentElement.lang;
457
+ sampleRUM('language', {
458
+ source,
459
+ target
460
+ });
461
+ }
462
+ function initEnhancer() {
463
+ try {
464
+ if (sampleRUM) {
465
+ addTrackingFromConfig();
466
+ window.hlx.rum.collector = trackCheckpoint;
467
+ processQueue();
468
+ }
469
+ } catch (error) {}
470
+ }
471
+ initEnhancer();})();