All Collections
Mobile Consent Solution
Consent Mode v2 implementation - Mobile apps
Consent Mode v2 implementation - Mobile apps

This article will walk you through implementing Consent Mode v2 with Cookie Information for Android and iOS apps.

Katarina Hansen avatar
Written by Katarina Hansen
Updated over a week ago

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's Mobile Consent Management platform makes it easy to handle user consents. It updates Firebase through Consent Mode v2, keeping you in line with legal standards.

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 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 then reopens the app.

'setConsent' updates only the parameters you specify.

Setting up Google Consent Mode v2

Android

  1. 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" />

  2. 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.

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

  1. 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/>

  2. 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

  1. Enable verbose logging on your device.

  2. 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

  1. Enable verbose logging on your device.

  2. 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'.

Did this answer your question?