衡量用户受众特征

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

Shared Storage API 是一种 适用于通用的跨站点存储的沙盒提案,支持多种 可能的使用场景。Private Aggregation API 是共享存储空间中提供的一种输出,可用于汇总跨网站数据。

试用用户受众特征衡量

若要尝试将“共享存储空间”和“私密汇总”功能用于用户受众特征衡量,请确认您使用的是 Chrome Canary 版和 Dev M107 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。

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

试用代码示例

您可能希望衡量在不同网站上观看过您的内容的用户的特定受众特征,例如年龄段或地理位置。在此示例中,内容 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 sharedStorage.get(key)) === 'true';
    const ageGroup = await sharedStorage.get('age-group');
    const continent = await 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.contributeToHistogram({ bucket, value });

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

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

Engage and share feedback

Note that the Shared Storage API proposal is under active discussion and development and therefore subject to change.

We're eager to hear your thoughts on the Shared Storage API.

Stay Informed

  • Mailing List: Subscribe to our mailing list for the latest updates and announcements related to the Shared Storage API.

Need Help?