Run a Shared Storage worklet to select a URL and render it in a fenced frame.
The Shared Storage API is a Privacy Sandbox proposal for general purpose, cross-site storage, which supports many possible use cases. One example is frequency control, which is available to test in Chrome Beta 104.0.5086.0 and later.
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 can be used to select new ads or other content when the frequency limit has been reached.
Test creative selection by frequency
To test creative selection by frequency with Shared Storage and Fenced Frames, confirm you're
using Chrome 104.0.5086.0 or later. Enable all the Ad privacy APIs under chrome://settings/adPrivacy
.
You can also enable Shared Storage with the --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames
flag in the command line.
Experiment with code samples
To select and create an opaque URL, register a worklet module to read shared storage data. The worklet class receives a list of up to eight URLs and then returns the index of the chosen URL.
When the client calls sharedStorage.selectURL()
, the worklet
executes and returns an opaque URL to be rendered into a fenced frame.
Let's say you want to select a different ad or content to render based on the frequency of how many times a user has seen it before. You can count how many times a user has seen a content, and store that value into shared storage. Once stored, the value in shared storage becomes available for you across different origins.
Then, the shared storage worklet reads the values in shared storage, and increments the counter with each additional view. If the count has not reached the predefined limit, the content you want to render is returned (index 1
). If not, the default URL is returned (index 0
).
In this example:
creative-selection-by-frequencyjs
is loaded via the content producer's or advertiser's iframe, and is responsible for loading the shared storage worklet, and rendering the returned opaque source into a fenced frame.creative-selection-by-frequency-worklet.js
is the shared storage worklet that reads the frequency count to determine which URL is returned for a content or an ad creative.
creative-selection-by-frequency.js
// The first URL is the default content or ad to be rendered when the frequency limits reached.
const CONTENT_URLS = [
{ url: `https://${contentProducerUrl}/default-content.html` },
{ url: `https://${contentProducerUrl}/example-content.html` },
];
async function injectAd() {
// Load the worklet module.
await window.sharedStorage.worklet.addModule('creative-selection-by-frequency-worklet.js');
// Set the initial frequency count
window.sharedStorage.set('frequency-count', 0, {
ignoreIfPresent: true,
});
// Run the URL selection operation to choose an ad based on the frequency count in shared storage.
const fencedFrameConfig = await window.sharedStorage.selectURL('creative-selection-by-frequency', CONTENT_URLS, {
resolveToConfig: true
});
// Render the opaque URL into a fenced frame
document.getElementById('content-slot').config = fencedFrameConfig;
}
injectAd();
creative-selection-by-frequency-worklet.js
const FREQUENCY_LIMIT = 5;
class CreativeSelectionByFrequencyOperation {
async run(urls, data) {
// Read the current frequency limit in shared storage
const count = parseInt(await sharedStorage.get('frequency-count'));
// Check if the frequency limit has been reached.
if (count === FREQUENCY_LIMIT) {
console.log('Frequency limit has been reached, and the default content will be rendered.');
return 0;
}
// Set the new frequency count in shared storage
await sharedStorage.set('frequency-count', count + 1);
return 1;
}
}
// Register the operation as 'creative-selection-by-frequency'.
register('creative-selection-by-frequency', CreativeSelectionByFrequencyOperation);
Use cases
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.
Content selection
Select and display different content on different websites in fenced frames based on information collected in Shared Storage. The output gate for these use cases is URL selection.
- Creative rotation: Store data, such as creative ID, view counts, and user interaction, to determine which creative users' see across different sites.
- A/B testing: You can assign a user to an experiment group, then store that group in Shared Storage to be accessed cross-site.
- Custom user experiences: Share custom content and calls-to-action based on a user's registration status or other user states
Generate summary reports
Collect information with Shared Storage and generated a noisy, aggregated summary report. The output gate for these use cases is the Private Aggregation API.
- Unique reach measurement: Many content producers and advertisers want to know how many unique people saw their content. Use Shared Storage to record the first time a user saw your ad, embedded video, or publication, and prevent duplicative counting of that same user on different sites. You can then use the Private Aggregation API to output a summary report for your 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.
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.