Set up consent mode for apps

This page is for developers that use the Google Analytics for Firebase SDK in their app and want to integrate consent mode. For an introduction to consent mode, read Consent mode overview.

Google Analytics offers consent mode to adjust how your SDK behaves based on the consent status of your users. You can implement consent mode in a basic or advanced way. If you aren't sure whether to implement basic or advanced consent mode, learn more about basic versus advanced consent mode and check with your company's guidelines.

Before you begin

Before you can manage user consent, you need to implement:

  • Google Analytics for Firebase SDK
  • A consent settings banner to capture user consent

To set up consent mode, you need to:

  1. Before a user grants consent: Temporarily disable Analytics collection.
  2. Set the default consent state.
  3. Update the consent state based on the user interaction with your consent settings.
  4. Re-enable Analytics collection.

Temporarily disable Analytics collection

A basic implementation of consent mode means blocking the Google Analytics for Firebase SDK from loading until a user grants consent.

To temporarily disable Analytics collection, set the value offirebase_analytics_collection_enabled to false in your app's AndroidManifest.xml in the application tag. For example:

<meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />

By default, no consent mode values are set. To set the default consent state for your app:

  1. Open your app's AndroidManifest.xml file.
  2. Add the consent mode key-value pairs. The key describes the consent type and the value indicates consent state. Values can either be true, meaning consent was granted, or false, meaning consent was denied.

    In accordance with the updates to consent mode for traffic in European Economic Area (EEA), a value of eu_consent_policy can be set for ad_user_data and ad_personalization, meaning consent is denied only for users in regions subject to the EU User Consent Policy.

    Set the following:

    • 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
  3. Save your changes. Next, implement the mechanism to update consent values.

For example, to set all grant consent for all parameters by default:

<meta-data android:name="google_analytics_default_allow_analytics_storage" android:value="true" />
<meta-data android:name="google_analytics_default_allow_ad_storage" android:value="true" />
<meta-data android:name="google_analytics_default_allow_ad_user_data" android:value="true" />
<meta-data android:name="google_analytics_default_allow_ad_personalization_signals" android:value="true" />

Re-enable Analytics collection

After a user has given consent, enable Analytics collection again using the setAnalyticsCollectionEnabled() method.

The value set by the setAnalyticsCollectionEnabled method persists across app executions and overrides the value for FIREBASE_ANALYTICS_COLLECTION_ENABLED in your app's AndroidManifest.xml file. Once you set a value for setAnalyticsCollectionEnabled, Analytics collection remains in that state until setAnalyticsCollectionEnabled is called again, even if a user closes and re-opens your app.

To update Analytics collection:

setAnalyticsCollectionEnabled(true);

To update consent values after an app has launched, call the setConsent method.

The value set by the setConsent method overrides the default setting and persists across app executions. The value remains in that state until setConsent is called again, even if a user closes and reopens the app. setConsent only updates the parameters you specify.

If a user withdraws their previously given consent for Analytics or Ad storage, Google Analytics deletes all user properties, including consent for ad_personalization. To preserve the user's consent choice for ad personalization, restore the previous value for ad personalization using setConsent (Kotlin+KTX | Java) .

The following example shows the setConsent method updating the different consent values to granted:

Java

// Set consent types.
Map<ConsentType, ConsentStatus> consentMap = new EnumMap<>(ConsentType.class);
consentMap.put(ConsentType.ANALYTICS_STORAGE, ConsentStatus.GRANTED);
consentMap.put(ConsentType.AD_STORAGE, ConsentStatus.GRANTED);
consentMap.put(ConsentType.AD_USER_DATA, ConsentStatus.GRANTED);
consentMap.put(ConsentType.AD_PERSONALIZATION, ConsentStatus.GRANTED);

mFirebaseAnalytics.setConsent(consentMap);

Kotlin

Firebase.analytics.setConsent {
  analyticsStorage(ConsentStatus.GRANTED)
  adStorage(ConsentStatus.GRANTED)
  adUserData(ConsentStatus.GRANTED)
  adPersonalization(ConsentStatus.GRANTED)
}

If a user decides to revoke their consent, make sure you update the consent states accordingly and disable Analytics collection again using setAnalyticsCollectionEnabled().

As a part of Google's ongoing commitment to a privacy-safe digital advertising ecosystem, we are strengthening the enforcement of our EU user consent policy.

Learn more about Google's Updates to consent mode for traffic in European Economic Area (EEA).

Consent mode users need to send two new parameters in addition to ad storage and analytics storage:

  1. Update your app's AndroidManifest.xml to include:

    <meta-data android:name="google_analytics_default_allow_ad_user_data" android:value="true" />
    <meta-data android:name="google_analytics_default_allow_ad_personalization_signals" android:value="true" />
    ```
    
  2. Update your consent calls to include the parameters for ad user data and ad personalization:

    Java

    // Set consent types.
    Map<ConsentType, ConsentStatus> consentMap = new EnumMap<>(ConsentType.class);
    consentMap.put(ConsentType.ANALYTICS_STORAGE, ConsentStatus.GRANTED);
    consentMap.put(ConsentType.AD_STORAGE, ConsentStatus.GRANTED);
    consentMap.put(ConsentType.AD_USER_DATA, ConsentStatus.GRANTED);
    consentMap.put(ConsentType.AD_PERSONALIZATION, ConsentStatus.GRANTED);
    
    mFirebaseAnalytics.setConsent(consentMap);
    

    Kotlin

    Firebase.analytics.setConsent {
      analyticsStorage(ConsentStatus.GRANTED)
      adStorage(ConsentStatus.GRANTED)
      adUserData(ConsentStatus.GRANTED)
      adPersonalization(ConsentStatus.GRANTED)
    }
    

You can verify that your consent settings are working as intended by viewing the log messages for your app.

Follow these steps:

  1. Enable verbose logging on your device.
  2. In the Android Studio logcat, find the log message that starts with Setting consent. For example, Ad storage is enabled, you'll see the following log message:

    Setting consent, ... AD_STORAGE=granted