* Tick the layout shift metric, following the latest CWV standard. This uses * a 5s maximum window with a 1s gap between events to define a "session". * We report the maximum session value over the lifetime of the page as the CLS. * * @param {!LayoutShift} entry
(entry)
| 590 | * @param {!LayoutShift} entry |
| 591 | */ |
| 592 | tickLayoutShiftScore_(entry) { |
| 593 | if (!this.ampdoc_) { |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | if (this.isVisibilityHidden_()) { |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | const entries = this.layoutShiftEntries_; |
| 602 | if (entries.length > 0) { |
| 603 | const first = entries[0]; |
| 604 | const last = entries[entries.length - 1]; |
| 605 | if ( |
| 606 | entry.startTime - last.startTime < CLS_SESSION_GAP && |
| 607 | entry.startTime - first.startTime < CLS_SESSION_MAX |
| 608 | ) { |
| 609 | // This entry continues the current CLS window. |
| 610 | entries.push(entry); |
| 611 | return; |
| 612 | } |
| 613 | // This entry is the start of a new CLS window, but we haven't flushed the old value yet. |
| 614 | this.flushLayoutShiftScore_(); |
| 615 | } |
| 616 | entries.push(entry); |
| 617 | // Ensure we report the CLS when the session closes. We're not guaranteed |
| 618 | // to get more LayoutShift entries, so we need some setTimeout magic to |
| 619 | // ensure it happens. |
| 620 | this.debouncedFlushLayoutShiftScore_(); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Records the normalized CLS score, following the latest CWV standard. |
no test coverage detected