高级用户同意流程
简介
如今,用户希望对自己的在线体验有更多的控制权。此外,发布商在如何向用户提供通知和选择方面面临着各种不同的要求 - 从供应商政策到不断发展的法律要求。开源 AMP 项目正致力于为发布商和技术供应商提供工具,以实施他们偏好的用户控件,并支持他们在 AMP 页面上不同的个人合规性要求。
此页面演示了如何构建更高级的阻止同意流程。它将显示接受和拒绝按钮,单击后将显示更多信息,例如用户接受时使用的第三方列表,或用户拒绝时的一般数据使用通知。
设置
在标头中导入同意组件。
<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
将首先检查本地存储中是否存在现有同意决定,如果存在则使用它。否则,如果已配置且 consentRequired
为 true
,它将显示 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-consent
的 amp-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 上编辑示例