Overview
leancode_analytics_base provides the foundation for analytics tracking in Flutter applications. It defines a unified interface that allows you to track events and screen views across different analytics providers without coupling your code to a specific implementation.
This is a base package that provides abstractions only. Use implementation packages like
leancode_analytics_firebase or leancode_analytics_posthog for actual analytics functionality.Installation
Add the package to yourpubspec.yaml:
Architecture
The analytics system follows a plugin-based architecture with clear separation between interface and implementation:Core Components
LeanAnalytics Interface
The main interface that all analytics backends must implement:lib/src/analytics.dart
Why use an interface?
Why use an interface?
The interface-based approach allows you to:
- Switch analytics providers without changing application code
- Use multiple analytics backends simultaneously
- Mock analytics in tests easily
- Keep your business logic decoupled from analytics implementation details
AnalyticsEvent Class
Base class for all analytics events:lib/src/event.dart
Built-in Event Types
The package includes commonly used event types:TapAnalyticsEvent
Predefined event for tracking user taps and interactions:lib/src/event.dart
LoginAnalyticsEvent
Predefined event for tracking user authentication:lib/src/event.dart
LeanAnalyticsRoute Interface
Interface for routes that should be automatically tracked:lib/src/route.dart
Usage Examples
Creating Custom Events
ExtendAnalyticsEvent to create domain-specific events:
Implementing Route Tracking
Make your routes trackable by implementingLeanAnalyticsRoute:
When using
NavigatorObservers from the analytics implementation, screen views will be tracked automatically for all routes implementing LeanAnalyticsRoute.Using with Dependency Injection
Implementation Packages
Choose an implementation package based on your analytics provider:Firebase Analytics
Google’s analytics solution with deep integration into Firebase ecosystem
PostHog Analytics
Open-source product analytics platform with session replay and feature flags
API Reference
LeanAnalytics
Registers an analytics event to be tracked by the backend
List of navigator observers for automatic screen tracking
AnalyticsEvent
The name of the event used for tracking
Additional parameters associated with this event
LeanAnalyticsRoute
Unique identifier for this screen/route used in analytics
Optional parameters to include with screen view events
Package Information
- Version: 0.1.0
- Minimum Dart SDK: 3.5.0
- Minimum Flutter: 3.24.0
- Repository: flutter_corelibrary
