AMP
  • 网站

高级用户同意流程

简介

如今,用户希望对自己的在线体验有更多控制权。此外,发布商在如何向用户提供通知和选择方面面临着各种不同的需求——从供应商政策到不断变化的法律要求。开源 AMP 项目正在努力为发布商和技术供应商提供工具,以实施其首选的用户控件,并支持其 AMP 页面上各种不同的合规性要求。

此页面演示如何构建更高级的阻止同意流程。它将显示接受和拒绝按钮,点击这些按钮将显示更多信息,例如用户接受时使用的第三方列表,或者用户拒绝时的一般数据使用通知。

同意信息会保存到本地存储中 - 在接受/拒绝一次后,只有在删除此主机的本地存储内容后才会再次出现弹出对话框,例如通过Chrome 开发工具

设置

在标题中导入同意组件。

<script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-0.1.js"></script>

在此示例中,我们使用 amp-ad 以及...

<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>

...amp-list 以及...

<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>

...amp-mustache

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>

基本用法

amp-consent 组件允许您使用必需的 consentRequired 标志指定此页面所需的​​用户同意。可以通过 checkConsentHref 属性指定 CORS 端点,该端点将通过 POST 请求进行检查。响应应如下所示

{
  "consentRequired": boolean (required),
  "consentStateValue": enum (accepted/rejected/unknown) (optional),
  "consentString": string (optional),
  "expireCache": boolean (default false),
}

响应将指示是否将显示在 promptUI 中给定的 id 的元素。 amp-consent 将首先检查本地存储中是否存在同意决定,如果存在则使用它。否则,如果配置了 promptUiconsentRequiredtrue,它将显示 promptUi。如果 consentRequired 设置为 false,`amp-consent 将立即解除阻止所有元素。

<amp-consent id="myConsent" layout="nodisplay">
      <script type="application/json">{
        "consentInstanceId": "advanced-consent",
        "consentRequired": "remote",
        "checkConsentHref": "/documentation/examples/api/get-consent",
        "promptUI": "consentDialog",
        "postPromptUI": "post-consent-ui"
      }</script>
      <div class="lightbox" id="consentDialog">
        <div class="lightbox-content">
          <div class="dismiss-button" role="button" tabindex="0" on="tap:myConsent.dismiss">X</div>
          <div class="message">
            <h2>Headline</h2>
            <p>This is an important message requiring you to make a choice.</p>
          </div>
          <div id="choice">
            <button on="tap:choice2.show,choice1.hide">Choice 2</button>
          </div>
          <div id="choice1" hidden class="message">
            <p>This is some more information about this choice. Here's a list of items related to this choice.</p>
            <amp-list height="132" layout="fixed-height" src="/json/consent-items.json" binding="no">
              <template type="amp-mustache">
                <li>{{.}}</li>
              </template>
            </amp-list>
            <button on="tap:myConsent.accept">Confirm</button>
          </div>
          <div id="choice2" hidden class="message">
            <p>This is some more information about this choice.</p>
            <button on="tap:myConsent.reject">Confirm</button>
          </div>
        </div>
      </div>
      <div id="post-consent-ui">
        <button on="tap:myConsent.prompt()">Update Consent</button>
      </div>
</amp-consent>

使用 data-block-on-consent 属性来阻止 AMP 组件,直到获得同意。各个 AMP 组件可以覆盖阻止行为并自行实现阻止逻辑。

这是一个图像,该图像在获得同意之前会被阻止

<amp-img data-block-on-consent src="/static/samples/img/landscape_lake_300x201.jpg" width="300" height="201">
</amp-img>

广告也可以被阻止,直到获得同意,但广告网络可以实施自己的行为(例如,默认为未经个性化的广告,而无需获得同意,如 此处针对 Doubleclick 的文档所示)。请注意,当接受或拒绝此特定示例的同意时,您可能会看到相同的广告,这是因为即使在接受同意的情况下,该示例也不使用个性化信息。

这是一个取决于 amp-consentamp-ad 示例

<amp-ad data-block-on-consent data-slot="/30497360/a4a/a4a_native" height="250" type="doubleclick" width="300">
</amp-ad>
需要进一步解释?

如果此页面上的解释没有涵盖您的所有问题,请随时与其他 AMP 用户联系,以讨论您的确切用例。

转到 Stack Overflow
无法解释的功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您针对您特别感兴趣的问题做出一次性贡献。

在 GitHub 上编辑示例