Skip to content

Commit

Permalink
add attribution in return type
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed May 1, 2024
1 parent 8fa200a commit f563c35
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
15 changes: 9 additions & 6 deletions src/attribution/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,29 @@ const getLargestLayoutShiftSource = (sources: LayoutShiftAttribution[]) => {
return sources.find((s) => s.node && s.node.nodeType === 1) || sources[0];
};

const attributeCLS = (metric: CLSMetric): void => {
const attributeCLS = (metric: CLSMetric): CLSMetricWithAttribution => {
const metricWithAttribution = metric as CLSMetricWithAttribution;

if (metric.entries.length) {
const largestEntry = getLargestLayoutShiftEntry(metric.entries);
if (largestEntry && largestEntry.sources && largestEntry.sources.length) {
const largestSource = getLargestLayoutShiftSource(largestEntry.sources);
if (largestSource) {
(metric as CLSMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
largestShiftTarget: getSelector(largestSource.node),
largestShiftTime: largestEntry.startTime,
largestShiftValue: largestEntry.value,
largestShiftSource: largestSource,
largestShiftEntry: largestEntry,
loadState: getLoadState(largestEntry.startTime),
};
return;
return metricWithAttribution;
}
}
}
// Set an empty object if no other attribution has been set.
(metric as CLSMetricWithAttribution).attribution = {};
metricWithAttribution.attribution = {};
return metricWithAttribution;
};

/**
Expand Down Expand Up @@ -75,7 +78,7 @@ export const onCLS = (
opts?: ReportOpts,
) => {
unattributedOnCLS((metric: CLSMetric) => {
attributeCLS(metric);
onReport(metric as CLSMetricWithAttribution);
const metricWithAttribution = attributeCLS(metric);
onReport(metricWithAttribution);
}, opts);
};
18 changes: 11 additions & 7 deletions src/attribution/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,38 @@ import {isInvalidTimestamp} from '../lib/isInvalidTimestamp.js';
import {onFCP as unattributedOnFCP} from '../onFCP.js';
import {FCPMetric, FCPMetricWithAttribution, ReportOpts} from '../types.js';

const attributeFCP = (metric: FCPMetric): void => {
const attributeFCP = (metric: FCPMetric): FCPMetricWithAttribution => {
const metricWithAttribution = metric as FCPMetricWithAttribution;

if (metric.entries.length) {
const navigationEntry = getNavigationEntry();
const fcpEntry = metric.entries[metric.entries.length - 1];

if (navigationEntry) {
const responseStart = navigationEntry.responseStart;
if (isInvalidTimestamp(responseStart)) return;
// TODO(bckenny): this is wrong.
if (isInvalidTimestamp(responseStart)) return metricWithAttribution;

const activationStart = navigationEntry.activationStart || 0;
const ttfb = Math.max(0, responseStart - activationStart);

(metric as FCPMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
timeToFirstByte: ttfb,
firstByteToFCP: metric.value - ttfb,
loadState: getLoadState(metric.entries[0].startTime),
navigationEntry,
fcpEntry,
};
return;
return metricWithAttribution;
}
}
// Set an empty object if no other attribution has been set.
(metric as FCPMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
timeToFirstByte: 0,
firstByteToFCP: metric.value,
loadState: getLoadState(getBFCacheRestoreTime()),
};
return metricWithAttribution;
};

/**
Expand All @@ -62,7 +66,7 @@ export const onFCP = (
opts?: ReportOpts,
) => {
unattributedOnFCP((metric: FCPMetric) => {
attributeFCP(metric);
onReport(metric as FCPMetricWithAttribution);
const metricWithAttribution = attributeFCP(metric);
onReport(metricWithAttribution);
}, opts);
};
11 changes: 7 additions & 4 deletions src/attribution/onFID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ import {getSelector} from '../lib/getSelector.js';
import {onFID as unattributedOnFID} from '../onFID.js';
import {FIDMetric, FIDMetricWithAttribution, ReportOpts} from '../types.js';

const attributeFID = (metric: FIDMetric): void => {
const attributeFID = (metric: FIDMetric): FIDMetricWithAttribution => {
const metricWithAttribution = metric as FIDMetricWithAttribution;

const fidEntry = metric.entries[0];
(metric as FIDMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
eventTarget: getSelector(fidEntry.target),
eventType: fidEntry.name,
eventTime: fidEntry.startTime,
eventEntry: fidEntry,
loadState: getLoadState(fidEntry.startTime),
};
return metricWithAttribution;
};

/**
Expand All @@ -44,7 +47,7 @@ export const onFID = (
opts?: ReportOpts,
) => {
unattributedOnFID((metric: FIDMetric) => {
attributeFID(metric);
onReport(metric as FIDMetricWithAttribution);
const metricWithAttribution = attributeFID(metric);
onReport(metricWithAttribution);
}, opts);
};
10 changes: 6 additions & 4 deletions src/attribution/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const getIntersectingLoAFs = (
return intersectingLoAFs;
};

const attributeINP = (metric: INPMetric): void => {
const attributeINP = (metric: INPMetric): INPMetricWithAttribution => {
const firstEntry = metric.entries[0];
const renderTime = entryToRenderTimeMap.get(firstEntry)!;
const group = pendingEntriesGroupMap.get(renderTime)!;
Expand Down Expand Up @@ -220,7 +220,8 @@ const attributeINP = (metric: INPMetric): void => {

const nextPaintTime = Math.max.apply(Math, nextPaintTimeCandidates);

(metric as INPMetricWithAttribution).attribution = {
const metricWithAttribution = metric as INPMetricWithAttribution;
metricWithAttribution.attribution = {
interactionTarget: getSelector(
firstEntryWithTarget && firstEntryWithTarget.target,
),
Expand All @@ -234,6 +235,7 @@ const attributeINP = (metric: INPMetric): void => {
presentationDelay: Math.max(nextPaintTime - processingEnd, 0),
loadState: getLoadState(firstEntry.startTime),
};
return metricWithAttribution;
};

/**
Expand Down Expand Up @@ -278,8 +280,8 @@ export const onINP = (
// running in Chrome (EventTimingKeypressAndCompositionInteractionId)
// 123+ that if rolled out fully would make this no longer necessary.
whenIdle(() => {
attributeINP(metric);
onReport(metric as INPMetricWithAttribution);
const metricWithAttribution = attributeINP(metric);
onReport(metricWithAttribution);
});
}, opts);
};
18 changes: 11 additions & 7 deletions src/attribution/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import {
ReportOpts,
} from '../types.js';

const attributeLCP = (metric: LCPMetric) => {
const attributeLCP = (metric: LCPMetric): LCPMetricWithAttribution => {
const metricWithAttribution = metric as LCPMetricWithAttribution;

if (metric.entries.length) {
const navigationEntry = getNavigationEntry();

if (navigationEntry) {
const responseStart = navigationEntry.responseStart;
if (isInvalidTimestamp(responseStart)) return;
// TODO(bckenny): this is wrong.
if (isInvalidTimestamp(responseStart)) return metricWithAttribution;

const activationStart = navigationEntry.activationStart || 0;
const lcpEntry = metric.entries[metric.entries.length - 1];
Expand Down Expand Up @@ -78,17 +81,18 @@ const attributeLCP = (metric: LCPMetric) => {
attribution.lcpResourceEntry = lcpResourceEntry;
}

(metric as LCPMetricWithAttribution).attribution = attribution;
return;
metricWithAttribution.attribution = attribution;
return metricWithAttribution;
}
}
// Set an empty object if no other attribution has been set.
(metric as LCPMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
timeToFirstByte: 0,
resourceLoadDelay: 0,
resourceLoadDuration: 0,
elementRenderDelay: metric.value,
};
return metricWithAttribution;
};

/**
Expand All @@ -107,7 +111,7 @@ export const onLCP = (
opts?: ReportOpts,
) => {
unattributedOnLCP((metric: LCPMetric) => {
attributeLCP(metric);
onReport(metric as LCPMetricWithAttribution);
const metricWithAttribution = attributeLCP(metric);
onReport(metricWithAttribution);
}, opts);
};
15 changes: 9 additions & 6 deletions src/attribution/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import {onTTFB as unattributedOnTTFB} from '../onTTFB.js';
import {TTFBMetric, TTFBMetricWithAttribution, ReportOpts} from '../types.js';

const attributeTTFB = (metric: TTFBMetric): void => {
const attributeTTFB = (metric: TTFBMetric): TTFBMetricWithAttribution => {
const metricWithAttribution = metric as TTFBMetricWithAttribution;

if (metric.entries.length) {
const navigationEntry = metric.entries[0];
const activationStart = navigationEntry.activationStart || 0;
Expand All @@ -43,7 +45,7 @@ const attributeTTFB = (metric: TTFBMetric): void => {
0,
);

(metric as TTFBMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
waitingDuration: waitEnd,
cacheDuration: dnsStart - waitEnd,
// dnsEnd usually equals connectStart but use connectStart over dnsEnd
Expand All @@ -57,16 +59,17 @@ const attributeTTFB = (metric: TTFBMetric): void => {
requestDuration: metric.value - connectEnd,
navigationEntry: navigationEntry,
};
return;
return metricWithAttribution;
}
// Set an empty object if no other attribution has been set.
(metric as TTFBMetricWithAttribution).attribution = {
metricWithAttribution.attribution = {
waitingDuration: 0,
cacheDuration: 0,
dnsDuration: 0,
connectionDuration: 0,
requestDuration: 0,
};
return metricWithAttribution;
};

/**
Expand All @@ -89,7 +92,7 @@ export const onTTFB = (
opts?: ReportOpts,
) => {
unattributedOnTTFB((metric: TTFBMetric) => {
attributeTTFB(metric);
onReport(metric as TTFBMetricWithAttribution);
const metricWithAttribution = attributeTTFB(metric);
onReport(metricWithAttribution);
}, opts);
};

0 comments on commit f563c35

Please sign in to comment.