Chạy thử nghiệm A/B

Sử dụng một worklet Shared Storage để chạy thử nghiệm A/B.

Shared Storage API là một đề xuất của Hộp cát về quyền riêng tư cho bộ nhớ đa dụng, trên nhiều trang web, hỗ trợ nhiều trường hợp sử dụng có thể xảy ra. Một ví dụ như vậy là thử nghiệm A/B. Bạn có thể thử nghiệm trong Chrome 104.0.5086.0 trở lên.

Bạn có thể chỉ định một người dùng vào một nhóm thử nghiệm, sau đó lưu trữ nhóm đó trong SharedStorage để truy cập trong môi trường trên nhiều trang web.

Thử nghiệm A/B

Để thử nghiệm A/B bằng Shared Storage, hãy xác nhận rằng bạn đang sử dụng Chrome 104.0.5086.0 trở lên. Bật tất cả các API Quyền riêng tư trong quảng cáo trong chrome://settings/adPrivacy.

Bạn cũng có thể bật Bộ nhớ dùng chung bằng cờ --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames trong dòng lệnh.

Thử nghiệm với mã mẫu

Để xem một thử nghiệm có mang lại hiệu quả như mong muốn hay không, bạn có thể chạy thử nghiệm A/B trên nhiều trang web. Là nhà quảng cáo hoặc nhà sản xuất nội dung, bạn có thể chọn hiển thị nội dung hoặc quảng cáo khác nhau dựa trên nhóm mà người dùng được chỉ định. Bài tập nhóm được lưu trong bộ nhớ dùng chung nhưng không thể trích xuất.

Trong ví dụ này:

  • ab-testing.js phải được nhúng trong một khung hình, trong đó ánh xạ một chế độ kiểm soát và hai nội dung thử nghiệm. Tập lệnh này gọi worklet bộ nhớ dùng chung cho thử nghiệm.
  • ab-testing-worklet.js là worklet lưu trữ dùng chung, trả về nhóm mà người dùng được chỉ định, xác định quảng cáo nào sẽ được hiển thị.

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 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

All available use cases for Select URL API can be found in this section. We'll continue to add examples as we receive feedback and discover new test cases.

Engage and share feedback

Note that the Select URL API proposal is under active discussion and development and subject to change.

We're eager to hear your thoughts on the Select URL API.