Skip to content

Commit 2989f73

Browse files
authored
ref(tracing): Remove updated CLS from web-vitals (getsentry#3822)
Switch to using the new definition of CLS and stop tracking both the old and new definition.
1 parent 316566e commit 2989f73

File tree

3 files changed

+37
-117
lines changed

3 files changed

+37
-117
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { msToSec } from '../utils';
99
import { getCLS, LayoutShift } from './web-vitals/getCLS';
1010
import { getFID } from './web-vitals/getFID';
1111
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
12-
import { getUpdatedCLS } from './web-vitals/getUpdatedCLS';
1312
import { getVisibilityWatcher } from './web-vitals/lib/getVisibilityWatcher';
1413
import { NavigatorDeviceMemory, NavigatorNetworkInformation } from './web-vitals/types';
1514

@@ -22,7 +21,6 @@ export class MetricsInstrumentation {
2221
private _performanceCursor: number = 0;
2322
private _lcpEntry: LargestContentfulPaint | undefined;
2423
private _clsEntry: LayoutShift | undefined;
25-
private _updatedClsEntry: LayoutShift | undefined;
2624

2725
public constructor() {
2826
if (!isNodeEnv() && global?.performance) {
@@ -189,10 +187,10 @@ export class MetricsInstrumentation {
189187
});
190188
}
191189

192-
// If FCP is not recorded we should not record the updated cls value
190+
// If FCP is not recorded we should not record the cls value
193191
// according to the new definition of CLS.
194192
if (!('fcp' in this._measurements)) {
195-
delete this._measurements['updated-cls'];
193+
delete this._measurements.cls;
196194
}
197195

198196
transaction.setMeasurements(this._measurements);
@@ -229,17 +227,13 @@ export class MetricsInstrumentation {
229227
transaction.setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)),
230228
);
231229
}
232-
233-
if (this._updatedClsEntry && this._updatedClsEntry.sources) {
234-
logger.log('[Measurements] Adding Updated CLS Data');
235-
this._updatedClsEntry.sources.forEach((source, index) =>
236-
transaction.setTag(`updated-cls.source.${index + 1}`, htmlTreeAsString(source.node)),
237-
);
238-
}
239230
}
240231

241232
/** Starts tracking the Cumulative Layout Shift on the current page. */
242233
private _trackCLS(): void {
234+
// See:
235+
// https://web.dev/evolving-cls/
236+
// https://web.dev/cls-web-tooling/
243237
getCLS(metric => {
244238
const entry = metric.entries.pop();
245239
if (!entry) {
@@ -250,20 +244,6 @@ export class MetricsInstrumentation {
250244
this._measurements['cls'] = { value: metric.value };
251245
this._clsEntry = entry as LayoutShift;
252246
});
253-
254-
// See:
255-
// https://web.dev/evolving-cls/
256-
// https://web.dev/cls-web-tooling/
257-
getUpdatedCLS(metric => {
258-
const entry = metric.entries.pop();
259-
if (!entry) {
260-
return;
261-
}
262-
263-
logger.log('[Measurements] Adding Updated CLS');
264-
this._measurements['updated-cls'] = { value: metric.value };
265-
this._updatedClsEntry = entry as LayoutShift;
266-
});
267247
}
268248

269249
/**

packages/tracing/src/browser/web-vitals/getCLS.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,40 @@ export const getCLS = (onReport: ReportHandler, reportAllChanges?: boolean): voi
3838
const metric = initMetric('CLS', 0);
3939
let report: ReturnType<typeof bindReporter>;
4040

41+
let sessionValue = 0;
42+
let sessionEntries: PerformanceEntry[] = [];
43+
4144
const entryHandler = (entry: LayoutShift): void => {
45+
// Only count layout shifts without recent user input.
46+
// TODO: Figure out why entry can be undefined
4247
if (entry && !entry.hadRecentInput) {
43-
(metric.value as number) += entry.value;
44-
metric.entries.push(entry);
45-
if (report) {
46-
report();
48+
const firstSessionEntry = sessionEntries[0];
49+
const lastSessionEntry = sessionEntries[sessionEntries.length - 1];
50+
51+
// If the entry occurred less than 1 second after the previous entry and
52+
// less than 5 seconds after the first entry in the session, include the
53+
// entry in the current session. Otherwise, start a new session.
54+
if (
55+
sessionValue &&
56+
sessionEntries.length !== 0 &&
57+
entry.startTime - lastSessionEntry.startTime < 1000 &&
58+
entry.startTime - firstSessionEntry.startTime < 5000
59+
) {
60+
sessionValue += entry.value;
61+
sessionEntries.push(entry);
62+
} else {
63+
sessionValue = entry.value;
64+
sessionEntries = [entry];
65+
}
66+
67+
// If the current session value is larger than the current CLS value,
68+
// update CLS and the entries contributing to it.
69+
if (sessionValue > metric.value) {
70+
metric.value = sessionValue;
71+
metric.entries = sessionEntries;
72+
if (report) {
73+
report();
74+
}
4775
}
4876
}
4977
};

packages/tracing/src/browser/web-vitals/getUpdatedCLS.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)