內容製作者通常想瞭解目標對象的人口統計資料。您可以在擁有使用者人口統計資料的情境 (例如第一方網站) 中,使用共用儲存空間記錄這些資料,然後透過匯總報表,將這些資料納入其他網站 (例如嵌入內容) 的報表。
Shared Storage API 是 Privacy Sandbox 提案,提供一般用途的跨網站儲存空間,支援多種可能用途。Private Aggregation API 是 Shared Storage 中提供的輸出內容,可讓您匯總跨網站資料。
試用使用者客層評估功能
如要使用共用儲存空間和私密匯總功能實驗性地評估使用者人口統計資料,請確認您使用的是 Chrome Canary 和開發人員版 M107 以上版本。啟用 chrome://settings/adPrivacy 下的所有廣告隱私權 API。
您也可以在指令列中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 旗標啟用共用儲存空間。
試用程式碼範例
您可能想評估在不同網站上看到您內容的使用者客層,例如年齡層或地理位置。在本範例中,內容 ID、年齡層 ID 和地理位置 ID 維度會編碼為匯總鍵 (buckets),而計數則做為可匯總的值。系統產生的摘要報表會提供相關資訊,例如「約有 391 位看過內容 ID 123 的使用者年齡介於 18 至 39 歲,且來自歐洲」。
在這個例子中:
demographic-measurement.js是使用影格載入,負責載入共用儲存空間工作單元。demographic-measurement-worklet.js是共用儲存空間的 Worklet,可讀取共用儲存空間中的目標對象統計資料,並使用 Private Aggregation API 傳送報表。
(在進行評估前執行,將目標對象統計資料設定到 Shared Storage 中)
function getDemogrationsData() {
// Collect age group and continent data
return {
ageGroup,
continent
}
}
async function storeDemographics() {
const { ageGroup, continent } = getDemographicsData();
await window.sharedStorage.set('age-group', ageGroup);
await window.sharedStorage.set('continent', continent);
}
storeDemographics();
async function measureDemographics() {
// Load the Shared Storage worklet
await window.sharedStorage.worklet.addModule('demographics-measurement-worklet.js');
// Run the demographics measurement operation
await window.sharedStorage.run('demographics-measurement', { data: { contentId: '123' } });
}
measureDemographics();
demographic-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 just the ad campaign
* ID itself. For more complex bucket key construction, see other use cases in
* this demo.
*/
const AGGREGATION_KEY_MAP = {
ageGroupId: {
'18-39': '1',
'40-64': '2',
'65+': '3',
},
continentId: {
africa: '1',
antarctica: '2',
asia: '3',
australia: '4',
europe: '5',
'north-america': '6',
'south-america': '7',
},
};
/**
* The aggregation key will be in the format of:
* contentId | ageGroupId | continentId
*
* For example, a user from Australia between the age of 40-64, who has
* seen the Content ID 321 will be represented by the key:
* 321 | 2 | 4 or 32124
*/
function generateAggregationKey(contentId, ageGroup, continent) {
const ageGroupId = AGGREGATION_KEY_MAP.ageGroupId[ageGroup];
const continentId = AGGREGATION_KEY_MAP.continentId[continent];
const aggregationKey = BigInt(`${contentId}${ageGroupId}${continentId}`);
return aggregationKey;
}
class DemographicsMeasurementOperation {
async run(data) {
const { contentId } = data;
// Read from Shared Storage
const key = 'has-reported-content';
const hasReportedContent = (await sharedStorage.get(key)) === 'true';
const ageGroup = await sharedStorage.get('age-group');
const continent = await sharedStorage.get('continent');
// Don't report if a report has been sent already
if (hasReportedContent) {
return;
}
// Generate the aggregation key and the aggregatable value
const bucket = generateAggregationKey(contentId, ageGroup, continent);
const value = 1 * SCALE_FACTOR;
// Send an aggregatable report using the Private Aggregation API
privateAggregation.contributeToHistogram({ bucket, value });
// Set the report submission status flag
await sharedStorage.set(key, true);
}
}
// Register the operation
register('demographics-measurement', DemographicsMeasurementOperation); \
參與討論及分享意見
請注意,Shared Storage API 提案目前仍在積極討論和開發中,因此可能會有所變更。
我們很期待聽到您對 Shared Storage API 的想法。