内容制作者通常希望了解其观众的受众特征。您可以使用共享存储空间在您拥有用户特征数据的环境(例如您的第一方网站)中记录用户特征数据,然后使用汇总报告将这些数据纳入其他网站(例如嵌入式内容)的报告中。
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 名年龄在 18-39 周岁且来自欧洲的用户观看了内容 ID 123。”
在此示例中:
demographic-measurement.js
使用帧加载,并负责加载共享存储工作容器。demographic-measurement-worklet.js
是 Shared Storage Worklet,用于读取 Shared Storage 中的受众特征数据,并使用 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); \
互动和分享反馈
请注意,Shared Storage API 提案正在积极讨论和开发中,因此可能会发生变化。
我们非常期待听到您对 Shared Storage API 的看法。
掌握最新动态
- 邮寄名单:订阅我们的邮寄名单,及时了解与 Shared Storage API 相关的最新动态和公告。
需要帮助?
- 开发者支持:在 Privacy Sandbox 开发者支持代码库中与其他开发者联系,并获取问题解答。