内容制作者通常希望了解其受众群体的受众特征。您可以使用共享存储空间在您拥有用户人口统计数据的情境(例如您的第一方网站)中记录这些数据,然后使用汇总报告将这些数据纳入其他网站(例如您的嵌入式内容)的报告中。
Shared Storage API 是一项 Privacy Sandbox 提案,旨在提供通用的跨网站存储空间,支持许多可能的用例。Private Aggregation API 是 Shared Storage 中提供的一种输出,可用于汇总跨网站数据。
试用用户人口统计信息衡量功能
如需通过共享存储空间和私密聚合功能来试用用户人口统计信息衡量功能,请确认您使用的是 Chrome Canary 和 Dev M107 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。
您还可以在命令行中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 标志启用共享存储空间。
试用代码示例
您可能希望衡量在不同网站上看到您内容的用户的一些人口统计特征,例如年龄段或地理位置。在此示例中,内容 ID、年龄段 ID 和地理位置 ID 维度会编码到汇总键(桶)中,而计数会用作可汇总的值。生成的摘要报告将提供以下信息:“大约有 391 名看过内容 ID 为 123 的用户年龄在 18-39 岁之间,并且来自欧洲。”
在此示例中:
demographic-measurement.js使用框架加载,负责加载共享存储区 applet。demographic-measurement-worklet.js是一个共享存储工作程序,用于读取共享存储中的人口统计数据,并使用 Private Aggregation API 发送报告。
(在进行衡量之前运行,用于将人口统计数据设置到 Shared Storage 中)
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();
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 just 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');
// Don't 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 using 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.
- Proposal: Review the detailed proposal.
- Discussion: Join the ongoing discussion to ask questions and share your insights.