ConsentManager
Gathers and manages user consent for ads using Google's User Messaging Platform (UMP).
Use this service to request consent information, present the consent form (for example, the GDPR/EEA consent dialog) and the privacy options form, and query the consent status before requesting ads.
A typical flow looks like this:
request_consent_info_update(at every app launch);load_and_show_consent_form_if_required(shows the form only if needed);can_request_ads(to decide whether to start loading ads).
More info in the AdMob documentation.
Raises:
- FletUnsupportedPlatformException - When any of its methods are called on a web and/or non-mobile platform.
Inherits: Service
Methods
can_request_ads- Indicates whether the app has gathered enough consent to request ads.get_consent_status- Get the user’s consent status.get_privacy_options_requirement_status- Gets the requirement status for showing a privacy options entry point.is_consent_form_available- Checks whether a consent form is available to be loaded and shown.load_and_show_consent_form_if_required- Loads a consent form and immediately shows it if consent is required.request_consent_info_update- Requests an update of the user's consent information.reset- Resets the consent state.show_privacy_options_form- Presents the privacy options form to the user.
Examples
AdMob Consent (UMP)
import flet as ft
import flet_ads as fta
def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
# mobile-only
supported = not page.web and page.platform.is_mobile()
if supported:
page.services.append(consent_manager := fta.ConsentManager())
async def refresh_status():
consent_status = await consent_manager.get_consent_status()
privacy_status = await consent_manager.get_privacy_options_requirement_status()
can_request = await consent_manager.can_request_ads()
status.value = (
f"Consent status: {consent_status.name}\n"
f"Privacy options required: {privacy_status.name}\n"
f"Can request ads: {can_request}"
)
# Only surface the privacy options entry point when we are required to.
privacy_button.visible = (
privacy_status == fta.PrivacyOptionsRequirementStatus.REQUIRED
)
page.update()
async def gather_consent(e: ft.Event[ft.OutlinedButton]):
status.value = "Gathering consent…"
status.update()
try:
# Remember to remove the debug settings in production.
await consent_manager.request_consent_info_update(
fta.ConsentRequestParameters(
consent_debug_settings=fta.ConsentDebugSettings(
debug_geography=fta.DebugGeography.EEA,
# test_identifiers=["<HASHED_ID>"], # for physical devices only
),
)
)
# Loads and shows the consent form only if consent is required.
await consent_manager.load_and_show_consent_form_if_required()
await refresh_status()
# At this point, if `can_request_ads()` is True, it is safe to start
# loading ads (e.g. fta.BannerAd / fta.InterstitialAd).
except Exception as ex:
status.value = f"Error: {ex}"
status.update()
async def show_privacy_options(e: ft.Event[ft.OutlinedButton]):
try:
await consent_manager.show_privacy_options_form()
await refresh_status()
except Exception as ex:
status.value = f"Error: {ex}"
page.update()
async def reset_consent(e: ft.Event[ft.OutlinedButton]):
# For testing only: resets consent so you can replay the flow.
try:
await consent_manager.reset()
status.value = 'Consent reset. Tap "Gather consent" to begin again.'
privacy_button.visible = False
except Exception as ex:
status.value = f"Error: {ex}"
page.update()
page.add(
ft.SafeArea(
content=ft.Column(
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
controls=[
ft.Text(
"Gather user consent (e.g. GDPR/EEA) with Google's User "
"Messaging Platform before requesting ads.",
text_align=ft.TextAlign.CENTER,
),
ft.OutlinedButton(
content="Gather consent",
on_click=gather_consent,
disabled=not supported,
),
privacy_button := ft.OutlinedButton(
content="Show privacy options form",
visible=False,
on_click=show_privacy_options,
),
ft.OutlinedButton(
content="Reset consent (testing)",
on_click=reset_consent,
disabled=not supported,
),
ft.Divider(),
status := ft.Text('Tap "Gather consent" to begin.'),
],
)
),
)
if __name__ == "__main__":
ft.run(main)
Methods
can_request_adsasync
can_request_ads() -> boolIndicates whether the app has gathered enough consent to request ads.
Returns:
- bool -
Trueif ads can be requested,Falseotherwise.
get_consent_statusasync
get_consent_status() -> ConsentStatusGet the user’s consent status.
This value is cached between app sessions and can be read before requesting an update of the consent information.
Returns:
- ConsentStatus - The user's current consent status.
get_privacy_options_requirement_statusasync
get_privacy_options_requirement_status() -> (
PrivacyOptionsRequirementStatus
)Gets the requirement status for showing a privacy options entry point.
Returns:
- PrivacyOptionsRequirementStatus - Whether a privacy options entry point (for example, a button that calls
show_privacy_options_form) is required.
is_consent_form_availableasync
is_consent_form_available() -> boolChecks whether a consent form is available to be loaded and shown.
request_consent_info_update should be awaited before calling
this method.
Returns:
- bool -
Trueif a consent form is available,Falseotherwise.
load_and_show_consent_form_if_requiredasync
load_and_show_consent_form_if_required()Loads a consent form and immediately shows it if consent is required.
If consent is not required, this method completes without showing
anything. This is the simplest way to gather consent: call
request_consent_info_update first, then this method.
request_consent_info_updateasync
request_consent_info_update(
params: ConsentRequestParameters | None = None,
)Requests an update of the user's consent information.
This should be called (and awaited) at every app launch, before loading/showing a consent form or reading the consent status.
Parameters:
- params (ConsentRequestParameters | None, default:
None) - Parameters such as debug settings (useful for testing the form during development) or the under-age tag. IfNone, default parameters are used.
resetasync
reset()Resets the consent state.
This is intended only for testing, allowing you to simulate a first-time user. It should not be used in production.
show_privacy_options_formasync
show_privacy_options_form()Presents the privacy options form to the user.
This lets users change or withdraw their consent after the initial
choice. Only present it when get_privacy_options_requirement_status
returns PrivacyOptionsRequirementStatus.REQUIRED.