Google consent mode v2 lets you control Firebase Analytics SDK's behavior based on user consent. You do this by using the setConsent
method in the Firebase SDK and choosing a consent type.
Cookie Information Mobile Consent Management platform makes it easy to handle user consents. It updates Firebase through Google consent mode v2, keeping you in line with legal standards.
Google consent mode v2 supports the following consent types:
ad_user_data
ad_storage
ad_personalization
analytics_storage
How Google Consent Mode v2 works
By default, Firebase has no consent mode values set.
If you plan to start collecting user data, you should set 'default' for all consent types in Google consent mode v2. This prevents Firebase from collecting user data without explicit user consent.
It's also important to inform the Firebase SDK when a user changes their consent. To update the consent values after the app starts, call the 'setConsent' method.
The value you set using the setConsent
method overrides the default and remains effective through multiple app uses. The value stays unchanged until setConsent
is called again, even if the user closes and reopens the app.
setConsent
updates only the parameters you specify.
Setting up Google Consent Mode v2
Android
To disable data collection and storage by default, 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" />When a user makes or changes their consent decision, update their current consent status. Use the
setConsent
method to refresh consents in the Firebase SDK:
Firebase.analytics.setConsent {
analyticsStorage(<consent status>)
adStorage(<consent status>)
adUserData(<consent status>)
adPersonalization(<consent status>)
}
With the Cookie Information Android SDK, you can fetch the most recent user consents. These consents are stored as Boolean values and must be mapped to ConsentStatus.GRANTED
and ConsentStatus.DENIED
for use with Firebase.
โ
From version 3+, use:
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") } )}
Up to version 3, use this:
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) {}
})
}
iOS
To disable data collection and storage by default, add the following key-value pairs to your application's Info.plist:
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_USER_DATA</key>
<false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_STORAGE</key>
<false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_PERSONALIZATION_SIGNALS</key>
<false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_ANALYTICS_STORAGE</key>
<false/>When a user makes or changes their consent decision, update their current consent status. Use the 'setConsent' method to refresh consents in the Firebase SDK. Here's an optional extension example that maps the Cookie Information consent status to the Firebase Analytics ConsentStatus type:
extention UserConsent {
var consentStatus: ConsentStatus {
return isSelected ? ConsentStatus.GRANTED : ConsentStatus.DENIED
}
}
mobileConsentsSDK.showPrivacyPopUp() { settings in
settings.forEach { consent in
switch consent.purpose {
case .statistical:
Analytics.setConsent(.analyticsStorage: consent.consentStatus
case .functional: break
case .marketing: break
Analytics.setConsent([
.adStorage: consent.consentStatus,
.adUserData: consent.consentStatus,
.adPersonalization: consent.consentStatus,
])
case .necessary: break
@unknown default:
break
}
print("Consent given for:\(consent.purpose): \(consent.isSelected)")
}
Analytics.setConsent([
.analyticsStorage: ,
.adStorage: <consent status>,
.adUserData: <consent status>,
.adPersonalization: <consent status>,
])
}
Checking your setup
You can check to see that your consent settings are working as intended by following the steps described below.
Android
Enable verbose logging on your device.
In the Android Studio Logcat, look for the log message that starts with 'Setting consent'.
For example, if ad storage is enabled, you'll see a log message like this:
'Setting consent, ... AD_STORAGE=granted'.
iOS
Enable verbose logging on your device.
In the Xcode debug console, search for the following terms:
ad_storage
,analytics_storage
,ad_user_data
,ad_personalization
.
For example, if ad storage
is enabled, you'll see a log message like this:
ad_storage is granted
.