Select a creative by frequency

Run a Shared Storage worklet to select a URL and render it in a fenced frame.

The Shared Storage API is a Privacy Sandbox proposal for general purpose, cross-site storage, which supports many possible use cases. One example is frequency control, which is available to test in Chrome Beta 104.0.5086.0 and later.

Run a worklet script to select a URL from a provided list, based on the stored data, and then render that URL in a fenced frame. This can be used to select new ads or other content when the frequency limit has been reached.

Test creative selection by frequency

To test creative selection by frequency with Shared Storage and Fenced Frames, confirm you're using Chrome 104.0.5086.0 or later. Enable all the Ad privacy APIs under chrome://settings/adPrivacy.

You can also enable Shared Storage with the --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames flag in the command line.

Experiment with code samples

To select and create an opaque URL, register a worklet module to read shared storage data. The worklet class receives a list of up to eight URLs and then returns the index of the chosen URL.

When the client calls sharedStorage.selectURL(), the worklet executes and returns an opaque URL to be rendered into a fenced frame.

Let's say you want to select a different ad or content to render based on the frequency of how many times a user has seen it before. You can count how many times a user has seen a content, and store that value into shared storage. Once stored, the value in shared storage becomes available for you across different origins.

Then, the shared storage worklet reads the values in shared storage, and increments the counter with each additional view. If the count has not reached the predefined limit, the content you want to render is returned (index 1). If not, the default URL is returned (index 0).

In this example:

  • creative-selection-by-frequencyjs is loaded via the content producer's or advertiser's iframe, and is responsible for loading the shared storage worklet, and rendering the returned opaque source into a fenced frame.
  • creative-selection-by-frequency-worklet.js is the shared storage worklet that reads the frequency count to determine which URL is returned for a content or an ad creative.

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);

使用场景

以上只是共享存储空间的一部分用例。我们将 我们会继续添加样本, 获取反馈 并发现新的应用场景

内容选择

在以下位置选择和显示不同网站上的不同内容: 围栏框架 共享存储空间这些用例的输出门控是网址选择。

  • 广告素材轮播: 存储广告素材 ID、观看次数和用户互动等数据,以确定哪个广告素材在不同网站上的呈现效果
  • A/B 测试: 您可以将用户分配到实验组,然后将该组存储在共享存储空间中,以便跨网站访问。
  • 自定义用户体验: 根据用户的注册状态或其他用户状态共享自定义内容和号召性用语

生成摘要报告

通过共享存储空间收集信息,并生成杂乱的汇总摘要报告。这些用例的输出门控是 Private Aggregation API

  • 唯一身份用户覆盖面衡量: 许多内容制作者和广告客户都想知道 人查看了他们的内容。使用共享存储空间记录用户首次访问 用户看过您的广告、嵌入式视频或发布内容,并防止重复 统计不同网站上的同一用户然后,您可以使用 Private Aggregation API 输出覆盖面的摘要报告。
  • 受众特征衡量: 内容制作者通常希望了解其网站的受众特征 受众群体。您可以使用共享存储空间来记录 背景信息(例如您的第一方网站),并使用汇总的数据 以便在其他许多网站(例如嵌入式内容)中生成此报告。
  • K+ 频次衡量: 有时也称为“有效频次”通常会有一个 用户会认出或回想某些内容(通常在 广告观看情境)。您可以使用共享存储空间生成报告 的唯一身份用户中,特定内容至少浏览了 K 次。

Engage and share feedback

The Shared Storage proposal is under active discussion and subject to change in the future. If you try this API and have feedback, we'd love to hear it.