If your app uses the Google Analytics for Firebase SDK, start it with every consent type denied and pass each choice to it with setConsent. Your app configuration says which of Google's consent types each category stands for. Google describes the setup in Set up consent mode for apps.
Start with every type denied
These defaults hold until your app first calls setConsent on the device. After that, Firebase starts with the last values your app passed, so your app calls it at every start (below). Set all four types to denied. In your app's Info.plist:
<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/>
In your app's AndroidManifest.xml, inside the application tag:
<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" />
Basic or advanced consent mode
Google defines two ways to implement consent mode, in its Consent mode overview:
- Basic: the SDK collects nothing until the person grants consent. Set
FIREBASE_ANALYTICS_COLLECTION_ENABLEDtoNOinInfo.plist, orfirebase_analytics_collection_enabledtofalsein the manifest, callsetAnalyticsCollectionEnabled(true)after a grant, andsetAnalyticsCollectionEnabled(false)again after a withdrawal. Firebase keeps this setting across app starts too, so call it at the same points assetConsent(below), withfalsewherever you pass every type denied. - Advanced: the SDK runs from the start with the denied defaults above. While a type is denied, it stores no device identifiers for it and sends measurements without them.
Google's Configure Analytics data collection and usage describes the collection settings.
Which types each category stands for
The configuration's google_consent_mode.types_by_category lists, for each of the site's categories that Google's types belong to, the types it allows:
| Category | Consent types |
|---|---|
marketing | ad_storage, ad_user_data, ad_personalization |
analytics | analytics_storage |
A type is granted when the person allows a category that lists it, and denied otherwise. The functional category maps to functionality_storage, personalization_storage, which are not Firebase consent types. Your website's banner uses the same mapping.
When to call setConsent
- When the app starts, every time and as early as you can. Firebase keeps the last values across app starts, so a grant from an earlier start stays in force until your app calls
setConsentagain. Pass the stored choice only while it is still valid, that is while none of the reasons in When to ask again applies to it. Whether a stored choice is still valid depends on the person's region rule, so until the region lookup answers, or fails and you take the strictest rule, pass every type denied, and callsetConsentagain once you know the rule. Without a valid choice, pass every type denied, except under ahiddenrule (below). - Before your consent screen shows again, when one of those reasons applies: pass every type denied, so the earlier choice no longer applies while the person decides.
- After every choice in your consent screen: Accept all, Reject all, a saved preference or a later change.
- Before any choice, only where the person's region rule is
hidden: no consent screen shows there, so grant the types of every category, as your website's banner does. Underopt_in,opt_outandnotice_only, every type stays denied from the call at app start until the person chooses.
On iOS, pass ad_storage, ad_user_data and ad_personalization as granted only while App Tracking Transparency (ATT) reports authorized, under every rule and before a choice as after one, hidden included. Until then, pass them denied, and call setConsent again after each answer to the ATT prompt. Tracking prompt and store privacy forms says when to show the prompt: the ATT answer wins.
Pass all four types every time. When a person withdraws consent for analytics or ad storage, Firebase deletes all user properties, the ad personalisation setting included; passing all four sets it again from the choice.
Samples
Swift:
import AppTrackingTransparency
import FirebaseAnalytics
// Applies a choice to Firebase. Call it at every launch: with an empty set
// (every type denied) until you know the person's region rule, then with the
// stored choice while it is still valid, else with every category under a
// hidden rule and with an empty set under any other rule. Call
// it again after every new choice, with an empty set whenever your app asks
// again, and after every answer to the tracking prompt. The ad types are
// granted only while App Tracking Transparency reports authorized. It passes
// all four types every time, so the ad personalisation setting Firebase
// deletes after a withdrawal is set again.
func applyToFirebase(accepted: Set<String>, typesByCategory: [String: [String]]) {
let trackingAuthorized = ATTrackingManager.trackingAuthorizationStatus == .authorized
func granted(_ type: String) -> Bool {
typesByCategory.contains { category, types in
accepted.contains(category) && types.contains(type)
}
}
func grantedAd(_ type: String) -> Bool {
trackingAuthorized && granted(type)
}
Analytics.setConsent([
.analyticsStorage: granted("analytics_storage") ? .granted : .denied,
.adStorage: grantedAd("ad_storage") ? .granted : .denied,
.adUserData: grantedAd("ad_user_data") ? .granted : .denied,
.adPersonalization: grantedAd("ad_personalization") ? .granted : .denied,
])
}
Kotlin:
import android.content.Context
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.FirebaseAnalytics.ConsentStatus
import com.google.firebase.analytics.FirebaseAnalytics.ConsentType
import org.json.JSONObject
// Applies a choice to Firebase. Call it at every launch: with an empty set
// (every type denied) until you know the person's region rule, then with the
// stored choice while it is still valid, else with every category under a
// hidden rule and with an empty set under any other rule. Call
// it again after every new choice, and with an empty set whenever your app
// asks again. It passes all four types every time, so the ad personalisation
// setting Firebase deletes after a withdrawal is set again.
fun applyToFirebase(context: Context, accepted: Set<String>, typesByCategory: JSONObject) {
fun status(type: String): ConsentStatus {
val granted = typesByCategory.keys().asSequence().any { category ->
val types = typesByCategory.getJSONArray(category)
category in accepted && (0 until types.length()).any { types.getString(it) == type }
}
return if (granted) ConsentStatus.GRANTED else ConsentStatus.DENIED
}
FirebaseAnalytics.getInstance(context).setConsent(
mapOf(
ConsentType.ANALYTICS_STORAGE to status("analytics_storage"),
ConsentType.AD_STORAGE to status("ad_storage"),
ConsentType.AD_USER_DATA to status("ad_user_data"),
ConsentType.AD_PERSONALIZATION to status("ad_personalization"),
)
)
}
To check the values Firebase receives, turn on its verbose logging and look for the consent types in the Xcode console or in Android Studio's Logcat, as Google's guide describes.
Help with Consent Mode
If Firebase is not getting Consent Mode signals from your app, or you run into another Consent Mode setup, timing or measurement problem, email Kukie.io's Google client support at google-support@kukie.io first, before you contact Google. We answer within 1 business day.
To copy Google's Consent Mode support team, add cmp-support-helper@google.com to your email, or choose the Google Consent Mode topic in the contact form and select the box.
Google's documentation
- Set up consent mode for apps
- Consent mode overview (basic and advanced consent mode)
- Configure Analytics data collection and usage
