You can set up Google consent mode v2 for iOS apps using Cookie Information Consent Management Platform. In this article, we’ll show you how to do it.
Before you start
Here are some things to know before you start:
- Google Consent Mode v2 lets you control the behavior of the Firebase Analytics SDK based on user consent. By default, Firebase does not apply any consent mode values.
- To collect user data, you should set the default for all consent types in Google consent mode v2. This prevents Firebase from collecting user data without explicit user consent.
Google consent mode v2 supports the following consent types:ad_user_dataad_storagead_personalizationanalytics_storage
- Cookie Information Consent Management Platform lets you handle user consent by updating Firebase through Google consent mode v2, keeping you in line with privacy regulations.
- Use the
setConsentmethod in the Firebase SDK to configure the appropriate consent types. - When a user changes their consent, you must inform the Firebase SDK. To update the consent values after the app starts, call the
setConsentmethod.
The value you set with the setConsent method overrides the default value and stays active across multiple app sessions. The value remains unchanged until setConsent is called again, even if the user closes and reopens the app. The setConsent updates only the parameters you specify.
Set up Google consent mode v2 for iOS apps
1. To set up Google consent mode v2 for iOS apps, you first need 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.
3. Here’s an optional extension that maps the Cookie Information consent status to Firebase Analytics’ ConsentStatus type:
extension UserConsent {
var consentStatus: ConsentStatus {
return isSelected ? .granted : .denied
}
}
mobileConsentsSDK.showPrivacyPopUp(customViewType: style.customController) { settings in
settings.forEach { consent in
switch consent.purpose {
case .statistical:
Analytics.setConsent([.analyticsStorage: consent.consentStatus])
case .functional: break
case .marketing:
Analytics.setConsent([
.adStorage: consent.consentStatus,
.adUserData: consent.consentStatus,
.adPersonalization: consent.consentStatus
])
case .necessary: break
case .custom:
if consent.purposeDescription.lowercased() == "age consent" {
// handle user defined consent items such as age consent
}
if consent.consentItem.id == "<UUID of consent item>" {
// handle user defined consent items such as age consent based on it's UUID
}
}
print("Consent given for:\(consent.purpose): \(consent.isSelected)")
}
} errorHandler: { err in
print("Ooops, we've encountered an error: \(err.localizedDescription)")
}
}
Check your Google consent mode v2 setup for your iOS app
To check that your consent settings work as expected, you need to first enable verbose logging, which helps you debug Firebase Analytics by providing detailed logs. To do that, follow these steps:
- In Xcode, click the scheme selector next to the Run button.
- Select Edit Scheme
- Select Arguments
- Under Arguments Passed On Launch, add this argument:
-FIRAnalyticsVerboseLoggingEnabled - In the Xcode debug console, search for the following terms:
ad_storage,analytics_storage,ad_user_data, andad_personalization.
For example, if ad storage is enabled, you’ll see the following log message: ad_storage is granted.
Here’s the full log:
11.12.0 - [FirebaseAnalytics][I-ACS023221]
ad_storage is granted.
analytics_storage is granted.
ad_user_data is granted.
ad_personalization is granted.
6. Done.