Mengukur demografi pengguna

Produser konten sering kali ingin memahami demografi audiensnya. Anda dapat menggunakan Penyimpanan Bersama untuk mencatat data demografis pengguna dalam konteks tempat data tersebut dimiliki, seperti situs pihak pertama, lalu menggunakan pelaporan gabungan untuk menyertakan data tersebut dalam laporan dari situs lain, seperti konten yang disematkan.

Shared Storage API adalah proposal Privacy Sandbox untuk penyimpanan lintas situs tujuan umum, yang mendukung banyak kemungkinan kasus penggunaan. Private Aggregation API adalah output yang tersedia di Shared Storage yang memungkinkan Anda menggabungkan data lintas situs.

Coba pengukuran demografi pengguna

Untuk bereksperimen dengan pengukuran demografi pengguna dengan Penyimpanan Bersama dan Agregasi Pribadi, pastikan Anda menggunakan Chrome Canary dan Dev M107 atau yang lebih baru. Kemudian, aktifkan tanda Eksperimen Privacy Sandbox Ads API di chrome://flags/#privacy-sandbox-ads-apis.

Tetapkan eksperimen Privacy Sandbox Ads API ke aktif untuk menggunakan API ini.

Anda juga dapat mengaktifkan Penyimpanan Bersama dengan tanda --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames di command line.

Bereksperimen dengan contoh kode

Sebaiknya ukur demografi tertentu dari pengguna yang telah melihat konten Anda di berbagai situs, misalnya rentang usia atau lokasi geografis. Dalam contoh ini, dimensi ID konten, ID kelompok usia, dan ID geografi dienkode ke dalam kunci agregasi (bucket), dan jumlahnya digunakan sebagai nilai gabungan. Laporan ringkasan yang dihasilkan akan memberikan informasi seperti "Sekitar 391 pengguna yang telah melihat ID konten 123 berusia antara 18-39 tahun dan berasal dari Eropa".

Dalam contoh ini:

  • demographic-measurement.js dimuat melalui frame, dan bertanggung jawab untuk memuat worklet penyimpanan bersama.
  • demographic-measurement-worklet.js adalah worklet penyimpanan bersama yang membaca data demografi dalam penyimpanan bersama dan mengirim laporan melalui Private Aggregation API.

store-demographic-data.js

(Berjalan pada waktu tertentu sebelum pengukuran dilakukan untuk menetapkan data demografi ke Penyimpanan Bersama)

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 时有反馈意见,我们非常期待收到您的宝贵意见。