按频次选择广告素材

运行共享存储区 worklet 以选择网址并在围栏框架中呈现该网址。

Shared Storage API 是一项 Privacy Sandbox 提案,旨在提供通用的跨网站存储空间,支持许多可能的用例。例如,频次控制功能可在 Chrome Beta 版 104.0.5086.0 及更高版本中进行测试。

运行一个微程序脚本,根据存储的数据从提供的列表中选择一个网址,然后在围栏框架中呈现该网址。此参数可用于在达到频次上限时选择新广告或其他内容。

按频次测试广告素材选择

如需使用共享存储空间和 Fenced Frame 按频次测试广告素材选择,请确认您使用的是 Chrome 104.0.5086.0 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。

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

试用代码示例

如需选择并创建不透明网址,请注册一个用于读取共享存储空间数据的工作程序模块。该 worklet 类会接收最多包含 8 个网址的列表,然后返回所选网址的索引。

当客户端调用 sharedStorage.selectURL() 时,工作程序会执行并返回一个不透明的网址,以渲染到围栏框架中。

假设您想根据用户之前看到某个广告或内容的频次来选择要呈现的其他广告或内容。您可以统计用户观看内容的次数,并将该值存储到共享存储空间中。存储后,共享存储空间中的值可在不同来源之间使用。

然后,共享存储工作程序会读取共享存储中的值,并在每次额外浏览时递增计数器。如果计数未达到预定义限值,则返回您要呈现的内容(索引 1)。否则,返回默认网址(索引 0)。

在此示例中:

  • creative-selection-by-frequencyjs 通过内容制作方或广告客户的 iframe 加载,负责加载共享存储区工作程序,并将返回的不透明来源渲染到受限框架中。
  • creative-selection-by-frequency-worklet.js 是一个共享存储区工作程序,用于读取频次计数,以确定为内容或广告素材返回哪个网址。

creative-selection-by-frequency.js

// The first URL is the default content or ad to be rendered when the frequency limits reached.
const CONTENT_URLS = [
  { url: `https://${contentProducerUrl}/default-content.html` },
  { url: `https://${contentProducerUrl}/example-content.html` },
];

async function injectAd() {
  // Load the worklet module.
  await window.sharedStorage.worklet.addModule('creative-selection-by-frequency-worklet.js');

  // Set the initial frequency count
  window.sharedStorage.set('frequency-count', 0, {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation to choose an ad based on the frequency count in shared storage.
  const fencedFrameConfig = await window.sharedStorage.selectURL('creative-selection-by-frequency', CONTENT_URLS, {
    resolveToConfig: true
  });

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

injectAd();

creative-selection-by-frequency-worklet.js

const FREQUENCY_LIMIT = 5;

class CreativeSelectionByFrequencyOperation {
  async run(urls, data) {
    // Read the current frequency limit in shared storage
    const count = parseInt(await sharedStorage.get('frequency-count'));

    // Check if the frequency limit has been reached.
    if (count === FREQUENCY_LIMIT) {
      console.log('Frequency limit has been reached, and the default content will be rendered.');
      return 0;
    }

    // Set the new frequency count in shared storage
    await sharedStorage.set('frequency-count', count + 1);
    return 1;
  }
}

// Register the operation as 'creative-selection-by-frequency'.
register('creative-selection-by-frequency', CreativeSelectionByFrequencyOperation);

使用场景

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

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

互动并分享反馈

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

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