@guardian/core-web-vitals
安装
yarn add @guardian/core-web-vitals
或者说
npm install @guardian/core-web-vitals
接下来
import {
initCoreWebVitals,
bypassCoreWebVitalsSampling,
} from '@guardian/core-web-vitals';
捆绑
此软件包使用 ES2020。 。 。 .
如果目标环境不支持此选项, 请在捆绑您的应用程序时确保您转换此软件包 .
使用量
默认情况下,采样率将定为1%,核心网络维度将为1% 集众出使. 可将样本设定为不同值 启动或同步绕过(见下文).
import { initCoreWebVitals, getCookie } from '@guardian/core-web-vitals';
// browserId & pageViewId are needed to join up the data downstream.
const init: InitCoreWebVitalsOptions = {
browserId: getCookie({ name: 'bwid', shouldMemoize: true }),
pageViewId: guardian.config.ophan.pageViewId,
// Whether to use CODE or PROD endpoints.
isDev: window.location.hostname !== 'www.theguardian.com',
};
initCoreWebVitals(init);
init.sampling
设置一个采样率,将数据发送到伐木端点.
默认为 1 / 100。 。 。 .
const init: InitCoreWebVitalsOptions = {
isDev: false,
// Send data for 20% of page views. Inform Data Tech team about expected
// spikes in data ingestion
sampling: 20 / 100,
};
initCoreWebVitals(init);
init.team
登录有效载荷是否成功排队的可选团队名称 转移.
const init: InitCoreWebVitalsOptions = {
isDev: false,
sampling: 100 / 100,
team: 'dotcom',
};
initCoreWebVitals(init);
// should call log('dotcom', 'Core Web Vitals payload successfully queued […]')
init.platform
可选的平台,可以传递给事件收集器
用于分解下游数据. 默认为 dcr 在事件收集中,如果
未指定.
const init: InitCoreWebVitalsOptions = {
isDev: false,
platform: 'support',
};
initCoreWebVitals(init);
bypassCoreWebVitalsSampling
允许同步绕过采样速率.
使用可选的团队名称来打印日志 .
/* … after having called initCoreWebVitals() … */
addEventListener('some-event', () => {
// CWV will be sent for all page views where `some-event` was triggered
bypassCoreWebVitalsSampling();
});
类型
CoreWebVitalsPayload
type CoreWebVitalsPayload = {
page_view_id: string | null;
browser_id: string | null;
fid: null | number;
cls: null | number;
lcp: null | number;
fcp: null | number;
ttfb: null | number;
};
InitCoreWebVitalsOptions
type InitCoreWebVitalsOptions = {
isDev: boolean;
browserId?: string | null;
pageViewId?: string | null;
sampling?: number;
team?: TeamName;
};