วัดข้อมูลประชากรของผู้ใช้

ผู้ผลิตเนื้อหามักต้องการทำความเข้าใจข้อมูลประชากรของผู้ชม คุณสามารถใช้พื้นที่เก็บข้อมูลที่ใช้ร่วมกันเพื่อบันทึกข้อมูลประชากรของผู้ใช้ในบริบทที่คุณมี เช่น เว็บไซต์บุคคลที่หนึ่ง จากนั้นใช้การรายงานแบบรวมเพื่อรวมข้อมูลดังกล่าวในรายงานจากเว็บไซต์อื่นๆ เช่น เนื้อหาที่ฝัง

Shared Storage API เป็นข้อเสนอ Privacy Sandbox สำหรับพื้นที่เก็บข้อมูลข้ามเว็บไซต์สำหรับวัตถุประสงค์ทั่วไป ซึ่งรองรับ Use Case ที่เป็นไปได้หลายกรณี Private Aggregation API เป็นเอาต์พุตที่พร้อมใช้งานในพื้นที่เก็บข้อมูลที่ใช้ร่วมกันซึ่งช่วยให้คุณรวบรวมข้อมูลข้ามเว็บไซต์ได้

ลองใช้การวัดข้อมูลประชากรของผู้ใช้

หากต้องการทดสอบการวัดข้อมูลประชากรของผู้ใช้ด้วยพื้นที่เก็บข้อมูลที่ใช้ร่วมกันและการรวมส่วนตัว ให้ตรวจสอบว่าคุณใช้ Chrome Canary และ Dev M107 หรือใหม่กว่า จากนั้นเปิดใช้แฟล็กการทดสอบ Privacy Sandbox API ที่ chrome://flags/#privacy-sandbox-ads-apis

ตั้งค่าการทดสอบ Privacy Sandbox API เป็นเปิดใช้เพื่อใช้ API เหล่านี้

คุณยังเปิดใช้พื้นที่เก็บข้อมูลที่ใช้ร่วมกันด้วยแฟล็ก --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames ในบรรทัดคำสั่งได้ด้วย

การทดสอบกับตัวอย่างโค้ด

คุณอาจต้องการวัดข้อมูลประชากรบางอย่างของผู้ใช้ที่ดูเนื้อหาในเว็บไซต์ต่างๆ เช่น ช่วงอายุหรือสถานที่ตั้งทางภูมิศาสตร์ ในตัวอย่างนี้ มิติข้อมูล Content ID, รหัสกลุ่มอายุ และรหัสภูมิศาสตร์จะเข้ารหัสไว้ในคีย์การรวม (ที่เก็บข้อมูล) และระบบจะใช้จำนวนดังกล่าวเป็นค่าที่รวบรวมได้ รายงานสรุปที่สร้างขึ้นจะให้ข้อมูล เช่น "ผู้ใช้ประมาณ 391 คนที่เห็น Content ID 123 มีอายุระหว่าง 18-39 ปีและมาจากยุโรป"

ในตัวอย่างนี้

  • demographic-measurement.js โหลดผ่านเฟรม และมีหน้าที่รับผิดชอบในการโหลด Worklet พื้นที่เก็บข้อมูลที่ใช้ร่วมกัน
  • demographic-measurement-worklet.js เป็น Worklet พื้นที่เก็บข้อมูลที่ใช้ร่วมกัน ซึ่งจะอ่านข้อมูลประชากรในพื้นที่เก็บข้อมูลที่ใช้ร่วมกันและส่งรายงานผ่าน 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 นี้แล้วมีความคิดเห็น เรายินดีรับฟัง