按频次选择广告素材

运行共享存储区 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);

Use cases

All available use cases for Select URL API can be found in this section. We'll continue to add examples as we receive feedback and discover new test cases.

互动并分享反馈

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

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