Skip to content

Commit 045563e

Browse files
authored
Merge pull request #14473 from guardian/ih/configure-ab-metrics
Enable per-test configuration to force metrics collection for A/B tests under the new framework
2 parents c7cf083 + fb89de0 commit 045563e

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

ab-testing/abTest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const ABTests: ABTest[] = [
3131
type: 'client',
3232
audienceSize: 10 / 100,
3333
groups: ['control', 'variant'],
34+
shouldForceMetricsCollection: true,
3435
},
3536
{
3637
name: 'commercial-server-side-test',
@@ -42,6 +43,7 @@ export const ABTests: ABTest[] = [
4243
type: 'server',
4344
audienceSize: 10 / 100,
4445
groups: ['control', 'variant'],
46+
shouldForceMetricsCollection: true,
4547
},
4648
{
4749
name: 'commercial-large-overlap-test',
@@ -57,5 +59,6 @@ export const ABTests: ABTest[] = [
5759
audienceSize: 50 / 100,
5860
groups: ['control', 'variant'],
5961
audienceSpace: 'B',
62+
shouldForceMetricsCollection: true,
6063
},
6164
];

ab-testing/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @ts-ignore - extension is required to import this as a package in DCR
2+
import { ABTests } from './abTest.ts';
3+
4+
export { ABTests };

ab-testing/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@guardian/ab-testing",
3+
"version": "1.0.0",
4+
"description": "A/B test definitions and configuration",
5+
"main": "index.ts",
6+
"type": "module"
7+
}

ab-testing/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ type ABTest = {
3737
audienceSpace?: 'A' | 'B' | 'C';
3838
/** Test group definition */
3939
groups: string[];
40+
/** Bypasses sampling to force metrics collection for this test
41+
* See DCR Metrics component for end usage
42+
*/
43+
shouldForceMetricsCollection?: boolean;
4044
};
4145

4246
export type { ABTest, FastlyTestParams, AudienceSpace, AllSpace };

dotcom-rendering/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@emotion/react": "11.14.0",
2828
"@emotion/server": "11.11.0",
2929
"@guardian/ab-core": "8.0.0",
30+
"@guardian/ab-testing": "workspace:ab-testing",
3031
"@guardian/braze-components": "22.2.0",
3132
"@guardian/bridget": "8.7.0",
3233
"@guardian/browserslist-config": "6.1.0",

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ABTest, ABTestAPI } from '@guardian/ab-core';
2+
import { ABTests } from '@guardian/ab-testing';
23
import {
34
bypassCommercialMetricsSampling,
45
EventTimer,
@@ -31,6 +32,15 @@ const clientSideTestsToForceMetrics: ABTest[] = [
3132
/* keep array multi-line */
3233
];
3334

35+
const shouldCollectMetricsForBetaTests = (userTestParticipations: string[]) => {
36+
const userParticipationConfigs = ABTests.filter((test) =>
37+
userTestParticipations.includes(test.name),
38+
);
39+
return userParticipationConfigs.some(
40+
(test) => test.shouldForceMetricsCollection,
41+
);
42+
};
43+
3444
const useBrowserId = () => {
3545
const [browserId, setBrowserId] = useState<string>();
3646

@@ -89,20 +99,22 @@ export const Metrics = ({ commercialMetricsEnabled, tests }: Props) => {
8999

90100
const userInServerSideTest = Object.keys(tests).length > 0;
91101

92-
const betaParticipations = betaABTest?.getParticipations() ?? {};
102+
const userBetaParticipations = betaABTest?.getParticipations() ?? {};
93103

94-
const userInBetaABTest = Object.keys(betaParticipations).length > 0;
104+
const collectBetaTestMetrics = shouldCollectMetricsForBetaTests(
105+
Object.keys(userBetaParticipations),
106+
);
95107

96108
const shouldBypassSampling = useCallback(
97109
(api: ABTestAPI) =>
98110
willRecordCoreWebVitals ||
99111
userInServerSideTest ||
100-
userInBetaABTest ||
112+
collectBetaTestMetrics ||
101113
clientSideTestsToForceMetrics.some((test) =>
102114
api.runnableTest(test),
103115
),
104116

105-
[userInServerSideTest, userInBetaABTest],
117+
[userInServerSideTest, collectBetaTestMetrics],
106118
);
107119

108120
useEffect(

pnpm-lock.yaml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ packages:
22
- 'apps-rendering'
33
- 'dotcom-rendering'
44
- 'libs/**'
5+
- 'ab-testing'

0 commit comments

Comments
 (0)