用戶端使用者同意流程
簡介
現今使用者希望對其線上體驗有更多的控制權。此外,發布商在如何向使用者提供通知和選擇方面臨各種不同的需求,從供應商政策到不斷演變的法律要求。開放原始碼 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 端點。然後 amp-consent
元件將透過 POST 請求檢查是否需要顯示同意 UI。回應應如下所示
{ "consentRequired": boolean (required), "consentStateValue": enum (accepted/rejected/unknown) (optional), "consentString": string (optional), "expireCache": boolean (default false), }
可以透過 myConsent.prompt()
動作重新觸發同意對話方塊。其中一個使用案例是讓使用者在關閉同意對話方塊後選擇變更其設定。為了使此功能運作,需要在 amp-consent
JSON 設定中指定同意後 UI:"promptUI": "consentDialog"
。如果在內嵌指令碼設定中將 consentRequired
設定為 true
,則 amp-consent
將首先檢查 localstorage 中是否存在現有的同意決定,並在存在時使用它。否則,它將顯示 promptUi(如果已設定)。如果 consentRequired
設定為 false,則 amp-consent
將立即解除封鎖所有元素。
<amp-consent id="myUserConsent" layout="nodisplay">
<script type="application/json">{
"consentInstanceId": "consent-id",
"consentRequired": true,
"promptUI": "consentDialog",
"postPromptUI": "post-consent-ui"
}</script>
<div class="popupOverlay" id="consentDialog">
<div class="consentPopup">
<div class="dismiss-button" role="button" tabindex="0" on="tap:myUserConsent.dismiss">X</div>
<h2>Headline</h2>
<p>This is an important message requiring you to make a choice.</p>
<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="/static/samples/json/consent-items.json" binding="no">
<template type="amp-mustache">
<li>{{.}}</li>
</template>
</amp-list>
<button on="tap:myUserConsent.accept">Accept</button>
<button on="tap:myUserConsent.reject">Reject</button>
</div>
</div>
<div id="post-consent-ui">
<button on="tap:myUserConsent.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-ad
依賴 amp-consent
的範例
<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 上編輯範例