许多内容制作者和广告客户都想知道有多少唯一身份用户观看了他们的内容。使用共享存储空间记录用户首次看到您的广告、嵌入的视频或发布内容,并防止在不同网站上重复统计同一用户。然后,您可以使用 Private Aggregation API 输出覆盖面的摘要报告。
Shared Storage API 是一种 适用于通用的跨站点存储的沙盒提案,支持多种 可能的使用场景。Private Aggregation API 是共享存储空间中提供的一种输出,可让您汇总跨网站数据。 如需详细了解实现这些衡量方法的方法,请参阅我们的覆盖面白皮书。
试用唯一身份用户覆盖面衡量功能
要尝试将唯一身份用户覆盖面衡量与共享存储空间和私密汇总功能结合使用,请确认您使用的是 Chrome M107 或更高版本。启用 chrome://settings/adPrivacy
下的所有广告隐私权 API。
您还可以在命令行中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames
标志启用共享存储空间。
试用代码示例
您可能希望跟踪在不同网站上有多少唯一身份用户看过您的内容。在此示例中,内容 ID 维度编码到汇总键(分桶)中,计数用作可汇总值。摘要报告将包含“大约 391 位用户看过 Content ID 123”之类的信息。
在此示例中:
* unique-reach-measurement.js
通过帧加载,负责加载共享存储空间 Worklet。
* unique-reach-measurement-worklet.js
是共享存储空间 Worklet,它检查共享存储空间中的标志并通过 Private Aggregation API 发送报告。
async function measureUniqueReach() {
// Load the Shared Storage worklet
await window.sharedStorage.worklet.addModule('reach-measurement-worklet.js');
// Run the reach measurement operation
await window.sharedStorage.run('reach-measurement', { data: { contentId: '1234' } });
}
measureUniqueReach();
// Learn more about noise and scaling from the Private Aggregation fundamentals
// documentation on Chrome blog
const SCALE_FACTOR = 65536;
function convertContentIdToBucket(contentId) {
return BigInt(contentId);
}
class ReachMeasurementOperation {
async run(data) {
const { contentId } = data;
// Read from Shared Storage
const key = 'has-reported-content';
const hasReportedContent = (await sharedStorage.get(key)) === 'true';
// Do not report if a report has been sent already
if (hasReportedContent) {
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.contributeToHistogram({ bucket, value });
// Set the report submission status flag
await sharedStorage.set(key, true);
}
}
// Register the operation
register('reach-measurement', ReachMeasurementOperation);
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.
Stay Informed
- Mailing List: Subscribe to our mailing list for the latest updates and announcements related to the Shared Storage API.
Need Help?
- Developer Support: Connect with other developers and get answers to your questions in the Privacy Sandbox Developer Support repository.