Skip to content

Commit

Permalink
Update example to use createWorklet over addModule
Browse files Browse the repository at this point in the history
We want to show more uses of the createWorklet method, this example is
a straight swap from addModule to createWorklet. In other examples I'll
remove the use of the iframe.
  • Loading branch information
taraojo committed Oct 29, 2024
1 parent 171154b commit 7a90bb3
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions sites/content-producer/url-selection/ab-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function getRandomExperiment() {

async function injectAd() {
// Load the worklet module
await window.sharedStorage.worklet.addModule('ab-testing-worklet.js');
const abTestingWorklet = await window.sharedStorage.createWorklet(
'ab-testing-worklet.js'
);

// Set the initial value in the storage to a random experiment group
window.sharedStorage.set('ab-testing-group', getRandomExperiment(), {
Expand All @@ -56,7 +58,7 @@ async function injectAd() {
const resolveToConfig = typeof window.FencedFrameConfig !== 'undefined';

// Run the URL selection operation to select an ad based on the experiment group in shared storage
const selectedUrl = await window.sharedStorage.selectURL('ab-testing', urls, {
const selectedUrl = await abTestingWorklet.selectURL('ab-testing', urls, {
data: groups,
resolveToConfig,
keepAlive: true,
Expand All @@ -71,7 +73,7 @@ async function injectAd() {
}

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

injectAd();

0 comments on commit 7a90bb3

Please sign in to comment.