Google Consent Mode v2 Support for inApp SDKs
Looking for a CMP that supports Google Consent Mode? See our Google Consent Mode v2 product page.
This guide provides instructions on how to integrate Google Consent Mode with the custom ConsentManager in your Android or iOS application.
Prerequisites
- Ensure Consent Mode is enabled (Menu > CMPs > Integrations > Google Consent Mode)
- Ensure Google Analytics, Google Ads or the other Google services are in your vendorlist
- A Firebase project with Google Analytics enabled.
- Firebase SDK integrated into your iOS project.
-
CMPManager
set up in your project.
Overview
Below you'll find helper methods that will allow you to retrieve and manage Google Consent Status using our CMP SDK.
iOS
/// Synchronizes the consent status from CMP to Firebase Analytics
func syncConsentToFirebase() {
let cmpManager = CMPManager.shared
guard let googleConsentModeStatus = cmpManager.getGoogleConsentModeStatus() else {
print("Google Consent Mode status not available")
return
}
// Define all expected consent types
let consentTypes = [
"analytics_storage",
"ad_storage",
"ad_user_data",
"ad_personalization"
]
// Build Firebase settings with proper defaults
var firebaseConsentSettings = [String: String]()
// Set defaults for all expected types (denied)
for consentType in consentTypes {
firebaseConsentSettings[consentType] = "denied"
}
// Override with actual values from CMP
for (key, value) in googleConsentModeStatus {
if consentTypes.contains(key) {
firebaseConsentSettings[key] = value
}
}
// Set the consent in Firebase
Analytics.setConsent(firebaseConsentSettings)
print("Firebase consent settings updated: \(firebaseConsentSettings)")
}
Android
/// Synchronizes the consent status from CMP to Firebase Analytics
fun updateFirebaseConsent(firebaseAnalytics: FirebaseAnalytics, cmpManager: CMPManager) {
// Get consent settings from CMP SDK
val cmpConsentSettings = cmpManager.getGoogleConsentModeStatus()
// Convert to Firebase's types
val firebaseConsentSettings = mutableMapOf<ConsentType, ConsentStatus>()
cmpConsentSettings.forEach { (key, value) ->
try {
val consentType = ConsentType.valueOf(key.uppercase())
val consentStatus = if (value == "granted")
ConsentStatus.GRANTED else ConsentStatus.DENIED
firebaseConsentSettings[consentType] = consentStatus
} catch (e: IllegalArgumentException) {
Log.w("ConsentManager", "Unknown consent type: $key")
}
}
// Update Firebase consent
firebaseAnalytics.setConsent(firebaseConsentSettings)
Log.d("ConsentManager", "Updated Firebase consent: $firebaseConsentSettings")
}