AMP
  • 網站

進階使用者同意流程

簡介

現今使用者希望對其線上體驗有更多控制權。此外,發布商在如何向使用者提供通知和選擇方面,也面臨各種不同的要求,從供應商政策到不斷演變的法律要求。開放原始碼 AMP 專案正致力於為發布商和技術供應商提供工具,以便在其 AMP 頁面上實作其偏好的使用者控制項,並支援其各種個別的法規遵循要求。

本頁面示範如何建置更進階的封鎖式同意流程。它將顯示「接受」和「拒絕」按鈕,按一下這些按鈕將顯示更多資訊,例如使用者接受時使用的第三方清單,或使用者拒絕時的一般資料使用通知。

同意聲明會儲存到本機儲存空間中,在接受/拒絕一次後,只有在刪除此主機的本機儲存空間內容後,例如透過Chrome 開發人員工具,才會再次顯示彈出式對話方塊。

設定

在標頭中匯入 consent 元件。

<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 旗標,指定此頁面所需的User Consent。CORS 端點可以透過 checkConsentHref 屬性指定,並將透過 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-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 上編輯範例