Skip to content

Update CLS metric to match newest CWV standard#36147

Merged
jridgewell merged 8 commits intoampproject:mainfrom
jridgewell:cls
Sep 24, 2021
Merged

Update CLS metric to match newest CWV standard#36147
jridgewell merged 8 commits intoampproject:mainfrom
jridgewell:cls

Conversation

@jridgewell
Copy link
Copy Markdown
Contributor

The CLS metric used to be a sum of the shifts over the entire lifetime of the page. However, Google has changed this so that it's now defined as the largest sum over a windowed session (see https://web.dev/evolving-cls/).

We're still going to record the old value, but it's now called cls-1. Regular cls will now be the windowed session value. Additionally, we'll record the types of nodes that contribute to the CLS (as clstu, standing for CLS-Type Union), so that we can better understand what's causing it.

Copy link
Copy Markdown
Member

@samouri samouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often do you want cls to be ticked? AFAICT this could happen many times in a single session

}
entries.push(entry);
// Ensure we report the CLS when the session closes. We're not guaranteed
// to get more LayoutShift entires, so we need some setTimeout magic to
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to get more LayoutShift entires, so we need some setTimeout magic to
// to get more LayoutShift entries, so we need some setTimeout magic to

// We'll record the largest windowed CLS.
this.metrics_.reset(TickLabel.CUMULATIVE_LAYOUT_SHIFT);
this.metrics_.reset(TickLabel.CUMULATIVE_LAYOUT_SHIFT_TYPE_UNION);
this.tickDelta(TickLabel.CUMULATIVE_LAYOUT_SHIFT, sum);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should calling tick on an already reported metric automatically reset it? I.e. so you don't need to do so here as well.

The CLS metric used to be a sum of the shifts over the entire lifetime of the page. However, Google has changed this so that it's now defined as the largest sum over a windowed session (see https://web.dev/evolving-cls/).

We're still going to record the old value, but it's now called `cls-1`. Regular `cls` will now be the windowed session value. Additionally, we'll record the types of nodes that contribute to the CLS (as `clstu`, standing for CLS-Type Union), so that we can better understand what's causing it.
@jridgewell jridgewell merged commit 202ac4f into ampproject:main Sep 24, 2021
@jridgewell jridgewell deleted the cls branch September 24, 2021 18:01
| Layout Jank, first exit | `lj` | The aggregate jank score when the user leaves the page (navigation, tab switching, dismissing application) for the first time. See https://gist.github.com/skobes/2f296da1b0a88cc785a4bf10a42bca07 |
| Layout Jank, second exit | `lj-2` | The aggregate jank score when the user leaves the page (navigation, tab switching, dismissing application) for the second time. |
| Cumulative Layout Shift, first exit | `cls` | The aggregate layout shift score when the user leaves the page (navigation, tab switching, dismissing application) for the first time. See https://web.dev/layout-instability-api |
| Cumulative Layout Shift | `cls` | The current maximum layout shift score with 5s windows and a 1s session gap. See https://web.dev/layout-instability-api |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duped?

* @private {Array<LayoutShift>}
*/
this.layoutShifts_ = [];
this.layoutShiftEntires_ = [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: entires --> entries

jridgewell added a commit to jridgewell/amphtml that referenced this pull request Oct 8, 2021
In ampproject#36147, we switched to the new windowed CLS metric. This new algorithm can send multiple CLS scores, where each new score is larger than the last.

Because of this multiple-sends-which-are-larger-than-the-last behavior, our CLS scores have gone up marginally. The p95 is considering every CLS score
sent by the page, instead of just the most recent. In order to determine which is the most recent, we'll add a random `eventid` to our `sendCsi` message,
which can then be used when aggregating.
jridgewell added a commit to jridgewell/amphtml that referenced this pull request Oct 8, 2021
In ampproject#36147, we switched to the new windowed CLS metric. This new algorithm can send multiple CLS scores, where each new score is larger than the last.

Because of this multiple-sends-which-are-larger-than-the-last behavior, our CLS scores have gone up marginally. The p95 is considering every CLS score
sent by the page, instead of just the most recent. In order to determine which is the most recent, we'll add a random `eventid` to our `sendCsi` message,
which can then be used when aggregating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants