From 4cfcad9e8271a2a3c4571db419e26857b72d0c6d Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 15 Feb 2024 16:24:55 +0100 Subject: [PATCH 1/4] fix(cwv): report metrics eagerly --- src/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 54b54f3..a86b3e5 100644 --- a/src/index.js +++ b/src/index.js @@ -107,14 +107,15 @@ function addCWVTracking() { data.cwv[measurement.name] = measurement.value; sampleRUM('cwv', data); }; + + const featureToggle = () => window.location.hostname === 'blog.adobe.com'; + // When loading `web-vitals` using a classic script, all the public // methods can be found on the `webVitals` global namespace. ['CLS', 'FID', 'LCP', 'INP', 'TTFB'] - .map((metric) => window.webVitals[`get${metric}`]) - .filter((metric) => typeof metric === 'function') - .forEach((invokeMetric) => { - invokeMetric(storeCWV); - }); + .map((metric) => window.webVitals[`on${metric}`]) + .filter((metricFn) => typeof metricFn === 'function') + .forEach((metricFn) => metricFn(storeCWV, { reportAllChanges: featureToggle() })); }; document.head.appendChild(script); }, 2000); // wait for delayed From 9d44949701865242ac44255d5080f52b22be2b72 Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 15 Feb 2024 18:25:15 +0100 Subject: [PATCH 2/4] fix(cwv): only report LCP and CLS eagerly --- src/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index a86b3e5..cb256b5 100644 --- a/src/index.js +++ b/src/index.js @@ -109,13 +109,16 @@ function addCWVTracking() { }; const featureToggle = () => window.location.hostname === 'blog.adobe.com'; + const isEager = (metric) => ['CLS', 'LCP'].includes(metric); - // When loading `web-vitals` using a classic script, all the public - // methods can be found on the `webVitals` global namespace. - ['CLS', 'FID', 'LCP', 'INP', 'TTFB'] - .map((metric) => window.webVitals[`on${metric}`]) - .filter((metricFn) => typeof metricFn === 'function') - .forEach((metricFn) => metricFn(storeCWV, { reportAllChanges: featureToggle() })); + ['FID', 'INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => { + // When loading `web-vitals` using a classic script, all the public + // methods can be found on the `webVitals` global namespace. + const metricFn = window.webVitals[`on${metric}`]; + if (typeof metricFn === 'function') { + metricFn(storeCWV, isEager(metric) ? { reportAllChanges: featureToggle() } : undefined); + } + }); }; document.head.appendChild(script); }, 2000); // wait for delayed From 3a41b52676484d44585e8b0debe7352fe7a2e5ac Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 15 Feb 2024 18:27:36 +0100 Subject: [PATCH 3/4] fix(cwv): small change --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index cb256b5..73ebba7 100644 --- a/src/index.js +++ b/src/index.js @@ -116,7 +116,8 @@ function addCWVTracking() { // methods can be found on the `webVitals` global namespace. const metricFn = window.webVitals[`on${metric}`]; if (typeof metricFn === 'function') { - metricFn(storeCWV, isEager(metric) ? { reportAllChanges: featureToggle() } : undefined); + const opts = isEager(metric) ? { reportAllChanges: featureToggle() } : undefined; + metricFn(storeCWV, opts); } }); }; From 542fd6a050fdb911b4fb5faad7f83d0843af4d1e Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 15 Feb 2024 18:29:03 +0100 Subject: [PATCH 4/4] fix(cwv): small change --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 73ebba7..30820b6 100644 --- a/src/index.js +++ b/src/index.js @@ -111,9 +111,9 @@ function addCWVTracking() { const featureToggle = () => window.location.hostname === 'blog.adobe.com'; const isEager = (metric) => ['CLS', 'LCP'].includes(metric); + // When loading `web-vitals` using a classic script, all the public + // methods can be found on the `webVitals` global namespace. ['FID', 'INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => { - // When loading `web-vitals` using a classic script, all the public - // methods can be found on the `webVitals` global namespace. const metricFn = window.webVitals[`on${metric}`]; if (typeof metricFn === 'function') { const opts = isEager(metric) ? { reportAllChanges: featureToggle() } : undefined;