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 show for users from US and another consent dialog that will show for users in the EEA (a predefined country group).

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 iso code us which represents the United States, preset-eea, and unknown

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

The flow should rely on amp-geo to determine which config to show based upon the user's location, so we set the flag: geoOverride.

If amp-geo finds that the user is in the US country group, then the contents of the US config will override the top-level consent config (and same for EEA).

Our final config for an EEA (and unknown) user would look like this:

{
  "consentInstanceId": "world-wide-consent",
  "consentRequired": true,
  "promptUI": "eea-consent-ui",
  "postPromptUI": "post-consent-ui"
}

Since consentRequired is true and promptUI is configured, amp-consent will show the prompt if no localstorage decision is found.

Our final config for a US user would look like this:

{
  "consentInstanceId": "world-wide-consent",
  "consentRequired": "remote",
  "checkConsentHref": "/documentation/examples/api/get-consent-sever-side",
  "promptUI": null,
  "postPromptUI": "post-consent-ui"
}

If no localstorage decision is found and since consentRequired is 'remote', amp-consent will wait until it gets a response from checkConsentHref. In this example, no matter what the response returned, it will not show anything because promptUI was overriden to null (since there could be an external site to manage user consent).

And our final config for a user in neither of those geo groups would be:

{
  "consentInstanceId": "world-wide-consent",
  "consentRequired": false,
  "postPromptUI": "post-consent-ui"
}

Localstorage decision will be used if it exists, otherwise, since consentRequired is false, amp-consent will not do anything.

Use expireCache: true in your response to never save consent states to localstorage.

<amp-consent id="myUserConsent"
  layout="nodisplay">
  <script type="application/json">
    {
      "consentInstanceId": "world-wide-consent",
      "consentRequired": false,
      "geoOverride": {
        "eea": {
          "promptUI": "eea-consent-ui",
          "consentRequired": true
        },
        "us": {
          "consentRequired": "remote",
          "checkConsentHref": "/documentation/examples/api/get-consent-server-side",
          "promptUI": null
        }
      },
      "postPromptUI": "post-consent-ui"
    }
  </script>
  <div id="eea-consent-ui"
    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 EA.</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:

需要进一步说明?

如果此页面的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与其中并作出贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您对热衷问题做出单次贡献。

在 GitHub 上编辑实例