按频次选择广告素材

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

Shared Storage API 是一个适用于通用跨站存储的 Privacy Sandbox 提案,支持许多可能的用例。例如频率控制,可在 Chrome Beta 版 104.0.5086.0 及更高版本中测试。

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

按频次测试广告素材选择

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

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

试用代码示例

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

当客户端调用 sharedStorage.selectURL() 时,worklet 会执行并返回不透明网址,以呈现到围栏框架中。

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

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

在此示例中:

  • creative-selection-by-frequencyjs 通过内容制作方或广告客户的 iframe 加载,负责加载共享存储 Worklet,并将返回的不透明来源呈现到围栏帧中。
  • creative-selection-by-frequency-worklet.js 是一个共享存储空间 Worklet,它读取频次计数以确定为内容或广告素材返回哪个网址。

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 this.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 this.sharedStorage.set('frequency-count', count + 1);
    return 1;
  }
}

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

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 并有反馈意见,我们非常期待收到您的反馈意见。