Do you build things with AMP? Fill out the AMP Developer Survey!
  • websites

Server Side User Consent Flow


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.


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

<script async custom-element="amp-consent" src=""></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"
  <script type="application/json">{
    "consentInstanceId": "server-side-consent",
    "consentRequired": "remote",
    "checkConsentHref": "/documentation/examples/api/get-consent-sever-side"
  <div id="myConsentFlow"
    <div class="consentPopup">
      <div class="dismiss-button"
      <p>This is an important message requiring you to make a choice if you're based if the server says you have not made a consent decision.</p>
      <button on="tap:myUserConsent.accept">Accept</button>
      <button on="tap:myUserConsent.reject">Reject</button>
  <div id="post-consent-ui">
    <button on="tap:myUserConsent.prompt()">Update Consent</button>
Требуются дальнейшие объяснения?

Если на этой странице нет ответов на все ваши вопросы, обратитесь к другим пользователям AMP и обсудите свой конкретный сценарий использования.

Перейти на Stack Overflow
Не нашли пояснения к функции?

Проект AMP активно поощряет ваше участие и сотрудничество! Мы надеемся, что вы станете постоянным участником нашего открытого сообщества, но разовые вклады в работу над задачами, которые вам особенно интересны, также приветствуются.

Редактировать пример на GitHub