AMP Conf 2019. April 17/18. Tokyo.
AMP
  • websites

Geolocation-based Consent Flow

Introduction

Sometimes is necessary to ask only users from specific countries for consent. This sample demonstrates

how you can use amp-consent together with amp-geo to achieve this. In this sample we'll build a

consent dialog that will only show for users from the EU.

Setup

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

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

... and the amp-geo extension.

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

Setting up amp-geo

First we need to setup the amp-geo extension. We'll use the present eea which includes all countries of the European Economic Area.

<amp-geo layout="nodisplay">
  <script type="application/json">
    {
      "ISOCountryGroups": {
        "eu": ["preset-eea"]
      }
    }
  </script>
</amp-geo>

We define a basic consent flow, similar to this sample. The flow should only trigger for users in the EU, so we set the flag: "promptIfUnknownForGeoGroup": "eu".

<amp-consent id="myUserConsent"
  layout="nodisplay">
  <script type="application/json">
    {
      "consents": {
        "eu": {
          "promptIfUnknownForGeoGroup": "eu",
          "promptUI": "myConsentFlow"
        }
      },
      "postPromptUI": "post-consent-ui"
    }
  </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>This is an important message requiring you to make a choice if you're based in the EU.</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>

Testing

You can test different behaviors by appending custom country codes to the URL and enabling the dev-channel here, for example:

Need further explanation?

If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.

Go to Stack Overflow
An unexplained feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Edit sample on GitHub