广东将升级改造12对服务区洗手间

百度 唐代译经家。

使用共享存储空间 Worklet 来识别已知客户。

Shared Storage API 是一项 Privacy Sandbox 提案,适用于通用的跨网站存储,支持许多可能的用例。例如,识别已知客户,您可以在 Chrome 104.0.5086.0 及更高版本中测试此功能。

您可以将用户是否已在您的网站上注册存储到 Shared Storage,然后根据用户的存储状态(用户是否为“已知”客户)呈现单独的元素。

设置已知客户

如需试用在“共享存储空间”中识别已知客户,请确认您使用的是 Chrome 104.0.5086.0 或更高版本。启用 chrome://settings/adPrivacy 下的所有广告隐私权 API。

您还可以在命令行中使用 --enable-features=PrivacySandboxAdsAPIsOverride,OverridePrivacySandboxSettingsLocalTesting,SharedStorageAPI,FencedFrames 标志启用共享存储空间。

试验代码示例

您可能希望根据用户是否在其他网站上看到过广告来呈现不同的元素。例如,付款服务提供商可能希望根据用户是否已在其网站上注册,呈现“注册”或“立即购买”按钮。共享存储空间可用于设置用户的状态,并根据该状态定制用户体验。

在此示例中:

  • known-customer.js 嵌入在帧中。此脚本用于设置网站上应显示的按钮:“注册”或“立即购买”。
  • known-customer-worklet.js 是用于确定用户是否已知的共享存储空间 Worklet。如果用户已知,系统会返回相应信息。如果用户未知,系统会返回相应信息以显示“注册”按钮,并将用户标记为已知用户以备日后使用。

known-customer.js

// The first URL for the "register" button is rendered for unknown users.
const BUTTON_URLS = [
  { url: `http://${advertiserUrl}/ads/register-button.html` },
  { url: `http://${advertiserUrl}/ads/buy-now-button.html` },
];

async function injectButton() {
  // Load the worklet module
  await window.sharedStorage.worklet.addModule('known-customer-worklet.js');

  // Set the initial status to unknown ('0' is unknown and '1' is known)
  window.sharedStorage.set('known-customer', 0, {
    ignoreIfPresent: true,
  });

  // Run the URL selection operation to choose the button based on the user status
  const fencedFrameConfig = await window.sharedStorage.selectURL('known-customer', BUTTON_URLS, {
    resolveToConfig: true
  });

  // Render the opaque URL into a fenced frame
  document.getElementById('button-slot').src = fencedFrameConfig;
}

injectButton();

known-customer-worklet.js

class SelectURLOperation {
  async run(urls) {
    const knownCustomer = await sharedStorage.get('known-customer');

    // '0' is unknown and '1' is known
    return parseInt(knownCustomer);
  }
}

register('known-customer', SelectURLOperation);

使用场景

本部分介绍了 Select 网址 API 的所有可用用例。我们会根据收到的反馈和发现的新测试用例,不断添加示例。

  • 轮替广告素材:存储广告素材 ID 和用户互动等数据,以确定用户在不同网站上看到的广告素材。
  • 按频次选择广告素材:使用观看次数数据确定用户在不同网站上看到的广告素材。
  • 运行 A/B 测试:您可以将用户分配到实验组,然后将该组存储在 Shared Storage 中以供跨网站访问。
  • 为已知客户量身定制体验:根据用户的注册状态或其他用户状态分享自定义内容和号召性用语。

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.

Stay Informed

  • Mailing List: Subscribe to our mailing list for the latest updates and announcements related to the Select URL and Shared Storage APIs.

Need Help?