Skip to content

Commit

Permalink
Setup multiple operations for A/B testing use case
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkiklee committed Jul 26, 2023
1 parent b1041b5 commit 9383b39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sites/content-producer/url-selection/ab-testing-worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,26 @@ class SelectURLOperation {
}
}

function getBucketForTestingGroup(testingGroup) {
switch (testingGroup) {
case 'control':
return 0;
case 'experiment-a':
return 1;
case 'experiment-b':
return 2;
}
}

class ExperimentGroupReportingOperation {
async run() {
const experimentGroup = await sharedStorage.get('ab-testing-group');

const bucket = BigInt(getBucketForTestingGroup(experimentGroup));
privateAggregation.contributeToHistogram({ bucket, value: 1 });
}
}

// Register the operation as 'ab-testing'
register('ab-testing', SelectURLOperation);
register('experiment-group-reporting', ExperimentGroupReportingOperation);
4 changes: 4 additions & 0 deletions sites/content-producer/url-selection/ab-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async function injectAd() {
const selectedUrl = await window.sharedStorage.selectURL('ab-testing', urls, {
data: groups,
resolveToConfig,
keepAlive: true,
});

const adSlot = document.getElementById('ad-slot');
Expand All @@ -68,6 +69,9 @@ async function injectAd() {
} else {
adSlot.src = selectedUrl;
}

// Run the reporting operation
await window.sharedStorage.run('experiment-group-reporting')
}

injectAd();

0 comments on commit 9383b39

Please sign in to comment.