diff --git a/src/log.ts b/src/log.ts index 69483b0..248eef0 100644 --- a/src/log.ts +++ b/src/log.ts @@ -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') { + 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 || {}); }; /** diff --git a/src/reportPerf.ts b/src/reportPerf.ts index 383fe1a..9edcb77 100644 --- a/src/reportPerf.ts +++ b/src/reportPerf.ts @@ -1,7 +1,7 @@ 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'; /** @@ -9,7 +9,7 @@ import { pushTask } from './utils'; */ export const reportPerf = ( measureName: string, - data: any, + data: IPerfumeData, rating: IVitalsScore, attribution: object, navigationType?: INavigationType, diff --git a/src/types.ts b/src/types.ts index c1397e7..29f25f5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; @@ -213,7 +221,10 @@ export interface IPerfumeDataConsumption { export type IPerfumeData = | number | IPerfumeNavigationTiming - | IPerfumeNetworkInformation; + | IPerfumeNetworkInformation + | IPerfumeStorageEstimate + | IPerfumeDataConsumption + | IPerformanceEntry; export type IVitalsScore = 'good' | 'needsImprovement' | 'poor' | null;