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:
- https://firebase.google.com/policies/analytics/
- https://support.google.com/firebase/answer/9019185
- https://support.google.com/firebase/answer/7663853
- How To Add Firebase Analytics To Android
- Android Settings Preference Using PreferenceFragmentCompat
- https://medium.com/schibsted-tech-polska/from-fabric-to-firebase-on-the-road-with-gdpr-b33342bf344b