Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Screen Tracking by Implementing NavigatorObserver-Based CustomerIORouteObserver #87

Open
AristideVB opened this issue Sep 24, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@AristideVB
Copy link

Problem

The current approach to screen/route tracking relies on manual listener attachment for each route change in our app. The existing strategy is non-idiomatic for Flutter and potentially less efficient:

_router.addListener(() => _onRouteChanged());

void _onRouteChanged() {
  if (_customerIOSDK.sdkConfig?.screenTrackingEnabled == true) {
    final Screen? screen = _router.location.toAppScreen();
    if (screen != null) {
      CustomerIO.screen(name: screen.name);
    }
  }
}

Proposed Solution

To solve this, I propose implementing CustomerIORouteObserver that leverages Flutter's NavigatorObserver. This approach aligns better with Flutter best practices and allows for more efficient and reliable route tracking. The GoRouter package supports route observers, allowing this new class to integrate seamlessly.

Integration with GoRouter

GoRouter provides a way to attach observers to your routes, enabling cleaner and more idiomatic tracking of screen views. You can easily add the CustomerIORouteObserver to the GoRouter setup like this:

GoRouter(
  observers: [CustomerIORouteObserver()],
  // ... other configurations
)

By adding the observer directly into GoRouter's observers array, we can automatically and efficiently handle route tracking without requiring any manual intervention for each route change.

Integration with MaterialApp

MaterialApp(
  home: MyAppHome(),
  navigatorObservers: [
    CustomerIORouteObserver(),
  ],
)

With this new setup, every time a route change occurs, the CustomerIORouteObserver will automatically fire and send the necessary tracking information to CustomerIO.

@WellDunDun
Copy link

Great suggestions and I really hope they implement them! I've been really struggling to get screen tracking working with GoRouter and I'm in desperate need of some help.

@Shahroz16 Shahroz16 added the enhancement New feature or request label Sep 28, 2023
@aqrc
Copy link

aqrc commented Oct 1, 2023

There is such code for observer in official documentation which can be copy-pasted https://customer.io/docs/sdk/flutter/track-events/#auto-screenview

I agree though that it would be nice to have it out of the box)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants