运行 A/B 测试

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

Shared Storage API 是 Privacy Sandbox 提案,用于实现通用的跨网站存储,它支持许多可能的使用场景。例如,A/B 测试就是这样一个例子,可用于在 Chrome 104.0.5086.0 及更高版本中进行测试。

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

尝试进行 A/B 测试

如要尝试使用共享存储空间进行 A/B 测试,请确保您使用的是 Chrome 104.0.5086.0 或更高版本。然后,在 chrome://flags/#privacy-sandbox-ads-apis 处启用 Privacy Sandbox Ads API 实验标志。

将 Privacy Sandbox 广告 API 实验设为“已启用”,以便使用这些 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 this.sharedStorage.get('ab-testing-group');

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

register('ab-testing', SelectURLOperation);

Use cases

These are only some of the possible use cases for Shared Storage. We'll continue to add examples as we receive feedback and discover new use cases.

Content selection

Select and display different content on different websites in fenced frames based on information collected in Shared Storage. The output gate for these use cases is URL selection.

  • Creative rotation: Store data, such as creative ID, view counts, and user interaction, to determine which creative users' see across different sites.
  • A/B testing: You can assign a user to an experiment group, then store that group in Shared Storage to be accessed cross-site.
  • Custom user experiences: Share custom content and calls-to-action based on a user's registration status or other user states

Generate summary reports

Collect information with Shared Storage and generated a noisy, aggregated summary report. The output gate for these use cases is the Private Aggregation API.

  • Unique reach measurement: Many content producers and advertisers want to know how many unique people saw their content. Use Shared Storage to record the first time a user saw your ad, embedded video, or publication, and prevent duplicative counting of that same user on different sites. You can then use the Private Aggregation API to output a summary report for your reach.
  • Demographics measurement: Content producers often want to understand the demographics of their audience. You can use Shared Storage to record user demographic data in a context where you have it, such as your first-party site, and use aggregated reporting to report on it across many other sites, such as embedded content.
  • K+ frequency measurement: Sometimes described as "effective frequency," there is often a minimum number views before a user will recognize or recall certain content (often in the context of advertisement views). You can use Shared Storage to build reports of unique users that have seen a piece of content at least K number of times.

互动和分享反馈

共享存储空间提案正在积极讨论中,将来可能会发生变化。如果您在试用此 API 时有反馈意见,我们非常期待收到您的宝贵意见。