執行共用儲存空間小程式,選取網址並在設有圍欄的影格中算繪。
Shared Storage API 是 Privacy Sandbox 提案,提供一般用途的跨網站儲存空間,支援多種可能用途。例如,您可以在 Chrome Beta 版 104.0.5086.0 以上版本中測試頻率控制功能。
執行工作小程式指令碼,根據儲存的資料從提供的清單中選取網址,然後在設有圍欄的影格中算繪該網址。達到頻率限制時,系統會使用這項功能選取新廣告或其他內容。
依頻率測試廣告素材選取
如要使用 Shared Storage 和 Fenced Frames 測試依頻率選取素材資源,請確認您使用的是 Chrome 104.0.5086.0 以上版本。啟用 chrome://settings/adPrivacy 下的所有廣告隱私權 API。
您也可以在指令列中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 旗標啟用共用儲存空間。
試用程式碼範例
如要選取及建立不透明網址,請註冊用來讀取共用儲存空間資料的 Worklet 模組。小程式類別會收到最多八個網址的清單,然後傳回所選網址的索引。
用戶端呼叫 sharedStorage.selectURL() 時,工作單元會執行並傳回不透明的網址,以便在封閉架構中算繪。
假設您想根據使用者先前觀看廣告或內容的頻率,選取要顯示的廣告或內容。您可以計算使用者觀看內容的次數,並將該值儲存在共用儲存空間中。儲存後,您就能在不同來源中使用共用儲存空間中的值。
接著,共用儲存空間工作單會讀取共用儲存空間中的值,並在每次額外瀏覽時遞增計數器。如果計數尚未達到預先定義的限制,系統會傳回要算繪的內容 (索引 1)。否則,系統會傳回預設網址 (索引 0)。
在這個例子中:
creative-selection-by-frequencyjs是透過內容製作人或廣告主的 iframe 載入,負責載入共用儲存空間 Worklet,並將傳回的不透明來源轉譯為設有安全防護的影格。creative-selection-by-frequency-worklet.js是共用儲存空間工作單元,會讀取頻率計數,判斷要為內容或廣告素材傳回哪個網址。
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);
用途
請參閱本節,瞭解 Select URL API 的所有可用用途。我們會持續新增示例,以回應使用者意見並探索新的測試案例。
- 輪替廣告素材:儲存廣告素材 ID 和使用者互動等資料,以決定使用者在不同網站上看到的廣告素材。
- 依據頻率選取廣告素材:使用觀看次數資料,決定使用者在不同網站上看到的廣告素材。
- 執行 A/B 版本測試:您可以將使用者指派至實驗群組,然後將該群組儲存在共用儲存空間中,以便跨網站存取。
- 為已知客戶提供客製化體驗:根據使用者的註冊狀態或其他使用者狀態,分享自訂內容和行動號召。
參與討論及分享意見
請注意,「選取網址」API 提案目前仍在積極討論和開發中,隨時可能變更。
我們很期待聽到您對 Select URL API 的想法。