androidflutterfirebasefirebase-analytics

How to ask for android user permission for Analytics on Flutter


I'm trying to add Firebase Analytics to my Flutter app. To my best understanding, I need to ask the user for permission... on both platforms.

For iOS, there's a package, app_tracking_transparency, with which I can easily call the system dialog to ask for permission.

await AppTrackingTransparency.requestTrackingAuthorization();

How do I do that with Android? Is there a package that can be used? are there any system settings that show if the user gave consent or not? Or does Android not care about that at all?

Super confused... as I can't seem to find any examples for asking permission on Android.


Solution

  • In Android is not necessary that you ask permission to collect analytics data, yet you can do something like this:

    import 'dart:io' show Platform;
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_analytics/firebase_analytics.dart';
    import 'package:flutter/material.dart';
    import 'package:app_tracking_transparency/app_tracking_transparency.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      FirebaseAnalytics analytics = FirebaseAnalytics();
      
      Future<void> requestPermission() async {
        if (Platform.isIOS) {
          await AppTrackingTransparency.requestTrackingAuthorization();
        }
      }
    
      Future<void> enableAnalytics(bool enabled) async {
        analytics.setAnalyticsCollectionEnabled(enabled);
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Firebase Analytics Example'),
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  FlatButton(
                    child: Text('Request Permission'),
                    onPressed: requestPermission,
                  ),
                  FlatButton(
                    child: Text('Disable Analytics'),
                    onPressed: () => enableAnalytics(false),
                  ),
                  FlatButton(
                    child: Text('Enable Analytics'),
                    onPressed: () => enableAnalytics(true),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    In this code, requestPermission is a function that asks for permission for application tracking on iOS. It does nothing on Android, as explicit permission is not required there.

    The functions enableAnalytics and disableAnalytics turn the collection of data for Firebase Analytics on and off, respectively. These could be connected to a setting in your app to allow users to enable or disable Analytics data collection.

    Of course, this is just a simplified example. In a real application, you would probably want to have a more detailed management of user permissions and settings.