Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Time to First Byte collection #5

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 17 additions & 0 deletions documents/adr/001-web-vitals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 001-web-vitals

## Status

Approved

## Context

There is good adoption of synthetic performance testing tools, particularly Google Lighthouse and [Page Speed Insights](https://pagespeed.web.dev). There are not as many widely deployed tools to anonymously collect field measurements of page speed performance from real users in a way that adequately captures their experience when using federal web properties.

## Decision

Google Analytics through the Digital Analytics Program within the federal government already has good adoption within the federal government. The `web-vitals.js` library from Google was selected as a way to instrument a website or application with performance collection. This library was chosen for its stability, as well as its completeness, as it is the basis of many commercial offerings (e.g. DataDog, Sentry) and its wide deployment through the Chrome Devtools Performance Panel.

## Consequences

This proposes an optional add-on for teams to be able to add and collect metrics on what a real user experiences as it relates to the universal performance indicators known as Core Web Vitals.
63 changes: 0 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/format-event-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import type {
FCPAttribution,
INPAttribution,
LCPAttribution,
TTFBAttribution,
} from 'web-vitals';

export type WebVitalsName = 'CLS' | 'FCP' | 'INP' | 'LCP';
export type WebVitalsName = 'CLS' | 'FCP' | 'INP' | 'LCP' | 'TTFB';
export type WebVitalsAttribution =
| CLSAttribution
| FCPAttribution
| INPAttribution
| LCPAttribution;
| LCPAttribution
| TTFBAttribution;

export const formatEventData = (
name: WebVitalsName,
Expand Down Expand Up @@ -69,6 +71,18 @@ export const formatEventData = (
debug_target: (attribution as LCPAttribution).element || '(not set)',
};
}
if (name === 'TTFB') {
return {
debug_waiting_duration: (attribution as TTFBAttribution)
.waitingDuration,
debug_dns_duration: (attribution as TTFBAttribution).dnsDuration,
debug_connection_duration: (attribution as TTFBAttribution)
.connectionDuration,
debug_cache_duration: (attribution as TTFBAttribution).cacheDuration,
debug_request_duration: (attribution as TTFBAttribution)
.requestDuration,
};
}
}
// Return default/empty params in case there is no attribution.
return {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { onCLS, onINP, onFCP, onLCP } from './web-vitals.js';
import { onCLS, onINP, onFCP, onLCP, onTTFB } from './web-vitals.js';
import { sendToAnalytics } from './send-to-analytics.js';

const initWebVitalsEvents = () => {
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);
onTTFB(sendToAnalytics);
};

if (typeof window !== 'undefined' && 'gas4' in window) {
Expand Down
4 changes: 3 additions & 1 deletion src/send-to-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
FCPMetricWithAttribution,
INPMetricWithAttribution,
LCPMetricWithAttribution,
TTFBMetricWithAttribution,
} from 'web-vitals';

import { formatEventData, type WebVitalsName } from './format-event-data.js';
Expand All @@ -11,7 +12,8 @@ export type WebVitalsWithAttribution =
| CLSMetricWithAttribution
| FCPMetricWithAttribution
| INPMetricWithAttribution
| LCPMetricWithAttribution;
| LCPMetricWithAttribution
| TTFBMetricWithAttribution;

declare const gas4: any;

Expand Down
4 changes: 2 additions & 2 deletions src/web-vitals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { onCLS, onINP, onFCP, onLCP } from 'web-vitals/attribution';
import { onCLS, onINP, onFCP, onLCP, onTTFB } from 'web-vitals/attribution';

export { onCLS, onINP, onFCP, onLCP };
export { onCLS, onINP, onFCP, onLCP, onTTFB };
111 changes: 111 additions & 0 deletions test/unit/format-event-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ describe('formatEventData', () => {
});
});

it('should format TTFB data correctly', () => {
const attribution = {
waitingDuration: 0,
cacheDuration: 0,
dnsDuration: 0,
connectionDuration: 2015,
requestDuration: 47,
};

// @ts-ignore
const result = formatEventData('TTFB', attribution);

expect(result).toEqual({
debug_cache_duration: attribution.cacheDuration,
debug_connection_duration: attribution.connectionDuration,
debug_dns_duration: attribution.dnsDuration,
debug_request_duration: attribution.requestDuration,
debug_waiting_duration: attribution.waitingDuration,
});
});

it('should return default/empty params if no attribution data is provided', () => {
// @ts-ignore
const result = formatEventData('unknown', null);
Expand Down Expand Up @@ -453,3 +474,93 @@ describe('formatEventData', () => {
// }
// }
// }

// {
// "name": "TTFB",
// "value": 2062,
// "rating": "poor",
// "delta": 2062,
// "entries": [
// {
// "name": "http://localhost:8000/demo/",
// "entryType": "navigation",
// "startTime": 0,
// "duration": 4181,
// "initiatorType": "navigation",
// "nextHopProtocol": "http/1.1",
// "workerStart": 0,
// "redirectStart": 0,
// "redirectEnd": 0,
// "fetchStart": -21,
// "domainLookupStart": -21,
// "domainLookupEnd": -21,
// "connectStart": -21,
// "connectEnd": 2015,
// "secureConnectionStart": 0,
// "requestStart": 2016,
// "responseStart": 2062,
// "responseEnd": 2062,
// "transferSize": 4277,
// "encodedBodySize": 3977,
// "decodedBodySize": 3977,
// "responseStatus": 200,
// "contentType": "text/html",
// "serverTiming": [],
// "unloadEventStart": 0,
// "unloadEventEnd": 0,
// "domInteractive": 2126,
// "domContentLoadedEventStart": 2199,
// "domContentLoadedEventEnd": 2200,
// "domComplete": 4180,
// "loadEventStart": 4180,
// "loadEventEnd": 4181,
// "type": "navigate",
// "redirectCount": 0
// }
// ],
// "id": "v4-1732826806920-8943990242578",
// "navigationType": "navigate",
// "attribution": {
// "waitingDuration": 0,
// "cacheDuration": 0,
// "dnsDuration": 0,
// "connectionDuration": 2015,
// "requestDuration": 47,
// "navigationEntry": {
// "name": "http://localhost:8000/demo/",
// "entryType": "navigation",
// "startTime": 0,
// "duration": 4181,
// "initiatorType": "navigation",
// "nextHopProtocol": "http/1.1",
// "workerStart": 0,
// "redirectStart": 0,
// "redirectEnd": 0,
// "fetchStart": -21,
// "domainLookupStart": -21,
// "domainLookupEnd": -21,
// "connectStart": -21,
// "connectEnd": 2015,
// "secureConnectionStart": 0,
// "requestStart": 2016,
// "responseStart": 2062,
// "responseEnd": 2062,
// "transferSize": 4277,
// "encodedBodySize": 3977,
// "decodedBodySize": 3977,
// "responseStatus": 200,
// "contentType": "text/html",
// "serverTiming": [],
// "unloadEventStart": 0,
// "unloadEventEnd": 0,
// "domInteractive": 2126,
// "domContentLoadedEventStart": 2199,
// "domContentLoadedEventEnd": 2200,
// "domComplete": 4180,
// "loadEventStart": 4180,
// "loadEventEnd": 4181,
// "type": "navigate",
// "redirectCount": 0
// }
// }
// }