Skip to content

Commit

Permalink
adding ability to track all changes for web vitals
Browse files Browse the repository at this point in the history
  • Loading branch information
eskirk committed Feb 18, 2025
1 parent 13a3700 commit 15d2327
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ export interface Config<P = APIEvent> {
*/
trackWebVitalsAttribution?: boolean;

/**
* Report all changes for web vitals (default: false)
*
* In most cases, you only want the callback function to be called when the metric is ready to be reported.
* However, it is possible to report every change (e.g. each larger layout shift as it happens)
* by setting reportAllChanges to true.
*/
reportAllWebVitalChanges?: boolean;

/**
* Configuration for the console instrumentation
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class WebVitalsInstrumentation extends BaseInstrumentation {

private intializeWebVitalsInstrumentation() {
if (this.config.trackWebVitalsAttribution) {
return new WebVitalsWithAttribution(this.api.pushMeasurement);
return new WebVitalsWithAttribution(this.api.pushMeasurement, this.config.reportAllWebVitalChanges);
}
return new WebVitalsBasic(this.api.pushMeasurement);
return new WebVitalsBasic(this.api.pushMeasurement, this.config.reportAllWebVitalChanges);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class WebVitalsBasic {
ttfb: onTTFB,
};

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

initialize(): void {
Object.entries(WebVitalsBasic.mapping).forEach(([indicator, executor]) => {
Expand All @@ -24,7 +24,7 @@ export class WebVitalsBasic {
[indicator]: metric.value,
},
});
});
}, { reportAllChanges: this.reportAllChanges });
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const loadStateKey = 'load_state';
const timeToFirstByteKey = 'time_to_first_byte';

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

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

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

this.pushMeasurement(values, context);
});
}, { reportAllChanges: this.reportAllChanges });
}

private buildInitialValues(metric: Metric): Values {
Expand Down

0 comments on commit 15d2327

Please sign in to comment.