評估使用者客層

內容製作者經常會想瞭解觀眾客層,您可以使用「共用儲存空間」在特定情境 (例如第一方網站) 中記錄使用者客層資料,然後使用匯總報表將這些資料納入其他網站 (例如您的嵌入內容) 的報表中。

Shared Storage API 是 Privacy Sandbox 提案之一,適用於一般用途的跨網站儲存空間。這個 API 支援許多可能的用途。Private Aggregation API 是「共用儲存空間」提供的輸出內容,可讓您匯總跨網站資料。

試試使用者客層評估功能

如要透過「共用儲存空間」和「私人匯總」功能測試使用者客層評估數據,請確認您使用的是 Chrome Canary 和 Dev M107 以上版本。接著,在 chrome://flags/#privacy-sandbox-ads-apis 中啟用 Privacy Sandbox Ads API 實驗旗標。

如要使用這些 API,請將 Privacy Sandbox Ads API 實驗設為啟用。

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

使用程式碼範例進行實驗

您可以針對曾在不同網站上 (例如年齡層或地理位置) 看過內容的使用者,評估特定客層。在這個範例中,系統會將 Content 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,並有任何意見,歡迎與我們分享。