衡量 K+ 频次覆盖面

有时也称为“有效频次”,是指通常在达到一定次数后用户才能识别或回想特定内容(通常在广告观看的情境中)。您可以使用共享存储空间,针对查看某一条内容至少 K 次的唯一身份用户生成报告。

Shared Storage API 是一个适用于通用跨站存储的 Privacy Sandbox 提案,支持许多可能的用例。Private Aggregation API 是共享存储空间中提供的一种输出,可用于汇总跨网站数据。

试用 K+ 频次衡量

若要尝试将 K+ 频次衡量与“共享存储空间”和“私密聚合”搭配使用,请确认您使用的是 Chrome M107 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。

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

试用代码示例

您可能希望衡量在指定客户在不同网站上查看过您的内容 K 次或更多次的用户数量。在本例中,展示次数会添加到共享存储空间,在该存储空间中,每当有内容加载时,展示次数都会增加 1。当展示次数达到 3 时,系统会调用 Private Aggregation API。将内容 ID 维度编码为汇总键,并将计数用作可汇总值。摘要报告将提供诸如“大约 391 位用户已至少浏览过广告系列 ID 123 3 次”之类的信息。

在此示例中:

k-frequency-measurement.js

async function injectContent() {
  // Load the Shared Storage worklet
  await window.sharedStorage.worklet.addModule('k-freq-measurement-worklet.js');

  // Run the K-frequency measurement operation
  await window.sharedStorage.run('k-freq-measurement', { data: { kFreq: 3, contentId: 123 });
}

injectContent();

k-frequency-measurement-worklet.js

// Learn more about noise and scaling from the Private Aggregation fundamentals
// documentation on Chrome blog
const SCALE_FACTOR = 65536;

/**
 * The bucket key must be a number, and in this case, it is simply the content
 * ID itself. For more complex bucket key construction, see other use cases in
 * this demo.
 */
function convertContentIdToBucket(contentId) {
  return BigInt(contentId);
}

class KFreqMeasurementOperation {
  async run(data) {
    const { kFreq, contentId } = data;

    // Read from Shared Storage
    const hasReportedContentKey = 'has-reported-content';
    const impressionCountKey = 'impression-count';
    const hasReportedContent = (await this.sharedStorage.get(hasReportedContentKey)) === 'true';
    const impressionCount = parseInt((await this.sharedStorage.get(impressionCountKey)) || 0);

    // Do not report if a report has been sent already
    if (hasReportedContent) {
      return;
    }

    // Check impression count against frequency limit
    if (impressionCount < kFreq) {
      await this.sharedStorage.set(impressionCountKey, impressionCount + 1);
      return;
    }

    // Generate the aggregation key and the aggregatable value
    const bucket = convertContentIdToBucket(contentId);
    const value = 1 * SCALE_FACTOR;

    // Send an aggregatable report via the Private Aggregation API
    privateAggregation.sendHistogramReport({ bucket, value });

    // Set the report submission status flag
    await this.sharedStorage.set(hasReportedContentKey, 'true');
  }
}

// Register the operation

register('k-freq-measurement', KFreqMeasurementOperation); \

互动和分享反馈

共享存储空间提案正在积极讨论中,将来可能会发生变化。如果您在试用此 API 时有反馈意见,我们非常期待收到您的宝贵意见。