Misurare i dati demografici degli utenti

Gli autori di contenuti spesso vogliono conoscere i dati demografici del proprio pubblico. Puoi utilizzare Shared Storage per registrare i dati demografici degli utenti in un contesto in cui li hai, ad esempio il tuo sito proprietario, e poi utilizzare i report aggregati per includerli nei report di altri siti, ad esempio i tuoi contenuti incorporati.

L'API Shared Storage è una proposta di Privacy Sandbox per l'archiviazione cross-site per uso generico, che supporta molti possibili casi d'uso. L'API Private Aggregation è un output disponibile in Shared Storage che consente di aggregare i dati tra siti.

Prova la misurazione dei dati demografici degli utenti

Per sperimentare la misurazione dei dati demografici degli utenti con Shared Storage e Private Aggregation, verifica di utilizzare Chrome Canary e Dev M107 o versioni successive. Abilita tutte le API per la privacy degli annunci in chrome://settings/adPrivacy.

Puoi anche attivare Shared Storage con il flag --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames nella riga di comando.

Sperimenta con esempi di codice

Potresti voler misurare determinati dati demografici degli utenti che hanno visualizzato i tuoi contenuti su diversi siti, ad esempio la fascia d'età o la posizione geografica. In questo esempio, le dimensioni ID contenuto, ID gruppo di età e ID area geografica sono codificate nella chiave di aggregazione (bucket) e il conteggio viene utilizzato come valore aggregabile. Il report di riepilogo generato fornirà informazioni come "Circa 391 utenti che hanno visualizzato l'ID contenuto 123 hanno un'età compresa tra 18 e 39 anni e provengono dall'Europa".

In questo esempio:

  • demographic-measurement.js viene caricato utilizzando un frame ed è responsabile del caricamento del worklet di Shared Storage.
  • demographic-measurement-worklet.js è il worklet di Shared Storage che legge i dati demografici in Shared Storage e invia un report utilizzando l'API Private Aggregation.

store-demographic-data.js

(Viene eseguito in un determinato momento prima che venga effettuata la misurazione per impostare i dati demografici in Shared Storage)

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

    // Don't 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 using the Private Aggregation API
    privateAggregation.contributeToHistogram({ bucket, value });

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

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

Engage and share feedback

Note that the Shared Storage API proposal is under active discussion and development and therefore subject to change.

We're eager to hear your thoughts on the Shared Storage API.