AMP
  • websites

Server Side User Consent Flow

Introduction

Sometimes is necessary to rely on external servers for consent due to needing to sync user consent between multiple platforms. This sample demonstrates how you can use amp-consent to achieve this. In this sample we'll build a consent dialog that will only show based upon the consent state from external servers.

Setup

We need to import the amp-consent extension...

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

We define a basic server-side consent flow, similar to this sample. The flow should only trigger when the server responds that consentRequired is true and there is no consentStateValue or consentString for this user.

When a user lands on the AMP page, amp-consent will check localstorage for an existing consent state. If a consent state exists, it will unblock all components. Otherwise, it will wait for the response from checkConsentHref (because consentRequired is set to 'remote') to determine if consent is required/if there is an existing consent state. If there is an existing consent state, amp-consent will use that value and unblock. Otherwise, it will show the promptUi.

A recommended design for sites that can manage their own consent (via external page) is to respond with the following response:

{
  "consentRequired": true,
  "consentStateValue": "accepted"/"rejected"/"unknown",
  "consentString": "example-string"
}

Use expireCache: true, in your response to clear localStorage consent decision if you need to update it for syncing purposes. Additonally, onUpdateHref can be used when users are managing their consent directly on the page.

<amp-consent id="myUserConsent" layout="nodisplay">
  <script type="application/json">{
    "consentInstanceId": "server-side-consent",
    "consentRequired": "remote",
    "checkConsentHref": "https://amp.dev/documentation/examples/api/get-consent-server-side",
    "promptUI": "myConsentFlow"
  }</script>
  <div id="myConsentFlow" class="popupOverlay">
    <div class="consentPopup">
      <div class="dismiss-button" role="button" tabindex="0" on="tap:myUserConsent.dismiss">X</div>
      <h2>Headline</h2>
      <p>If the server says you have not made a consent decision, we require you to make a choice.</p>
      <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>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기