细粒度的用户同意
简介
细粒度的同意允许用户在您的页面上进行一系列同意选择。全局同意允许他们为页面做出单一选择。amp-consent
组件允许您设置细粒度的同意、全局同意或两者同时设置。在此示例中,我们将向您展示如何使用细粒度的同意。
设置
首先,您需要导入 amp-consent
扩展的脚本。
<script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-0.1.js"></script>
amp-consent
允许您阻止 AMP 组件,直到用户表示同意。我们将使用 amp-fit-text
组件演示这一点,所以我们也导入该脚本。
<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
您需要为您的同意目的选择名称。在这里,我们将它们称为 purpose-marketing
、purpose-analytics
和 purpose-marketing
。
然后,您将创建一个包含以下内容的 <amp-consent>
组件:- 一个 JSON 配置对象 - 同意 UI,允许用户进行同意选择 - 可选的,重新提示 UI,允许用户再次显示同意 UI
配置对象
该文档列出了此对象可以包含的所有键。在这里,我们使用了这些
consentInstanceId
。这是必需的。consentRequired
。通过将其设置为true
,我们表示需要同意。要从端点获取此信息,我们将使用remote
。promptUI
。指定用户可以进行同意选择的对话框的id
。postPromptUI
。允许用户再次看到该对话框的区域的id
。uiConfig
。在此处包含"overlay": true
会告诉amp-consent
在对话框打开时使同意对话框外的区域变暗。
{ "consentInstanceId": "consent1", "consentRequired": true, "promptUI": "consentDialog", "postPromptUI": "repromptDialog", "uiConfig": { "overlay": true }, "purposeConsentRequired": ["purpose-analytics", "purpose-marketing"] }
同意对话框
这通常包含一组输入,通常是复选框,让用户进行同意选择。它还可能包含一些按钮,让用户保存他们的选择、保存或拒绝所有内容,或者只是关闭对话框。
当用户进行同意选择时,使用 setPurpose()
操作临时存储该信息。此操作的形式为 setPurpose({purpose type}={boolean value})
。
例如,在这里,如果用户点击我们的营销 cookie 复选框,我们将相应地设置我们的 purpose-marketing
目的
setPurpose(purpose-marketing=event.checked)
当 AMP 在相应的复选框上触发 change
事件时,我们设置该目的。以下是它的外观
<input id="consent-purpose-analytics" type="checkbox" on="change:consent1.setPurpose(purpose-marketing=event.checked)" >
要保存用户的选择,请使用 accept
操作。要隐藏同意 UI 而不保存选择,请使用 dismiss
操作。您的用户可能会选择某些同意目的,但不是所有目的。您可以通过将 accept
参数 (purposeConsentDefault={false|true})
传递给其余同意目的来设置他们的选择。
以下是这些按钮在我们的示例中的外观
<button class="choiceButton" on="tap:siteConsent.accept(purposeConsentDefault=false)"> Save </button> <button class="choiceButton" on="tap:siteConsent.dismiss"> Dismiss </button>
提示后 UI
最后,您可以创建一个区域,供用户再次显示同意 UI。使用 prompt
操作来完成此操作。以下是它在此示例中的外观
<div id="repromptDialog"> Do you want to make your consent choices again? <button on="tap:siteConsent.prompt">I do!</button> </div>
这是我们整个 <amp-consent> 组件。
您的隐私
您可以控制我们改进和个性化您的体验的方式。请选择您是否希望允许以下内容
点击“保存”以保存您的选择。
点击“关闭”以消除此对话框而不保存您的选择。
<amp-consent layout="nodisplay" id="siteConsent">
<script type="application/json">
{
"consentInstanceId": "consent1",
"consentRequired": true,
"promptUI": "consentPopup",
"postPromptUI": "repromptDialog",
"uiConfig": {
"overlay": true
},
"purposeConsentRequired": ["purpose-analytics", "purpose-marketing"]
}
</script>
<div id="consentPopup">
<div id="consentDialog">
<div class="dismissButton" role="button" tabindex="0" on="tap:siteConsent.dismiss">
𝗫
</div>
<h3>Your privacy</h3>
<p>
You can control the ways in which we improve and personalize your
experience. Please choose whether you wish to allow the following:
</p>
<div class="choices">
<label class="consentLabel" for="consent-purpose-marketing">
<input id="consent-purpose-marketing" type="checkbox" on="change:siteConsent.setPurpose(purpose-marketing=event.checked)">
Marketing cookies
</label>
<label class="consentLabel" for="consent-purpose-conversion">
<input id="consent-purpose-conversion" type="checkbox" on="change:siteConsent.setPurpose(purpose-conversion=event.checked)">
Conversion tracking cookies
</label>
<label class="consentLabel" for="consent-purpose-analytics">
<input id="consent-purpose-analytics" type="checkbox" on="change:siteConsent.setPurpose(purpose-analytics=event.checked)">
Analytics
</label>
<button class="choiceButton" on="tap:siteConsent.accept(purposeConsentDefault=false)">
Save
</button>
<button class="choiceButton" on="tap:siteConsent.dismiss">
Dismiss
</button>
</div>
<p>
Click "Save" to save your choices.
<br>
Click "Dismiss" to get rid of this dialog box without saving your
choices.
</p>
</div>
</div>
<div id="repromptDialog">
Do you want to make your consent choices again?
<button on="tap:siteConsent.prompt">I do!</button>
</div>
</amp-consent>
阻止组件
如果用户尚未接受正确的同意目的,您可以标记任何 AMP 组件被阻止。在 data-block-on-consent-purposes
属性中以逗号分隔的列表形式指定这些目的。
(在此处查看您的同意选择的结果)
<div id="consentDemoArea">
<p class="note">(See the results of your consent choices here)</p>
<amp-fit-text id="marketingContent" width="200" height="50" layout="fixed" data-block-on-consent-purposes="purpose-marketing">
I'm shown if you allow marketing cookies.
</amp-fit-text>
<amp-fit-text id="conversionContent" width="200" height="50" layout="fixed" data-block-on-consent-purposes="purpose-marketing,purpose-analytics">
I'm shown if you allow marketing AND analytics cookies.
</amp-fit-text>
<amp-fit-text id="analyticsContent" width="200" height="50" layout="fixed" data-block-on-consent-purposes="purpose-analytics">
I'm shown if you allow analytics cookies.
</amp-fit-text>
</div>
如果此页面上的解释没有涵盖您所有的问题,请随时与其他 AMP 用户联系,讨论您的确切用例。
前往 Stack Overflow 未解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您为自己特别感兴趣的问题做出一次性贡献。
在 GitHub 上编辑示例-
撰写者: @morss