Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,33 @@ import { visibility } from './onVisibilityChange';
import { po } from './performanceObserver';
import { reportPerf } from './reportPerf';
import { initTotalBlockingTime } from './totalBlockingTime';
import { Metric } from './types';
import { IPerfumeData, Metric } from './types';
import { roundByFour } from './utils';
import { getVitalsScore } from './vitalsScore';

export const logData = (
measureName: string,
metric: any,
metric: IPerfumeData,
attribution?: object,
): void => {
Object.keys(metric).forEach(key => {
if (typeof metric[key] === 'number') {
metric[key] = roundByFour(metric[key]);
const roundNumericProperties = (data: IPerfumeData): IPerfumeData => {
if (typeof data === 'number') {
Copy link
Author

@peterpeterparker peterpeterparker Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this if statement is designed to replicate the original behavior, if I get it right. It's unclear why IPerfumeData values of type number are not rounded, unlike objects.

return data;
}
});

return Object.entries(data).reduce(
(acc, [key, value]) => ({
...acc,
[key]: typeof value === 'number' ? roundByFour(value) : value,
}),
{} as typeof data,
);
};

const convertedMetric = roundNumericProperties(metric);

// Sends the metric to an external tracking service
reportPerf(measureName, metric, null, attribution || {});
reportPerf(measureName, convertedMetric, null, attribution || {});
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/reportPerf.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { config } from './config';
import { getNavigatorInfo } from './getNavigatorInfo';
import { visibility } from './onVisibilityChange';
import { IVitalsScore, INavigationType } from './types';
import { IVitalsScore, INavigationType, IPerfumeData } from './types';
import { pushTask } from './utils';

/**
* Sends the User timing measure to analyticsTracker
*/
export const reportPerf = (
measureName: string,
data: any,
data: IPerfumeData,
rating: IVitalsScore,
attribution: object,
navigationType?: INavigationType,
Expand Down
13 changes: 12 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ export interface IPerfumeNetworkInformation {
saveData?: boolean;
}

export interface IPerfumeStorageEstimate {
quota: number | null;
usage: number | null;
caches: number | null;
indexedDB: number | null;
serviceWorker: number | null;
}

export interface IPerfumeDataConsumption {
beacon: number;
css: number;
Expand All @@ -213,7 +221,10 @@ export interface IPerfumeDataConsumption {
export type IPerfumeData =
| number
| IPerfumeNavigationTiming
| IPerfumeNetworkInformation;
| IPerfumeNetworkInformation
| IPerfumeStorageEstimate
| IPerfumeDataConsumption
| IPerformanceEntry;

export type IVitalsScore = 'good' | 'needsImprovement' | 'poor' | null;

Expand Down