Skip to content

Commit 741a3f1

Browse files
jarstelfoxdashed
andcommitted
Metrics: Tag CLS elements
This will help people dig into pages with bad layout shifts Co-authored-by: Alberto Leal <mail4alberto@gmail.com>
1 parent 688a986 commit 741a3f1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNode
66
import { Span } from '../span';
77
import { Transaction } from '../transaction';
88
import { msToSec } from '../utils';
9-
import { getCLS } from './web-vitals/getCLS';
9+
import { getCLS, LayoutShift } from './web-vitals/getCLS';
1010
import { getFID } from './web-vitals/getFID';
1111
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
1212
import { getFirstHidden } from './web-vitals/lib/getFirstHidden';
@@ -20,6 +20,7 @@ export class MetricsInstrumentation {
2020

2121
private _performanceCursor: number = 0;
2222
private _lcpEntry: LargestContentfulPaint | undefined;
23+
private _clsEntry: LayoutShift | undefined;
2324

2425
public constructor() {
2526
if (!isNodeEnv() && global?.performance) {
@@ -207,6 +208,13 @@ export class MetricsInstrumentation {
207208

208209
transaction.setTag('lcp.size', this._lcpEntry.size);
209210
}
211+
212+
if (this._clsEntry) {
213+
logger.log('[Measurements] Adding CLS Data');
214+
transaction.setData('measurements.cls', {
215+
sources: this._clsEntry.sources.map(source => htmlTreeAsString(source.node)),
216+
});
217+
}
210218
}
211219
}
212220

@@ -221,6 +229,7 @@ export class MetricsInstrumentation {
221229

222230
logger.log('[Measurements] Adding CLS');
223231
this._measurements['cls'] = { value: metric.value };
232+
this._clsEntry = entry as LayoutShift;
224233
});
225234
}
226235

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ import { onHidden } from './lib/onHidden';
2121
import { ReportHandler } from './types';
2222

2323
// https://wicg.github.io/layout-instability/#sec-layout-shift
24-
interface LayoutShift extends PerformanceEntry {
24+
export interface LayoutShift extends PerformanceEntry {
2525
value: number;
2626
hadRecentInput: boolean;
27+
sources: Array<LayoutShiftAttribution>;
28+
toJSON(): Record<string, unknown>;
29+
}
30+
31+
export interface LayoutShiftAttribution {
32+
node?: Node;
33+
previousRect: DOMRectReadOnly;
34+
currentRect: DOMRectReadOnly;
2735
}
2836

2937
export const getCLS = (onReport: ReportHandler, reportAllChanges = false): void => {

0 commit comments

Comments
 (0)