Chạy một Worklet Bộ nhớ dùng chung để chọn một URL và kết xuất URL đó trong một khung bảo vệ.
API Bộ nhớ dùng chung là một Quyền riêng tư Đề xuất hộp cát cho mục đích chung, lưu trữ trên nhiều trang web, hỗ trợ nhiều các trường hợp sử dụng có thể xảy ra. Một ví dụ là kiểm soát tần suất, có sẵn cho thử nghiệm trong Chrome Beta 104.0.5086.0 trở lên.
Chạy tập lệnh worklet để chọn một URL trong danh sách được cung cấp, dựa trên dữ liệu đã lưu trữ rồi hiển thị URL đó trong một khung bảo vệ. Bạn có thể dùng công cụ này để chọn quảng cáo mới hoặc nội dung khác khi đã đạt đến giới hạn tần suất.
Thử nghiệm lựa chọn mẫu quảng cáo theo tần suất
Để kiểm tra lựa chọn mẫu quảng cáo theo tần suất với Bộ nhớ dùng chung và Khung bảo vệ, hãy xác nhận rằng bạn
sử dụng Chrome 104.0.5086.0 trở lên. Bật mọi API quyền riêng tư trong quảng cáo trong chrome://settings/adPrivacy.
Bạn cũng có thể bật Bộ nhớ dùng chung bằng cờ --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames trong dòng lệnh.
Thử nghiệm mã mẫu
Để chọn và tạo một URL mờ, hãy đăng ký mô-đun worklet để đọc nội dung được chia sẻ dữ liệu lưu trữ. Lớp worklet nhận danh sách tối đa 8 URL, sau đó sẽ trả về chỉ mục của URL đã chọn.
Khi ứng dụng gọi sharedStorage.selectURL(), worklet
thực thi và trả về một URL mờ để kết xuất vào khung bảo vệ.
Giả sử bạn muốn chọn một quảng cáo hoặc nội dung khác để hiển thị dựa trên tần suất người dùng đã nhìn thấy quảng cáo hoặc nội dung đó trước đó. Bạn có thể đếm số lần một người dùng đã xem một nội dung và lưu trữ giá trị đó vào bộ nhớ dùng chung. Sau khi được lưu trữ, bạn có thể sử dụng giá trị trong bộ nhớ dùng chung trên nhiều nguồn gốc.
Sau đó, worklet bộ nhớ dùng chung sẽ đọc các giá trị trong bộ nhớ dùng chung và tăng bộ đếm theo mỗi chế độ xem bổ sung. Nếu số lượng chưa đạt đến giới hạn xác định trước, nội dung bạn muốn kết xuất sẽ được trả về (chỉ mục 1). Nếu không, URL mặc định sẽ được trả về (chỉ mục 0).
Trong ví dụ này:
creative-selection-by-frequencyjsđược tải thông qua iframe của nhà sản xuất nội dung hoặc nhà quảng cáo, đồng thời chịu trách nhiệm để tải worklet bộ nhớ dùng chung và kết xuất hình ảnh mờ được trả về nguồn vào một khung bảo vệ.creative-selection-by-frequency-worklet.jslà worklet bộ nhớ dùng chung có chức năng đọc để xác định URL nào được trả về cho một nội dung hoặc một quảng cáo.
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
All available use cases for Select URL API can be found in this section. We'll continue to add examples as we receive feedback and discover new test cases.
- Rotate ad creatives: Store data, such as creative ID and user interaction, to determine which creative users' see across different sites.
- Select ad creatives by frequency: Use view count data to determine which creative users' see across different sites.
- Run A/B testing: You can assign a user to an experiment group, then store that group in Shared Storage to be accessed cross-site.
- Customize experience for known customers: Share custom content and calls-to-action based on a user's registration status or other user states.
Engage and share feedback
Note that the Select URL API proposal is under active discussion and development and subject to change.
We're eager to hear your thoughts on the Select URL API.
- Proposal: Review the detailed proposal.
- Discussion: Join the ongoing discussion to ask questions and share your insights.