Geolocation-based Consent Flow
Introduction
Sometimes is necessary to ask only users from specific countries or regions 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 U.S. California 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" ],
"usca": [ "preset-us-ca" ]
}
}
</script>
</amp-geo>
Defining the consent flow
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 California group, then the contents of the corresponding 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 California user would look like this:
{ "consentInstanceId": "world-wide-consent", "consentRequired": true, "promptUI": "usca-consent-ui", "postPromptUI": "post-consent-ui" }
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.
<amp-consent id="myUserConsent" layout="nodisplay">
<script type="application/json">{
"consentInstanceId": "world-wide-consent",
"consentRequired": false,
"geoOverride": {
"eea": {
"promptUI": "eea-consent-ui",
"consentRequired": true
},
"usca": {
"consentRequired": true,
"promptUI": "usca-consent-ui"
}
},
"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 EEA.</p>
<button on="tap:myUserConsent.accept">Accept</button>
<button on="tap:myUserConsent.reject">Reject</button>
</div>
</div>
<div id="usca-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 U.S. California.</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 beta-channel
here, for example:
- US California: https://amp.dev/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=us%20us-ca
- US (Not California): https://amp.dev/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=us
- EEA: https://amp.dev/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=de
- Neither: https://amp.dev/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=td
Nếu bạn vẫn còn thắc mắc sau khi đọc hướng dẫn trên trang này, hãy liên hệ với những người dùng AMP khác để thảo luận về trường hợp sử dụng cụ thể của bạn.
Truy cập Stack Overflow Một tính năng chưa được giải thích?Dự án AMP đặc biệt khuyến khích sự tham gia và đóng góp của bạn! Chúng tôi hi vọng bạn sẽ trở thành một người tham gia tích cực trong cộng đồng mã nguồn mở của chúng tôi, nhưng chúng tôi cũng chào mừng các đóng góp đơn lẻ về vấn đề mà bạn đặc biệt quan tâm.
Chỉnh sửa mẫu trên GitHub