Skip to content

Commit a900031

Browse files
committed
rework logic and rename functions to make the metrics sampling code easier to grok
1 parent b27a03a commit a900031

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

dotcom-rendering/src/components/Metrics.importable.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Props = {
2424

2525
const sampling = 1 / 100;
2626
/** defining this here allows to share this with other metrics */
27-
const willRecordCoreWebVitals = Math.random() < sampling;
27+
const isInSample = Math.random() < sampling;
2828

2929
// For these tests switch off sampling and collect metrics for 100% of views
3030
const clientSideTestsToForceMetrics: ABTest[] = [
@@ -88,9 +88,8 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
8888

8989
const userInServerSideTest = Object.keys(tests).length > 0;
9090

91-
const shouldBypassSampling = useCallback(
91+
const bypassSampling = useCallback(
9292
(api: ABTestAPI) =>
93-
willRecordCoreWebVitals ||
9493
userInServerSideTest ||
9594
clientSideTestsToForceMetrics.some((test) =>
9695
api.runnableTest(test),
@@ -105,7 +104,7 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
105104
if (isUndefined(isDev)) return;
106105
if (isUndefined(pageViewId)) return;
107106

108-
const bypassSampling = shouldBypassSampling(abTestApi);
107+
const recordMetrics = isInSample || bypassSampling(abTestApi);
109108

110109
/**
111110
* We rely on `bypassSampling` rather than the built-in sampling,
@@ -121,11 +120,11 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
121120
team: 'dotcom',
122121
});
123122

124-
if (bypassSampling || isDev) {
123+
if (recordMetrics || isDev) {
125124
void bypassCoreWebVitalsSampling('commercial');
126125
}
127126
},
128-
[abTestApi, browserId, isDev, pageViewId, shouldBypassSampling],
127+
[abTestApi, browserId, isDev, pageViewId, bypassSampling],
129128
);
130129

131130
useEffect(
@@ -139,7 +138,7 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
139138
if (isUndefined(isDev)) return;
140139
if (isUndefined(pageViewId)) return;
141140

142-
const bypassSampling = shouldBypassSampling(abTestApi);
141+
const recordMetrics = isInSample || bypassSampling(abTestApi);
143142

144143
// This is a new detection method we are trying, so we want to record it separately to `adBlockerInUse`
145144
EventTimer.get().setProperty(
@@ -154,7 +153,7 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
154153
adBlockerInUse,
155154
})
156155
.then(() => {
157-
if (bypassSampling || isDev) {
156+
if (recordMetrics || isDev) {
158157
void bypassCommercialMetricsSampling();
159158
}
160159
})
@@ -172,7 +171,7 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
172171
commercialMetricsEnabled,
173172
isDev,
174173
pageViewId,
175-
shouldBypassSampling,
174+
bypassSampling,
176175
],
177176
);
178177

0 commit comments

Comments
 (0)