基於地理位置的同意聲明流程
簡介
有時,只需要向特定國家或地區的使用者要求同意聲明。此範例示範如何搭配 amp-consent
和 amp-geo
來達成此目的。在此範例中,我們將建立一個同意聲明對話方塊,該對話方塊會向美國加州的使用者顯示,另一個同意聲明對話方塊則會向歐洲經濟區 (EEA,預先定義的國家/地區群組) 的使用者顯示。
設定
我們需要匯入 amp-consent
和 ...
<script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-0.1.js"></script>
... amp-geo
擴充功能。
<script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>
設定 amp-geo
首先,我們需要設定 amp-geo
擴充功能。我們將使用代表美國的 ISO 代碼 us
、preset-eea
和 unknown
。
<amp-geo layout="nodisplay">
<script type="application/json">
{
"ISOCountryGroups": {
"eea": [ "preset-eea", "unknown" ],
"usca": [ "preset-us-ca" ]
}
}
</script>
</amp-geo>
定義同意聲明流程
流程應依賴 amp-geo 來判斷要根據使用者位置顯示哪個設定,因此我們設定旗標:geoOverride
。
如果 amp-geo 發現使用者位於美國加州群組,則對應設定的內容會覆寫頂層同意聲明設定 (EEA 的情況亦然)。
EEA (和不明) 使用者的最終設定會如下所示
{ "consentInstanceId": "world-wide-consent", "consentRequired": true, "promptUI": "eea-consent-ui", "postPromptUI": "post-consent-ui" }
由於 consentRequired
為 true
且已設定 promptUI
,如果找不到本機儲存空間決策,amp-consent
就會顯示提示。
美國加州使用者的最終設定會如下所示
{ "consentInstanceId": "world-wide-consent", "consentRequired": true, "promptUI": "usca-consent-ui", "postPromptUI": "post-consent-ui" }
既非上述地理位置群組的使用者的最終設定會是
{ "consentInstanceId": "world-wide-consent", "consentRequired": false, "postPromptUI": "post-consent-ui" }
如果本機儲存空間決策存在,系統會加以使用;否則,由於 consentRequired
為 false
,amp-consent
將不會執行任何動作。
<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>
測試
您可以透過將自訂國家/地區代碼附加到網址並在此處啟用 beta-channel
here 來測試不同的行為,例如:- 美國加州:https://amp.dev.org.tw/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=us%20us-ca - 美國 (非加州):https://amp.dev.org.tw/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=us - EEA:https://amp.dev.org.tw/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=de - 皆非:https://amp.dev.org.tw/documentation/examples/user-consent/geolocation-based_consent_flow#amp-geo=td
如果本頁的說明未涵蓋您的所有問題,請隨時與其他 AMP 使用者聯繫,討論您的確切使用案例。
前往 Stack Overflow 有未說明的特色功能嗎?AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎您針對您特別關注的問題做出一次性貢獻。
在 GitHub 上編輯範例