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
이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.
Stack Overflow로 이동 설명이 부족한 기능을 발견하셨나요?AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.
GitHub에서 샘플 수정하기