評估使用者客層

內容製作者往往會希望瞭解自己的觀眾客層。使用「共用儲存空間」時,您可以記錄自己擁有的客層資料 (例如第一方網站),然後使用匯總報表,將這些資料納入其他網站 (例如您嵌入的內容) 的報表中。

Shared Storage API 是適用於一般用途的跨網站儲存空間 Privacy Sandbox 提案,可支援許多可能的用途。Private Aggregation API 是共用儲存空間中的輸出項目,可讓您匯總跨網站資料。

試用使用者客層評估

如要以共用儲存空間和私人匯總功能進行使用者客層評估,請確認使用的是 Chrome Canary 和開發人員 M107 以上版本。啟用 chrome://settings/adPrivacy 下的所有廣告隱私權 API。

您也可以在指令列中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 旗標啟用共用儲存空間。

使用程式碼範例進行實驗

你可能會想評估在不同網站上看過你的內容的使用者客層,例如年齡層或地理位置。在這個範例中,內容 ID、年齡層 ID 和地理位置 ID 維度會編碼為匯總鍵 (值區),而計數會做為可匯總值使用。產生的摘要報告會提供下列資訊:「看過 Content ID 123 的使用者約 391 位介於 18 至 39 歲與歐洲使用者之間」。

在這個例子中:

  • demographic-measurement.js 是透過框架載入,並負責載入共用儲存空間活程式。
  • demographic-measurement-worklet.js 是共用儲存空間工作程式,可讀取共用儲存空間中的客層資料,並透過 Private Aggregation API 傳送報表。

store-demographic-data.js

(在測量的時間點執行,以便將客層資料設為共用儲存空間)

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();

demographic-measurement.js

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 simply 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 this.sharedStorage.get(key)) === 'true';
    const ageGroup = await this.sharedStorage.get('age-group');
    const continent = await this.sharedStorage.get('continent');

    // Do not 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 via the Private Aggregation API
    privateAggregation.sendHistogramReport({ bucket, value });

    // Set the report submission status flag
    await this.sharedStorage.set(key, true);
  }
}

// Register the operation
register('demographics-measurement', DemographicsMeasurementOperation); \

交流及分享意見回饋

共用儲存空間提案仍在進行中的討論,日後可能會有變動。如果您試用這個 API,並有任何意見,歡迎與我們分享。