Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase Analytics: Track page changes with onGenerateRoute

I have firebase analytics enabled for my application and I am trying to get the firebase debug-view to show my page changes. With the code below, currently nothing is being tracked, and I don't understand why. I have followed the firebase documentation for tracking page-views, but apparently I'm missing something when it comes to using onGenerateRoute.

My main:

class MyApp extends StatelessWidget {
  static FirebaseAnalytics analytics = FirebaseAnalytics();
  static FirebaseAnalyticsObserver observer =
      FirebaseAnalyticsObserver(analytics: analytics);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "localhour",
        onGenerateRoute: router.generateRoute,
        initialRoute: '/',
        navigatorObservers: <NavigatorObserver>[observer],
      ),
    );
  }
}

My generateRoute function:

Route<dynamic> generateRoute(RouteSettings settings) {
  switch (settings.name) {
    case '/':
      return MaterialPageRoute(builder: (context) => RootPage());
      break;
    case '/login':
      return MaterialPageRoute(builder: (context) => LoginPage());
      break;
    case '/home':
      var user = settings.arguments;
      return MaterialPageRoute(
          builder: (context) => HomePage(
                user: user,
              ));
      break;
    case '/store':
      return MaterialPageRoute(builder: (context) => StorePage());
      break;
    case '/store-details':
      var argument = settings.arguments;
      return MaterialPageRoute(
          builder: (context) => StoreDetails(
                storeDetails: argument,
              ));
      break;
  }
}

I have used analytics before to track page changes automatically in a previous app but used the "routes" method in my main function. Can someone explain why I am not getting any feedback using this method above? Do the screens have to be named explicitly? (Which I have also tried, but unsuccessfully)

like image 800
Michael Tolsma Avatar asked Apr 22 '20 13:04

Michael Tolsma


1 Answers

Hey if you're still looking for an answer to this, it's because your not pushing the name into the MaterialPageRoute.

So in your generateRoute function:

  '/login':
      return MaterialPageRoute(
         builder: (context) => LoginPage(),
         settings: RouteSettings(name: settings.name));
      break;

That'll push the name to Firebase Analytics for you. Just add it for each case in the switch.

like image 58
Tyrell James Avatar answered Nov 15 '22 07:11

Tyrell James