Skip to content

Commit

Permalink
Merge pull request #122 from ekremney/report-cwv-changes
Browse files Browse the repository at this point in the history
fix(cwv): report metrics eagerly
  • Loading branch information
trieloff authored Feb 28, 2024
2 parents 1ebc895 + 542fd6a commit 536b3b4
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ function addCWVTracking() {
data.cwv[measurement.name] = measurement.value;
sampleRUM('cwv', data);
};

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[`get${metric}`])
.filter((metric) => typeof metric === 'function')
.forEach((invokeMetric) => {
invokeMetric(storeCWV);
});
['FID', 'INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => {
const metricFn = window.webVitals[`on${metric}`];
if (typeof metricFn === 'function') {
const opts = isEager(metric) ? { reportAllChanges: featureToggle() } : undefined;
metricFn(storeCWV, opts);
}
});
};
document.head.appendChild(script);
}, 2000); // wait for delayed
Expand Down

0 comments on commit 536b3b4

Please sign in to comment.