You can set up Google consent mode v2 for Android apps using Cookie Information Consent Management Platform. In this article, we’ll show you how to do it.
Before you start
Here are some things to know before you start:
- Google Consent Mode v2 lets you control the behavior of the Firebase Analytics SDK based on user consent. By default, Firebase does not apply any consent mode values, meaning it will not collect user data unless you set it up to.
- If you want to collect user data, you should set the default for all consent types in Google consent mode v2. This prevents Firebase from collecting user data without explicit user consent.
Google consent mode v2 supports the following consent types:ad_user_dataad_storagead_personalizationanalytics_storage
- Cookie Information Consent Management Platform lets you handle user consent by updating Firebase through Google consent mode v2, keeping you in line with privacy regulations.
- Use the
setConsentmethod in the Firebase SDK to configure the appropriate consent types. - When a user changes their consent, you must inform the Firebase SDK. To update the consent values after the app starts, call the
setConsentmethod.
The value you set with the setConsent method overrides the default value and stays active across multiple app sessions. The value remains unchanged until setConsent is called again, even if the user closes and reopens the app. The setConsent updates only the parameters you specify.
Set up Google consent mode v2 for Android
1. To set up Google consent mode v2 for Android, you first need to disable data collection and storage by default. To do that, add the following XML to AndroidManifest.xml:
<meta-data android:name="google_analytics_default_allow_ad_user_data" android:value="false" />
<meta-data android:name="google_analytics_default_allow_ad_storage" android:value="false" />
<meta-data android:name="google_analytics_default_allow_ad_personalization_signals" android:value="false" />
<meta-data android:name="google_analytics_default_allow_analytics_storage"
android:value="false" />
2. When a user gives or updates their consent, update their current consent status using the setConsent method in the Firebase SDK:
Firebase.analytics.setConsent {
analyticsStorage(<consent status>)
adStorage(<consent status>)
adUserData(<consent status>)
adPersonalization(<consent status>)
}
3. Starting with Android SDK version 3.0, you can retrieve the most recent user consents using the getSavedItemsAsFlow() method.
Here’s an example using lifecycleScope:
lifecycleScope.launch{ConsentsUISDK.getSavedItemsAsFlow().collect { it.fold( onSuccess = { consentItems ->//this is on return from the popup, we get //the list of items and set your //data collectors accordingly} }, onFailure = { throwable -> Log.d(TAG, throwable.message ?: "no message") } )}
Example implementation of setting up Google consent mode v2 using the SDK:
private fun displayIfNeeded(coroutineScope: CoroutineScope) {
coroutineScope.launch {
ConsentsUISDK.showPrivacyPopupIfNeeded(this@MainActivity).collect {
it.fold(
onSuccess = { consentItems ->
// Map consent items to Firebase consent status
val consentMap = mapOf(
FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE to
if (consentItems.any { it.type == UIConsentType.STATISTICS && it.accepted })
FirebaseAnalytics.ConsentStatus.GRANTED
else
FirebaseAnalytics.ConsentStatus.DENIED,
FirebaseAnalytics.ConsentType.AD_STORAGE to
if (consentItems.any { it.type == UIConsentType.MARKETING && it.accepted })
FirebaseAnalytics.ConsentStatus.GRANTED
else
FirebaseAnalytics.ConsentStatus.DENIED,
FirebaseAnalytics.ConsentType.AD_USER_DATA to
if (consentItems.any { it.type == UIConsentType.MARKETING && it.accepted })
FirebaseAnalytics.ConsentStatus.GRANTED
else
FirebaseAnalytics.ConsentStatus.DENIED,
FirebaseAnalytics.ConsentType.AD_PERSONALIZATION to
if (consentItems.any { it.type == UIConsentType.FUNCTIONAL && it.accepted })
FirebaseAnalytics.ConsentStatus.GRANTED
else
FirebaseAnalytics.ConsentStatus.DENIED
)
analytics.setConsent(consentMap)
consentItems.forEach { consentItem ->
Log.d(TAG, "Consent given for ${consentItem.title}: ${consentItem.accepted}")
}
},
onFailure = { throwable ->
Log.d(TAG, throwable.message ?: "no message")
}
)
}
}
}
4. For Android SDK versions earlier than 3.0, use:
lifecycleScope.launch {
(applicationContext as App).sdk.getSavedConsentsWithType(object : CallListener<Map<UUID, ConsentWithType>> {
override fun onSuccess(result: Map<UUID, ConsentWithType>) {
val mapped = result.values.associate { it.type to it.consented }
Firebase.analytics.setConsent {
analyticsStorage(if(mapped[ConsentItem.Type.TypeStatistical] == true) ConsentStatus.GRANTED else ConsentStatus.DENIED )
adStorage(if(mapped[ConsentItem.Type.TypeMarketing] == true) ConsentStatus.GRANTED else ConsentStatus.DENIED )
adUserData(if(mapped[ConsentItem.Type.TypeMarketing] == true) ConsentStatus.GRANTED else ConsentStatus.DENIED )
adPersonalization(if(mapped[ConsentItem.Type.TypeFunctional] == true) ConsentStatus.GRANTED else ConsentStatus.DENIED )
}
}
override fun onFailure(error: IOException) {}
})
}
Check your Google consent mode v2 setup for your Android app
1. To check that your consent settings work as expected, first enable verbose logging for Firebase Analytics on your Android device. Run the following commands in your terminal:
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC
2. In Android Studio Logcat, look for log messages that confirm Firebase Analytics consent status updates. Look for a log message that starts with Setting consent.
For example, if ad_storage consent is granted, you’ll see a message like: Setting consent, ... AD_STORAGE=granted
Here’s the full log:
Setting storage consent(FE): source=API, ad_storage=granted, analytics_storage=granted
3. All done.