Pomiar danych demograficznych użytkowników

Producenci treści często chcą poznać dane demograficzne swoich odbiorców. Możesz korzystać z pamięci współdzielonej, aby rejestrować dane demograficzne użytkowników tam, gdzie je masz, np. w swojej własnej witrynie, a potem używać raportów zbiorczych, aby uwzględniać te dane w raportach z innych witryn, np. z umieszczonych treści.

Shared Storage API to oferta pakietowa Piaskownicy prywatności do ogólnego przeznaczenia do przechowywania w różnych witrynach, która obsługuje wiele możliwych przypadków użycia. Private Aggregation API to dane wyjściowe dostępne w pamięci współdzielonej, które umożliwiają agregowanie danych z różnych witryn.

Wypróbuj pomiar grupy demograficznej użytkowników

Aby eksperymentować z pomiarami demograficznymi użytkowników za pomocą pamięci współdzielonej i agregacji prywatnej, potwierdź, że używasz Chrome Canary i Dev M107 lub nowszej wersji. Włącz wszystkie interfejsy Ad Privacy API w sekcji chrome://settings/adPrivacy.

Możesz też włączyć pamięć współdzieloną, używając flagi --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames w wierszu poleceń.

Eksperyment z przykładowym kodem

Możesz mierzyć określone dane demograficzne użytkowników, którzy widzieli Twoje treści w różnych witrynach, np. przedział wiekowy lub położenie geograficzne. W tym przykładzie wymiary identyfikatora treści, identyfikatora grupy wiekowej i identyfikatora geograficznego są zakodowane w kluczu agregacji (zasobnik), a liczba jest używana jako wartość agregowana. Wygenerowany raport podsumowujący będzie zawierał informacje takie jak „Około 391 użytkowników, którzy zobaczyli identyfikator treści 123, jest w wieku od 18 do 39 lat i pochodzi z Europy”.

W tym przykładzie:

  • Plik demographic-measurement.js jest ładowany za pomocą ramki i odpowiada za wczytanie Workletu pamięci współdzielonej.
  • demographic-measurement-worklet.js to Worklet pamięci współdzielonej, który odczytuje dane demograficzne w pamięci współdzielonej i wysyła raport za pomocą interfejsu Private Aggregation API.

store-demographic-data.js

(jest przeprowadzany w pewnym momencie przed wykonaniem pomiaru w celu przeniesienia danych demograficznych do pamięci współdzielonej).

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

Angażuj i dziel się opiniami

Oferta pamięci współdzielonej jest obecnie przedmiotem dyskusji i w przyszłości może ulec zmianie. Jeśli wypróbowujesz ten interfejs API i chcesz podzielić się opinią, chętnie poznamy Twoją opinię.