Google Analytics for Firebase offers a consent mode that allows you to adjust the SDK’s behavior based on your user’s consent status. This guide will walk you through integrating Google Consent Mode v2 for Firebase Analytics using the iubenda SDK.
Integrating Google Consent Mode v2 in your App
Before starting, ensure Firebase SDK is correctly integrated into your app. Find more information here for Android and iOS. Follow this guide to integrate the iubenda SDK with your app.
Setting Up Consent Mode
To set up Consent Mode v2 in your app, you need to:
- Set the default consent state: By default, no consent mode values are set. You need to define these in your app’s manifest (Android) or Info.plist (iOS) file.
- Update consent values: Based on user preferences captured through the iubenda SDK, update the consent values in Firebase Analytics.
Consider that consent types indicate the type of storage being used. These types include:
Consent Type | Description |
---|---|
ad_storage | Enables storage, such as cookies (web) or device identifiers (apps), related to advertising. |
ad_user_data | Sets consent for sending user data to Google for online advertising purposes. |
ad_personalization | Sets consent for personalized advertising. |
analytics_storage | Enables storage, such as cookies (web) or device identifiers (apps), related to analytics, for example, visit duration. |
functionality_storage | Enables storage that supports the functionality of the website or app, for example, language settings |
personalization_storage | Enables storage related to personalization, for example, video recommendations |
security_storage | Enables storage related to security such as authentication functionality, fraud prevention, and other user protection |
You can find the official documentation for using Consent mode with Firebase SDK here.
Let’s see how it works: ⬇️
For Android Apps
1. Set default consent state
First, you need to set the default consent state for your app since by default, no consent mode values are set
To set them, open the manifest file (the AndroidManifest.xml) and add the consent mode key-value pairs.
The keys provided are:
- google_analytics_default_allow_analytics_storage
- google_analytics_default_allow_ad_storage
- google_analytics_default_allow_ad_user_data
- google_analytics_default_allow_ad_personalization_signals
The keys are set with a value that indicates the consent state:
– true
, meaning consent was granted, or
– false
, meaning consent was denied.
In the example below, we added all the default values set to false
:
<meta-data android:name="google_analytics_default_allow_analytics_storage" 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_user_data" android:value="false" />
<meta-data android:name="google_analytics_default_allow_ad_personalization_signals" android:value="false" />
Next, you need to implement the consent values update method.
2. Update consent values
- Open the Activity/Fragment where you prompt the cookie notice with the iubenda SDK,
- implement “
IubendaCMPChangeListener
” interface with this code:
public class MainActivity extends AppCompatActivity implements IubendaCMPChangeListener
- add “
onConsentChanged()
” override method to get consent changes:
@Override
public void onConsentChanged() {
}
Doing so, the iubenda SDK will trigger onConsentChanged()
whenever a user sets their consent preferences (e.g. with the IubendaCMP.askConsent()
method.
After capturing consent settings, you need to send them to Firebase Analytics SDK
To update consent values after the user has expressed their preferences, you need to call the setConsent
method.
Use this code to update the different consent values to granted
:
@Override
public void onConsentChanged() {
Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> consentMap = new EnumMap<>(FirebaseAnalytics.ConsentType.class);
if(IubendaCMP.isPurposeEnabled(4)){
consentMap.put(FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE, FirebaseAnalytics.ConsentStatus.GRANTED);
}
if(IubendaCMP.isPurposeEnabled(5)){
consentMap.put(FirebaseAnalytics.ConsentType.AD_STORAGE, FirebaseAnalytics.ConsentStatus.GRANTED);
consentMap.put(FirebaseAnalytics.ConsentType.AD_USER_DATA, FirebaseAnalytics.ConsentStatus.GRANTED);
consentMap.put(FirebaseAnalytics.ConsentType.AD_PERSONALIZATION, FirebaseAnalytics.ConsentStatus.GRANTED);
}
mFirebaseAnalytics.setConsent(consentMap);
}
As you can see, the ANALYTICS_STORAGE consent type is mapped with the purpose 4 of the Privacy Controls and Cookie Solution (Measurement), while the others (AD_STORAGE, AD_USER_DATA and AD_PERSONALIZATION) are mapped on the purpose 5 of the Privacy Controls and Cookie Solution (Marketing).
Verify consent settings
To verify that your consent settings are working as expected, enable verbose logging on your device, and in the Android Studio logcat, find the log message that starts with Setting consent
.
For iOS Apps
1. Set default consent state
First, you need to set the default consent state for your app since, by default, no consent mode values are set.
To set them, open the Info.plist file and add the consent mode key-value pairs.
The keys provided are:
- GOOGLE_ANALYTICS_DEFAULT_ALLOW_ANALYTICS_STORAGE
- GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_STORAGE
- GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_USER_DATA
- GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_PERSONALIZATION_SIGNALS
The keys are set with a value that indicates consent state:
– true
, meaning consent was granted, or
– false
, meaning consent was denied.
In the example below, we added all the default values set to false
:
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_ANALYTICS_STORAGE</key> <false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_STORAGE</key> <false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_USER_DATA</key> <false/>
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_PERSONALIZATION_SIGNALS</key> <false/>
Next, you need to implement the consent values update method.
2. Update consent values
- Open the ViewController where you prompt the cookie notice with the iubenda SDK,
- implement “
NSNotification.Name.ConsentChanged
” selector with this code:
NotificationCenter.default.addObserver(self, selector: #selector(consentDidChange), name: NSNotification.Name.ConsentChanged, object: nil)
- add “
consentDidChange()
” method to get consent changes:
@objcfunc consentDidChange() {
}
Doing so, the iubenda SDK will trigger consentDidChange()
whenever a user sets their consent preferences (e.g. with the IubendaCMP.askConsent(from: self)
method.
After capturing consent settings, you need to send them to Firebase Analytics SDK.
To update consent values after the user has expressed their preferences, you need to call the setConsent
method.
Use this code to update the different consent values to granted
:
@objc func consentDidChange() {
var analyticsStorageGranted = false
var adStorageGranted = false
var adUserDataGranted = false
var adPersonalizationGranted = false
if(IubendaCMP.isPurposeEnabled(id: 4)){
analyticsStorageGranted = true
}else{
analyticsStorageGranted = false
}
if(IubendaCMP.isPurposeEnabled(id: 5)){
adStorageGranted = true
adUserDataGranted = true
adPersonalizationGranted = true
}else{
adStorageGranted = false
adUserDataGranted = false
adPersonalizationGranted = false
}
Analytics.setConsent([
.analyticsStorage: analyticsStorageGranted ? .granted : .denied,
.adStorage: adStorageGranted ? .granted : .denied,
.adUserData: adUserDataGranted ? .granted : .denied,
.adPersonalization: adPersonalizationGranted ? .granted : .denied,
])
}
As you can see, the analyticsStorage
consent type is mapped with the purpose 4 of the Privacy Controls and Cookie Solution (Measurement), while the others (adStorage
, adUserData
and adPersonalization
) are mapped on the purpose 5 of the Privacy Controls and Cookie Solution (Marketing).
Verify consent settings
To verify that your consent settings are working as expected, enable verbose logging on your device, and in the Xcode, find the log message related to the parameters. For example:
ad_storage is granted.
analytics_storage is granted.
ad_user_data is granted.
ad_personalization is granted.
Remember, these settings help tailor your app’s functionality to respect user preferences and legal requirements. For detailed steps and code snippets, refer to the official documentation and ensure your app is set up correctly.