按频次选择广告素材

运行共享存储空间 Worklet,以选择网址并将其呈现在围栏框架中。

Shared Storage API 是一种 适用于通用的跨站点存储的沙盒提案,支持多种 可能的使用场景。例如,频次控制 在 Chrome 测试版 104.0.5086.0 及更高版本中进行测试。

运行 Worklet 脚本,根据存储的 然后在围栏框架中呈现该网址。它可用于选择 新广告或其他内容的广告。

按频次测试广告素材选择

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

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

试用代码示例

如需选择并创建一个不透明网址,请注册一个 Worklet 模块,以读取共享的 Worklet 模块 存储数据Worklet 类会收到最多包含 8 个网址的列表,然后 返回所选网址的索引。

当客户端调用 sharedStorage.selectURL() 时,worklet 执行并返回要渲染到围栏框架中的不透明网址。

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

然后,共享存储空间 Worklet 会读取共享存储空间中的值,并且每增加一个视图就会递增计数器。如果计数尚未达到预定义的上限,则系统会返回要呈现的内容(索引 1)。否则,系统会返回默认网址(索引 0)。

在此示例中:

  • creative-selection-by-frequencyjs 通过内容制作者或广告客户的 iframe 加载,负责 用于加载共享存储 Worklet,并将返回的 封装到一个围栏帧中。
  • 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 的看法。

掌握最新动态

  • 邮寄名单:订阅我们的邮寄名单,及时了解与 Select 网址 API 和 Shared Storage API 相关的最新动态和公告。

需要帮助?