Skip to content

Commit 15d2327

Browse files
committed
adding ability to track all changes for web vitals
1 parent 13a3700 commit 15d2327

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

packages/core/src/config/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ export interface Config<P = APIEvent> {
163163
*/
164164
trackWebVitalsAttribution?: boolean;
165165

166+
/**
167+
* Report all changes for web vitals (default: false)
168+
*
169+
* In most cases, you only want the callback function to be called when the metric is ready to be reported.
170+
* However, it is possible to report every change (e.g. each larger layout shift as it happens)
171+
* by setting reportAllChanges to true.
172+
*/
173+
reportAllWebVitalChanges?: boolean;
174+
166175
/**
167176
* Configuration for the console instrumentation
168177
*/

packages/web-sdk/src/instrumentations/webVitals/instrumentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class WebVitalsInstrumentation extends BaseInstrumentation {
1515

1616
private intializeWebVitalsInstrumentation() {
1717
if (this.config.trackWebVitalsAttribution) {
18-
return new WebVitalsWithAttribution(this.api.pushMeasurement);
18+
return new WebVitalsWithAttribution(this.api.pushMeasurement, this.config.reportAllWebVitalChanges);
1919
}
20-
return new WebVitalsBasic(this.api.pushMeasurement);
20+
return new WebVitalsBasic(this.api.pushMeasurement, this.config.reportAllWebVitalChanges);
2121
}
2222
}

packages/web-sdk/src/instrumentations/webVitals/webVitalsBasic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class WebVitalsBasic {
1212
ttfb: onTTFB,
1313
};
1414

15-
constructor(private pushMeasurement: MeasurementsAPI['pushMeasurement']) {}
15+
constructor(private pushMeasurement: MeasurementsAPI['pushMeasurement'], private reportAllChanges?: boolean) {}
1616

1717
initialize(): void {
1818
Object.entries(WebVitalsBasic.mapping).forEach(([indicator, executor]) => {
@@ -24,7 +24,7 @@ export class WebVitalsBasic {
2424
[indicator]: metric.value,
2525
},
2626
});
27-
});
27+
}, { reportAllChanges: this.reportAllChanges });
2828
});
2929
}
3030
}

packages/web-sdk/src/instrumentations/webVitals/webVitalsWithAttribution.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const loadStateKey = 'load_state';
1616
const timeToFirstByteKey = 'time_to_first_byte';
1717

1818
export class WebVitalsWithAttribution {
19-
constructor(private corePushMeasurement: MeasurementsAPI['pushMeasurement']) {}
19+
constructor(private corePushMeasurement: MeasurementsAPI['pushMeasurement'], private reportAllChanges?: boolean) {}
2020

2121
initialize(): void {
2222
this.measureCLS();
@@ -40,7 +40,7 @@ export class WebVitalsWithAttribution {
4040
this.addIfPresent(context, 'largest_shift_target', largestShiftTarget);
4141

4242
this.pushMeasurement(values, context);
43-
});
43+
}, { reportAllChanges: this.reportAllChanges });
4444
}
4545

4646
private measureFCP(): void {
@@ -55,7 +55,7 @@ export class WebVitalsWithAttribution {
5555
this.addIfPresent(context, loadStateKey, loadState);
5656

5757
this.pushMeasurement(values, context);
58-
});
58+
}, { reportAllChanges: this.reportAllChanges });
5959
}
6060

6161
private measureFID(): void {
@@ -71,7 +71,7 @@ export class WebVitalsWithAttribution {
7171
this.addIfPresent(context, loadStateKey, loadState);
7272

7373
this.pushMeasurement(values, context);
74-
});
74+
}, { reportAllChanges: this.reportAllChanges });
7575
}
7676

7777
private measureINP(): void {
@@ -100,7 +100,7 @@ export class WebVitalsWithAttribution {
100100
this.addIfPresent(context, 'interaction_type', interactionType);
101101

102102
this.pushMeasurement(values, context);
103-
});
103+
}, { reportAllChanges: this.reportAllChanges });
104104
}
105105

106106
private measureLCP(): void {
@@ -118,7 +118,7 @@ export class WebVitalsWithAttribution {
118118
this.addIfPresent(context, 'element', element);
119119

120120
this.pushMeasurement(values, context);
121-
});
121+
}, { reportAllChanges: this.reportAllChanges });
122122
}
123123

124124
private measureTTFB(): void {
@@ -135,7 +135,7 @@ export class WebVitalsWithAttribution {
135135
const context = this.buildInitialContext(metric);
136136

137137
this.pushMeasurement(values, context);
138-
});
138+
}, { reportAllChanges: this.reportAllChanges });
139139
}
140140

141141
private buildInitialValues(metric: Metric): Values {

0 commit comments

Comments
 (0)