运行 A/B 测试

使用共享存储空间 worklet 运行 A/B 测试。

Shared Storage API 是一项 Privacy Sandbox 提案,旨在提供通用的跨网站存储空间,支持许多可能的用例。其中一个示例是 A/B 测试,该测试可在 Chrome 104.0.5086.0 及更高版本中进行。

您可以将用户分配到实验组,然后将该组存储在共享存储空间中,以便在跨网站环境中访问。

试用 A/B 测试

如需通过 Shared Storage 试用 A/B 测试,请确认您使用的是 Chrome 104.0.5086.0 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。

您还可以在命令行中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 标志启用共享存储空间。

试用代码示例

若要了解实验是否具有预期效果,您可以跨多个网站运行 A/B 测试。作为广告客户或内容制作者,您可以选择根据用户所属的群组呈现不同的内容或广告。群组分配信息会保存在共享存储空间中,但无法被窃取。

在此示例中:

  • ab-testing.js 应嵌入到框架中,该框架会映射一个对照组和两个实验内容。脚本会调用实验的共享存储空间 worklet。
  • ab-testing-worklet.js 是共享存储区 worklet,用于返回用户所属的组,从而确定要展示的广告。

ab-testing.js

// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
  return Math.round(Math.random());
}

async function injectContent() {
  // Register the Shared Storage worklet
  await window.sharedStorage.worklet.addModule('ab-testing-worklet.js');

  // Assign user to a random group (0 or 1) and store it in Shared Storage
  window.sharedStorage.set('ab-testing-group', getExperimentGroup(), {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation
  const fencedFrameConfig = await window.sharedStorage.selectURL(
    'ab-testing',
    [
      { url: `https://your-server.example/content/default-content.html` },
      { url: `https://your-server.example/content/experiment-content-a.html` }
    ],
    {
      resolveToConfig: true
    }
  );

  // Render the chosen URL into a fenced frame
  document.getElementById('content-slot').config = fencedFrameConfig;
}

injectContent();

ab-testing-worklet.js

class SelectURLOperation {
  async run(urls, data) {
    // Read the user's experiment group from Shared Storage
    const experimentGroup = await sharedStorage.get('ab-testing-group');

    // Return the corresponding URL (first or second item in the array)
    return urls.indexOf(experimentGroup);
  }
}

register('ab-testing', SelectURLOperation);

使用场景

本部分介绍了 Select 网址 API 的所有可用用例。我们会根据收到的反馈和发现的新测试用例,不断添加示例。

  • 轮替广告素材:存储广告素材 ID 和用户互动等数据,以确定用户在不同网站上看到的广告素材。
  • 按频次选择广告素材:使用观看次数数据确定用户在不同网站上看到的广告素材。
  • 运行 A/B 测试:您可以将用户分配到实验组,然后将该组存储在 Shared Storage 中以供跨网站访问。
  • 为已知客户量身定制体验:根据用户的注册状态或其他用户状态分享自定义内容和号召性用语。

互动并分享反馈

请注意,Select 网址 API 提案正在积极讨论和制作中,因此可能会发生变化。

我们非常期待您就 Select 网址 API 提供反馈。