有时也称为“有效频次”通常须达到最低观看次数要求,用户才能识别或回想特定内容(通常在观看广告的情况下)。您可以使用共享存储空间,针对查看某一条内容至少 K 次的唯一身份用户生成报告。
Shared Storage API 是一种 适用于通用的跨站点存储的沙盒提案,支持多种 可能的使用场景。Private Aggregation API 是共享存储空间中提供的一种输出,可用于汇总跨网站数据。
试用 K+ 频次衡量
若要尝试将 K+ 频次衡量与“共享存储空间”和“私密聚合”搭配使用,请确认您使用的是 Chrome M107 或更高版本。启用 chrome://settings/adPrivacy
下的所有广告隐私权 API。
您还可以在命令行中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames
标志启用共享存储空间。
试用代码示例
您可能希望衡量在指定客户在不同网站上查看过您的内容 K 次或更多次的用户数量。在本例中,展示次数会添加到共享存储空间,在该存储空间中,每当有内容加载时,展示次数都会增加 1。当展示次数达到 3 时,系统会调用 Private Aggregation API。将内容 ID 维度编码为汇总键,并将计数用作可汇总值。摘要报告将提供诸如“大约 391 位用户已至少浏览过广告系列 ID 123 3 次”之类的信息。
在此示例中:
k-frequency-measurement.js
通过帧加载,负责加载共享存储空间 Worklet。k-frequency-measurement-worklet.js
是一个共享存储空间 Worklet,它读取共享存储空间中的展示次数并通过 Private Aggregation API 发送报告。
k-frequency-measurement.js
async function injectContent() {
// Load the Shared Storage worklet
await window.sharedStorage.worklet.addModule('k-freq-measurement-worklet.js');
// Run the K-frequency measurement operation
await window.sharedStorage.run('k-freq-measurement', { data: { kFreq: 3, contentId: 123 });
}
injectContent();
k-frequency-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 content
* ID itself. For more complex bucket key construction, see other use cases in
* this demo.
*/
function convertContentIdToBucket(contentId) {
return BigInt(contentId);
}
class KFreqMeasurementOperation {
async run(data) {
const { kFreq, contentId } = data;
// Read from Shared Storage
const hasReportedContentKey = 'has-reported-content';
const impressionCountKey = 'impression-count';
const hasReportedContent = (await sharedStorage.get(hasReportedContentKey)) === 'true';
const impressionCount = parseInt((await sharedStorage.get(impressionCountKey)) || 0);
// Do not report if a report has been sent already
if (hasReportedContent) {
return;
}
// Check impression count against frequency limit
if (impressionCount < kFreq) {
await sharedStorage.set(impressionCountKey, impressionCount + 1);
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(hasReportedContentKey, 'true');
}
}
// Register the operation
register('k-freq-measurement', KFreqMeasurementOperation); \
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.