uxm
Version:
User experience metrics
184 lines (183 loc) • 5.05 kB
TypeScript
/**
* @typedef {'phone' | 'tablet' | 'desktop' | null} DeviceType
* @typedef {'full' | 'lite' | null} DeviceMemory
* @typedef {'slow-2g' | '2g' | '3g' | '4g' | null} EffectiveConnectionType
*
* @typedef {object} UserTiming
* @prop {'mark' | 'measure'} type
* @prop {string} name
* @prop {number} startTime
* @prop {number} [duration]
*
* @typedef {object} Resource
* @prop {string} url
* @prop {string} type
* @prop {number} size
* @prop {number} startTime
* @prop {number} duration
*
* @typedef {object} LongTask
* @prop {number} startTime
* @prop {number} duration
*
* @typedef {object} UxmResult
* @prop {DeviceType} deviceType
* @prop {EffectiveConnectionType} effectiveConnectionType
* @prop {number | null} timeToFirstByte
* @prop {number | null} firstPaint
* @prop {number | null} firstContentfulPaint
* @prop {number | null} domContentLoaded
* @prop {number | null} onLoad
* @prop {string} [url]
* @prop {string} [userAgent]
* @prop {DeviceMemory | null} [deviceMemory]
* @prop {UserTiming[] | null} [userTiming]
* @prop {LongTask[] | null} [longTasks]
* @prop {Resource[] | null} [resources]
*/
/**
* Get all metrics.
*
* @param {{ all?: boolean, url?: boolean, userAgent?: boolean, deviceMemory?: boolean, userTiming?: boolean, longTasks?: boolean, resources?: boolean }} [opts]
* @return {Promise<UxmResult>}
*/
export function uxm(opts?: {
all?: boolean | undefined;
url?: boolean | undefined;
userAgent?: boolean | undefined;
deviceMemory?: boolean | undefined;
userTiming?: boolean | undefined;
longTasks?: boolean | undefined;
resources?: boolean | undefined;
} | undefined): Promise<UxmResult>;
/**
* Create a custom performance mark with `markName` name.
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/mark
*
* @param {string} markName
*/
export function mark(markName: string): void;
/**
* Create performance measurement `measureName` between marks, the navigation start time, or `startMarkName`.
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/measure
*
* @param {string} measureName
* @param {string} [startMarkName]
*/
export function measure(measureName: string, startMarkName?: string | undefined): void;
/**
* Get effective connection type.
* https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
*
* @return {EffectiveConnectionType}
*/
export function getEffectiveConnectionType(): "slow-2g" | "2g" | "3g" | "4g" | null;
/**
* Get `first-paint` mark.
*
* @return {number | null}
*/
export function getFirstPaint(): number | null;
/**
* Get `first-contentful-paint` mark.
*
* @return {number | null}
*/
export function getFirstContentfulPaint(): number | null;
/**
* Get server response timestamp.
*
* @return {number | null}
*/
export function getTimeToFirstByte(): number | null;
/**
* Get `DomContentLoaded` event timestamp.
*
* @return {number | null}
*/
export function getDomContentLoaded(): number | null;
/**
* Get `load` event timestamp.
*
* @return {number | null}
*/
export function getOnLoad(): number | null;
/**
* Get device type.
* based on https://github.com/matthewhudson/current-device/blob/master/src/index.js
*
* @param {string} [ua]
* @return {DeviceType}
*/
export function getDeviceType(ua?: string | undefined): "phone" | "tablet" | "desktop" | null;
/**
* Get current url.
*
* @return {String}
*/
export function getUrl(): string;
/**
* Get user-agent.
*
* @return {String}
*/
export function getUserAgent(): string;
/**
* Get effective device memory.
*
* @return {DeviceMemory | null}
*/
export function getDeviceMemory(): "full" | "lite" | null;
/**
* Get all user timings.
*
* @return {UserTiming[] | null}
*/
export function getUserTiming(): UserTiming[] | null;
/**
* Get resources.
*
* @return {Resource[] | null}
*/
export function getResources(): Resource[] | null;
/**
* Get collected long tasks.
*
* @return {LongTask[] | null}
*/
export function getLongTasks(): LongTask[] | null;
export type DeviceType = "phone" | "tablet" | "desktop" | null;
export type DeviceMemory = "full" | "lite" | null;
export type EffectiveConnectionType = "slow-2g" | "2g" | "3g" | "4g" | null;
export type UserTiming = {
type: "mark" | "measure";
name: string;
startTime: number;
duration?: number;
};
export type Resource = {
url: string;
type: string;
size: number;
startTime: number;
duration: number;
};
export type LongTask = {
startTime: number;
duration: number;
};
export type UxmResult = {
deviceType: "phone" | "tablet" | "desktop" | null;
effectiveConnectionType: "slow-2g" | "2g" | "3g" | "4g" | null;
timeToFirstByte: number | null;
firstPaint: number | null;
firstContentfulPaint: number | null;
domContentLoaded: number | null;
onLoad: number | null;
url?: string;
userAgent?: string;
deviceMemory?: "full" | "lite" | null;
userTiming?: UserTiming[] | null;
longTasks?: LongTask[] | null;
resources?: Resource[] | null;
};