• websites

External User Consent Flow


Sometimes publishers want to collect user consent using external prompt UI flow with iframes. This sample demonstrates how you can use amp-consent together with amp-iframe to achieve this. In this sample we'll build a consent dialog from an iframe.


We need to import both, the amp-consent ...

<script async custom-element="amp-consent" src=""></script>

... and the amp-iframe extension.

<script async custom-element="amp-iframe" src=""></script>

We define a basic consent flow, similar to this sample. The consent dialog is from a CORS iframe loaded with the amp-iframe component

<amp-consent id="myUserConsent"
  <script type="application/json">
      "consentInstanceId": "iframe-consent",
      "checkConsent": "remote",
      "checkConsentHref": "/documentation/examples/api/get-consent",
      "promptUI": "myConsentFlow",
      "postPromptUI": "post-consent-ui"
  <div id="myConsentFlow"
    <div class="consentPopup">
      <amp-iframe layout="fill"
        sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
        <div placeholder>Loading</div>
  <div id="post-consent-ui">
    <button on="tap:myUserConsent.prompt()">Update Consent</button>

The consent dialog iframe can inform the amp-consent component user's choice through postMessages.

  type: 'consent-response',
  action: 'accept'
}, '*');

Here action can be either 'accept', 'reject' or 'dismiss'.

Use data-block-on-consent attribute to block AMP components until consent is given. Individual AMP components can override blocking behavior and implement blocking logic themselves.

Here is an image which is blocked until consent is given:

<amp-img data-block-on-consent
Ti servono altre spiegazioni?

Se le spiegazioni riportate in questa pagina non rispondono a tutte le tue domande, non esitare a contattare altri utenti AMP per discutere il tuo caso d'uso specifico.

Vai a Stack Overflow
Ci sono funzioni non descritte?

Il progetto AMP invita tutti a partecipare e dare il proprio contributo! Ci auguriamo che tu possa partecipare regolarmente alla nostra community open source, ma saremo anche lieti di ricevere eventuali contributi una-tantum sulle questioni che ti interessano.

Modifica esempio in GitHub