Android Allow User to Disable Firebase Analytics (GDPR)

September 28, 2018

If you have yet to setup Firebase Analytics, refer to How To Add Firebase Analytics To Android.

If are not familiar with Android settings UI using PreferenceFragment, refer to Android Settings Preference Using PreferenceFragmentCompat.

To allow runtime/user enable/disable Firebase Analytics, you must disable Analytics through AndroidManifest.xml.

You might want to Disable Advertising ID collection (Android Advertising ID and Advertising Identifier for iOS).

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

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

    </application>
</manifest>

During application startup, check SharedPreference value to enable Crashlytics if required.

class LuaApp : Application() {
    override fun onCreate() {
        super.onCreate()

        val context = this
        val settingsSharedPref = PreferenceManager.getDefaultSharedPreferences(context)

        val isEnableAnalytics = settingsSharedPref.getBoolean(context.getString(R.string.pref_key_enable_analytics), true)
        
        FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(isEnableAnalytics)
        Log.d(TAG, "isEnableAnalytics=$isEnableAnalytics")
    }
}

Edit your PreferenceFragment settings file (e.g. res/xml/pref_settings.xml) to add SwitchPreference for Crashlytics.

<PreferenceScreen ...>
    <PreferenceCategory
        android:title="Privacy"
        android:key="pref_key_privacy_settings">

        <SwitchPreference
            android:key="@string/pref_key_enable_analytics"
            android:title="Enable Basic Analytics"
            android:defaultValue="true"
            />

    </PreferenceCategory>  
</PreferenceScreen>

Edit your PreferenceFragment in Activity.

class SettingsFragment: PreferenceFragmentCompat() {
    override fun onPreferenceTreeClick(preference: Preference): Boolean {
        return when (preference.key) {
            getString(R.string.pref_key_enable_analytics) -> {
                val isEnableAnalytics = (preference as SwitchPreference).isChecked
                
                FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(isEnableAnalytics)
                Log.d(TAG, "isEnableAnalytics=$isEnableAnalytics")

                true
            }
            else -> {
                super.onPreferenceTreeClick(preference)
            }    
        }
    }
}

NOTE: Refer to General Data Protection Regulation (GDPR) compliance options

To configure data retention period, goto your Firebase project, click Dashboard, click Settings, under User Data Retention:

  • Turn on the switch to retain user data for 14 months.
  • Turn off the switch, and retain user data according to Analytics default data-retention policy.

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.