進行 A/B 測試

使用「共用儲存空間」小程式執行 A/B 測試。

Shared Storage API 是 Privacy Sandbox 提案之一,適用於一般用途的跨網站儲存空間。這個 API 支援許多可能的用途。例如 A/B 測試,可在 Chrome 104.0.5086.0 以上版本中進行測試。

您可以將使用者指派給實驗群組,然後將該群組儲存至共用儲存空間,以便在跨網站環境中存取。

嘗試 A/B 測試

如要透過共用儲存空間進行 A/B 測試,請確認您目前使用的是 Chrome 104.0.5086.0 以上版本。接著,在 chrome://flags/#privacy-sandbox-ads-apis 中啟用 Privacy Sandbox Ads API 實驗旗標。

將 Privacy Sandbox Ads API 實驗設為啟用以使用這些 API

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

使用程式碼範例進行實驗

如要確認實驗是否達到預期效果,可以跨多個網站執行 A/B 測試。身為廣告客戶或內容製作人,您可以選擇根據使用者被指派的群組,顯示不同的內容或廣告。群組指派作業會儲存在共用儲存空間中,但無法竊取。

在這個例子中:

  • ab-testing.js 應嵌入在頁框中,該頁框可對應一個控制項和兩個實驗內容。指令碼會呼叫實驗的共用儲存小程式。
  • ab-testing-worklet.js 是共用儲存工作小程式,可傳回使用者被指派的群組,以決定要顯示的廣告。

ab-testing.js

// Randomly assigns a user to a group 0 or 1
function getExperimentGroup() {
  return Math.round(Math.random());
}

async function injectContent() {
  // Register the Shared Storage worklet
  await window.sharedStorage.worklet.addModule('ab-testing-worklet.js');

  // Assign user to a random group (0 or 1) and store it in Shared Storage
  window.sharedStorage.set('ab-testing-group', getExperimentGroup(), {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation
  const fencedFrameConfig = await window.sharedStorage.selectURL(
    'ab-testing',
    [
      { url: `https://your-server.example/content/default-content.html` },
      { url: `https://your-server.example/content/experiment-content-a.html` }
    ],
    {
      resolveToConfig: true
    }
  );

  // Render the chosen URL into a fenced frame
  document.getElementById('content-slot').config = fencedFrameConfig;
}

injectContent();

ab-testing-worklet.js

class SelectURLOperation {
  async run(urls, data) {
    // Read the user's experiment group from Shared Storage
    const experimentGroup = await this.sharedStorage.get('ab-testing-group');

    // Return the corresponding URL (first or second item in the array)
    return urls.indexOf(experimentGroup);
  }
}

register('ab-testing', SelectURLOperation);

Use cases

These are only some of the possible use cases for Shared Storage. We'll continue to add examples as we receive feedback and discover new use cases.

Content selection

Select and display different content on different websites in fenced frames based on information collected in Shared Storage. The output gate for these use cases is URL selection.

  • Creative rotation: Store data, such as creative ID, view counts, and user interaction, to determine which creative users' see across different sites.
  • A/B testing: You can assign a user to an experiment group, then store that group in Shared Storage to be accessed cross-site.
  • Custom user experiences: Share custom content and calls-to-action based on a user's registration status or other user states

Generate summary reports

Collect information with Shared Storage and generated a noisy, aggregated summary report. The output gate for these use cases is the Private Aggregation API.

  • Unique reach measurement: Many content producers and advertisers want to know how many unique people saw their content. Use Shared Storage to record the first time a user saw your ad, embedded video, or publication, and prevent duplicative counting of that same user on different sites. You can then use the Private Aggregation API to output a summary report for your reach.
  • Demographics measurement: Content producers often want to understand the demographics of their audience. You can use Shared Storage to record user demographic data in a context where you have it, such as your first-party site, and use aggregated reporting to report on it across many other sites, such as embedded content.
  • K+ frequency measurement: Sometimes described as "effective frequency," there is often a minimum number views before a user will recognize or recall certain content (often in the context of advertisement views). You can use Shared Storage to build reports of unique users that have seen a piece of content at least K number of times.

交流及分享意見回饋

共用儲存空間提案仍在進行中的討論,日後可能會有變動。如果您試用這個 API,並有任何意見,歡迎與我們分享。