Google Consent Mode

Google Analytics offers Consent Mode to adjust how your Google tags behave based on the consent status of your users. You can indicate whether consent has been granted for Analytics and Ads identifiers. To implement Consent Mode for apps, use the Google Analytics for Firebase SDK to set a default setting and use the setConsent API to manage the setting programmatically, based on in-app user consent. To learn more, see Consent Mode.

Initial setup

By default, Analytics and Ad storage are enabled. Follow the instructions below to change the default state for your app.

Analytics storage

Android
iOS
To disable Analytics storage, set the value of google_analytics_default_allow_analytics_storage to false in the application tag of your app's AndroidManifest.xml file. For example:
<meta-data android:name="google_analytics_default_allow_analytics_storage" android:value="false" />
To disable Analytics storage, set the value of GOOGLE_ANALYTICS_DEFAULT_ALLOW_ANALYTICS_STORAGE to NO in your app's Info.plist file. For example, viewed in the source XML:
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_ANALYTICS_STORAGE</key>
<false/>

Ad storage

Android
iOS
To disable Ad storage, set the value of google_analytics_default_allow_ad_storage to false in the application tag of your app's AndroidManifest.xml file. For example:
<meta-data android:name="google_analytics_default_allow_ad_storage" android:value="false" />
To disable Ad storage, set the value of GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_STORAGE to NO in your app's Info.plist file. For example, viewed in the source XML:
<key>GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_STORAGE</key>
<false/>
To re-enable Analytics and Ad storage, such as after an end-user provides consent, call the setConsent instance method of Analytics.
To update the Analytics and Ad storage values after an app has launched, call the setConsent method.
The value set by the setConsent method persists across app executions and overrides the default setting. The value remains in that state until setConsent is called again, even if a user closes and reopens the app. Calling setConsent to modify Ad storage does not change the state of Analytics storage.
The following example shows the setConsent method updating values for Analytics and Ad storage to granted or denied:
Java
Kotlin
Swift
Obj-C
// Set consent types.
Map<ConsentType, ConsentSetting> consentMap = new EnumMap<>(ConsentType.class);
consentMap.put(ConsentType.ANALYTICS_STORAGE, ConsentStatus.GRANTED|ConsentStatus.DENIED);
consentMap.put(ConsentType.AD_STORAGE, ConsentStatus.GRANTED|ConsentStatus.DENIED);
mFirebaseAnalytics.setConsent(consentMap);
Firebase.analytics.setConsent {
analyticsStorage(ConsentStatus.GRANTED|ConsentStatus.DENIED)
adStorage(ConsentStatus.GRANTED|ConsentStatus.DENIED)
}
Analytics.setConsent([
.analyticsStorage: .granted|.denied
.adStorage: .granted|.denied
])
[FIRAnalytics setConsent:@{
FIRConsentTypeAnalyticsStorage : FIRConsentStatusGranted|FIRConsentStatusDenied
FIRConsentTypeAdStorage : FIRConsentStatusGranted|FIRConsentStatusDenied
}];
Google Vendor id is google will be used for AD_STORAGE. Google Analytics Vendor (custom vendor) id is c:googleana-4TXnJigR will be used for ANALYTICS_STORAGE.
The following example shows how to listen the changes to the UserStatus from Didomi SDK and apply the changes to Google Consent Mode:
Java
Kotlin
Swift
Obj-C
Didomi.getInstance().addEventListener(new EventListener() {
@Override
public void consentChanged(ConsentChangedEvent event) {
// The consent status of the user has changed
UserStatus userStatus = Didomi.getInstance().getUserStatus();
// Enabled consent ids for vendors
Set<String> enabledVendorsConsentIds = userStatus.getVendors().getConsent().getEnabled();
// Search for Google vendors
ConsentStatus analyticsStorageStatus = enabledVendorsConsentIds.contains("c:googleana-4TXnJigR") ? ConsentStatus.GRANTED : ConsentStatus.DENIED
ConsentStatus adStorageStatus = enabledVendorsConsentIds.contains("google") ? ConsentStatus.GRANTED : ConsentStatus.DENIED
// Set consent types
Map<ConsentType, ConsentSetting> consentMap = new EnumMap<>(ConsentType.class);
consentMap.put(ConsentType.ANALYTICS_STORAGE, analyticsStorageStatus);
consentMap.put(ConsentType.AD_STORAGE, adStorageStatus);
mFirebaseAnalytics.setConsent(consentMap);
}
})
Didomi.getInstance().addEventListener(object : EventListener() {
override fun consentChanged(event: ConsentChangedEvent) {
// The consent status of the user has changed
val userStatus = Didomi.getInstance().userStatus
// Enabled consent ids for vendors
val enabledVendorsConsentIds = userStatus.vendors.consent.enabled
// Search for Google vendors
val analyticsStorageStatus = if (enabledVendorsConsentIds.contains("c:googleana-4TXnJigR")) ConsentStatus.GRANTED else ConsentStatus.DENIED
val adStorageStatus = if (enabledVendorsConsentIds.contains("google")) ConsentStatus.GRANTED else ConsentStatus.DENIED
// Set consent types
Firebase.analytics.setConsent {
analyticsStorage(analyticsStorageStatus)
adStorage(adStorageStatus)
}
}
})
let didomiEventListener = EventListener()
didomiEventListener.onConsentChanged = { event in
// The consent status of the user has changed
let userStatus = Didomi.shared.getUserStatus()
// Enabled consent ids for vendors
let enabledVendorsConsentIds = userStatus.vendors.consent.enabled
// Search for Google vendors
let analyticsStorageStatus = enabledVendorsConsentIds.contains("c:googleana-4TXnJigR") ? .granted : .denied
let adStorageStatus = enabledVendorsConsentIds.contains("google") ? .granted : .denied
// Set consent types
Analytics.setConsent([
.analyticsStorage: googleConsentModeStatus
.adStorage: googleConsentModeStatus
])
}
DDMEventListener *didomiEventListener = [[DDMEventListener alloc] init];
[didomiEventListener setOnConsentChanged:^(enum DDMEventType event) {
// The consent status of the user has changed
Didomi *didomi = [Didomi shared];
DDMUserStatus *userStatus = [didomi getUserStatus];
// Enabled consent ids for vendors
NSSet *enabledVendorsConsentIDs = [[[userStatus vendors] consent] enabled];
// Search for Google vendors
FIRConsentType *analyticsStorageStatus = [enabledVendorsConsentIDs containsObject:@"c:googleana-4TXnJigR"] ? FIRConsentStatusGranted : FIRConsentStatusDenied;
FIRConsentType *adStorageStatus = [enabledVendorsConsentIDs containsObject:@"google"] ? FIRConsentStatusGranted : FIRConsentStatusDenied;
// Set consent types
[FIRAnalytics setConsent:@{
FIRConsentTypeAnalyticsStorage : analyticsStorageStatus
FIRConsentTypeAdStorage : adStorageStatus
}];
}];