skip to Main Content

I am trying to connect and log events log using firebase analytics but there is no progress, I already have intergrated firebase google signin methods but not able to do so with firebase analytics.
Here are all the chnages i have done till now
added

implementation 'com.google.firebase:firebase-analytics' 

in app level build.gradle in dependencies
also have firebase and firebase_analytics package install, i am still unable to log events

2

Answers


  1. in android/app/build.gradle add

    apply plugin: 'com.google.gms.google-services'
    
    //Firebase
    implementation platform('com.google.firebase:firebase-bom:31.2.2')
    implementation 'com.google.firebase:firebase-analytics'```
    

    in android/build.gradle add

    classpath 'com.google.gms:google-services:4.3.15'
    

    in pubspec.yaml add

    firebase_core: ^2.7.0
    firebase_analytics: ^10.1.4
    

    here sample demo

    import 'dart:io';
    import 'package:firebase_analytics/firebase_analytics.dart';
    import 'package:flutter/material.dart';
    
    class MyRouteObserver extends RouteObserver<PageRoute<dynamic>> {
      @override
      void didPush(Route route, Route? previousRoute) async {
        if(route.settings.name.toString().toLowerCase().replaceAll("/", "").isNotEmpty){
          await FirebaseAnalytics.instance.logEvent(
            name: Platform.isIOS
                ? "ios_${route.settings.name.toString().toLowerCase().replaceAll("/", "")}"
                : route.settings.name.toString().toLowerCase().replaceAll("/", ""),
            parameters: {},
          );
        }
    
        super.didPush(route, previousRoute);
      }
    }
    
    

    in lib/main.dart add in this line inside MaterialApp

    navigatorObservers: [MyRouteObserver()],
    

    Don’t forget to initialize Firebase

    await Firebase.initializeApp();
    
    Login or Signup to reply.
  2. in android/app/build.gradle add

    id "com.google.gms.google-services"
    

    in android/build.gradle add

    classpath 'com.google.gms:google-services:4.3.15'
    

    in main.dart

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      try {
        await Firebase.initializeApp();
        FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true);
      } catch (e) {
        print("Failed to initialize Firebase: $e");
      }
    
      runApp(const MyApp());
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search