K+ frequency measurement
Use Shared Storage and Private Aggregation for K+ frequency measurement
The Shared Storage API is a Privacy Sandbox proposal for general purpose, cross-site storage, which supports many possible use cases. Private Aggregation API is an output available in Shared Storage that allows you to aggregate cross-site data.
Try K+ frequency measurement
To experiment with K+ frequency measurement with Shared Storage and Private Aggregation, confirm you're using Chrome M107 or later. Then enable the Privacy Sandbox Ads APIs experiment flag at chrome://flags/#privacy-sandbox-ads-apis
.
You can also enable Shared Storage with the --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames
flag in the command line.
Experiment with code samples
The following code samples were created to demonstrate how the API may be used for the given use cases. These are not meant to be used in production.
You may want to measure the number of users who have seen your content K or more times a given client across different sites. In this example, the impression count is added to shared storage where it increments by 1 whenever the content is loaded. When the impression count has reached 3, the Private Aggregation API is called. The content ID dimension is encoded as the aggregation key, and the count is used as the aggregatable value. The summary report will provide information such as "Approximately 391 users have seen the ad campaign ID 123 at least 3 times."
In this example:
k-frequency-measurement.js
is loaded via a frame, and is responsible for loading the shared storage worklet.k-frequency-measurement-worklet.js
is the shared storage worklet that reads the impression count in shared storage and sends a report via the 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 this.sharedStorage.get(hasReportedContentKey)) === 'true';
const impressionCount = parseInt((await this.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 this.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.sendHistogramReport({ bucket, value });
// Set the report submission status flag
await this.sharedStorage.set(hasReportedContentKey, 'true');
}
}
// Register the operation
register('k-freq-measurement', KFreqMeasurementOperation); \
Other use cases
Explore other Shared Storage use cases and code samples:
Generate reports with Private Aggregation
- Unique reach measurement: Many content producers and advertisers often want to know how many unique people saw their content. You can use Shared Storage to report on the first time a user saw your ad, embedded video, publication, and prevent duplicative counting of that same user on a different site, giving you an aggregated noisy report of your approximate unique reach.
- Demographics measurement: Content producers often want to understand the demographics of their audience. You can use Shared Storage to record user demographic data in a context where you have it, such as your first-party site, and use aggregated reporting to report on it across many other sites, such as embedded content.
- K+ frequency measurement: Sometimes described as "effective frequency," there is often a minimum number views before a user will recognize or recall certain content (often in the context of advertisement views). You can use Shared Storage to build reports of unique users that have seen a piece of content at least K number of times.
URL selection
- Frequency control: run a worklet script to select a URL from a provided list, based on the stored data, and then render that URL in a fenced frame. This has many possible uses, such as selecting new content when a frequency cap is reached.
- A/B testing: You can assign a user to an experiment group, then store that group in Shared Storage to be accessed cross-site.
- Creative rotation: You can store the creative rotation mode, and other metadata, to rotate the creatives across different sites.
- Known customer for payment provider: You can store whether the user has registered on your site into shared storage, then render a different element based on that stored status.
These are only some of the possible use cases for Shared Storage. We'll continue to add examples as we receive feedback and discover new use cases.
Engage and share feedback
The Shared Storage proposal is under active discussion and subject to change in the future. If you try this API and have feedback, we'd love to hear it.
- GitHub: Read the proposal, raise questions and participate in discussion.
- Shared Storage API announcements: Join or view past announcements on our mailing list
- Developer support: Ask questions and join discussions on the Privacy Sandbox Developer Support repo.