Skip to content

Commit

Permalink
Fixes bejamas#3 CLS event values sent to GA aren't useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
laradevitt committed Jun 8, 2021
1 parent 1c12462 commit c134430
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ or
}
```

## Important note

Because Google Analytics metrics must be integers, all event values are rounded
before being sent. To be meaningful, CLS values are also multiplied by 1000.
This is the approach recommended in the [Web Vitals documentation](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics) and will be
important to keep in mind when preparing your reports.

---

Plugin based on [`nuxt-vitals`](https://github.com/daliborgogic/nuxt-vitals).
4 changes: 3 additions & 1 deletion src/web-vitals.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function sendToAnalytics(metric, options) {
ea: name,
el: id,
// Google Analytics metrics must be integers, so the value is rounded.
ev: parseInt(delta),
// For CLS the value is first multiplied by 1000 for greater precision.
ev: Math.round(name === 'CLS' ? delta * 1000 : delta),
dl: location.href,
dp: location.pathname,
ni: true,
Expand All @@ -52,6 +53,7 @@ function sendToAnalytics(metric, options) {
const url = encode(obj);

if (options.debug) {
onDebug(name + " original value", delta);
onDebug(name, JSON.stringify(obj, null, 2));
onDebug(name + " URL", url);
}
Expand Down

0 comments on commit c134430

Please sign in to comment.