衡量用户受众特征

内容制作者通常希望了解观众的受众特征。您可以使用共享存储空间在您拥有的场所(例如您的第一方网站)中记录用户的受众特征数据,然后使用汇总报告将这些数据(例如您的嵌入式内容)纳入来自其他网站的报告。

Shared Storage API 是 Privacy Sandbox 提案,用于实现通用的跨网站存储,它支持许多可能的使用场景。Private Aggregation API 是共享存储空间中提供的一种输出,可让您汇总跨网站数据。

尝试用户受众特征衡量

若要尝试使用共享存储空间和不公开汇总功能来衡量用户的受众特征,请确认您使用的是 Chrome Canary 版和 M107 版或更高版本。然后,在 chrome://flags/#privacy-sandbox-ads-apis 处启用 Privacy Sandbox Ads API 实验标志。

将 Privacy Sandbox 广告 API 实验设为启用,以便使用这些 API。

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

使用代码示例进行实验

您可能希望衡量在不同网站上观看过您的内容的用户的特定受众特征,例如年龄段或地理位置。在此示例中,Content ID、年龄段 ID 和地理位置 ID 维度已编码到汇总键(存储分区)中,并且计数将用作可汇总的值。生成的摘要报告将提供以下信息:“大约有 391 位看到过 Content ID 123 的用户年龄在 18-39 周岁之间,并且来自欧洲”。

在此示例中:

  • demographic-measurement.js 通过帧加载,负责加载共享存储空间 Worklet。
  • demographic-measurement-worklet.js 是一个共享存储空间 Worklet,它读取共享存储空间中的受众特征数据,并通过 Private Aggregation API 发送报告。

store-demographic-data.js

(在测量之前的某个时间点运行,以便将受众特征数据设置为共享存储空间)

function getDemogrationsData() {
  // Collect age group and continent data
  return {
    ageGroup,
    continent
  }
}

async function storeDemographics() {
  const { ageGroup, continent } = getDemographicsData();
  await window.sharedStorage.set('age-group', ageGroup);
  await window.sharedStorage.set('continent', continent);
}

storeDemographics();

demographic-measurement.js

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

  // Run the demographics measurement operation
  await window.sharedStorage.run('demographics-measurement', { data: { contentId: '123' } });
}

measureDemographics();

demographic-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 ad campaign
 * ID itself. For more complex bucket key construction, see other use cases in
 * this demo.
 */

const AGGREGATION_KEY_MAP = {
  ageGroupId: {
    '18-39': '1',
    '40-64': '2',
    '65+': '3',
  },

  continentId: {
    africa: '1',
    antarctica: '2',
    asia: '3',
    australia: '4',
    europe: '5',
    'north-america': '6',
    'south-america': '7',
  },

};

/**
 * The aggregation key will be in the format of:
 * contentId | ageGroupId | continentId
 *
 * For example, a user from Australia between the age of 40-64, who has
 * seen the Content ID 321 will be represented by the key:
 * 321 | 2 | 4 or 32124
 */

function generateAggregationKey(contentId, ageGroup, continent) {
  const ageGroupId = AGGREGATION_KEY_MAP.ageGroupId[ageGroup];
  const continentId = AGGREGATION_KEY_MAP.continentId[continent];
  const aggregationKey = BigInt(`${contentId}${ageGroupId}${continentId}`);

  return aggregationKey;
}

class DemographicsMeasurementOperation {
  async run(data) {
    const { contentId } = data;

    // Read from Shared Storage
    const key = 'has-reported-content';
    const hasReportedContent = (await this.sharedStorage.get(key)) === 'true';
    const ageGroup = await this.sharedStorage.get('age-group');
    const continent = await this.sharedStorage.get('continent');

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

    // Generate the aggregation key and the aggregatable value
    const bucket = generateAggregationKey(contentId, ageGroup, continent);
    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(key, true);
  }
}

// Register the operation
register('demographics-measurement', DemographicsMeasurementOperation); \

互动和分享反馈

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