https://github.com/sayedmoataz/firebase_analytics_services
https://github.com/sayedmoataz/firebase_analytics_services
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sayedmoataz/firebase_analytics_services
- Owner: sayedmoataz
- Created: 2025-07-12T15:39:48.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-12T16:33:23.000Z (11 months ago)
- Last Synced: 2025-07-12T17:52:24.113Z (11 months ago)
- Language: Dart
- Size: 9.77 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Firebase Analytics Module for Flutter
A clean and extensible Firebase Analytics module built for Flutter apps using dependency injection and event abstraction. This package helps you track user behavior, screen views, and custom events in a scalable and testable way.
---
## ✨ Features
- ✅ Clean architecture (models, services, helpers, extensions)
- ✅ Easy-to-use event extensions
- ✅ No analytics logic in the UI
- ✅ Automatic user context injection (ID, email, role…)
- ✅ Custom and screen view event abstraction
- ✅ Dependency Injection ready (via `getIt`)
- ✅ Error-safe logging using Crashlytics
- ✅ Lightweight and easily customizable
- ✅ Type-safe parameter handling
- ✅ Easy to extend for any custom use case
- ℹ️ Note: Crashlytics and GetIt are optional. You can replace them with any other error logger or DI framework.
---
## 📦 Installation
In your `pubspec.yaml`:
```yaml
dependencies:
firebase_core: ^3.13.0
firebase_analytics: ^11.5.0
````
---
## 🔧 Setup
### 1. Initialize Firebase
```dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
setupDependencies(); // Register your dependencies (or use your own DI method)
runApp(MyApp());
}
```
---
### 2. Dependency Injection (GetIt)
```dart
final getIt = GetIt.instance;
void setupDependencies() {
final sharedPreferences = await SharedPreferences.getInstance();
getIt.registerSingleton(sharedPreferences);
getIt.registerSingleton(PrefManager(sharedPreferences));
getIt.registerSingleton(FirebaseCrashlytics.instance);
getIt.registerLazySingleton(AnalyticsHelper.new);
getIt.registerLazySingleton(FirebaseAnalyticsService.new);
}
```
---
### 3. Use Navigator Observer
```dart
MaterialApp(
navigatorObservers: [
NavigationService.routeObserver,
(getIt() as FirebaseAnalyticsService).observer,
],
home: MyHomePage(),
);
```
---
## 🚀 Usage Examples
### Log a login event:
```dart
await getIt().logLogin(method: 'social_login');
```
---
### Log a custom event:
```dart
await getIt().logUpdateProfile(parameters: {
'field_changed': 'username',
});
```
---
### Log a screen view manually:
```dart
await getIt().radarAROpenView(
screenClass: 'RadarScreen',
parameters: {'source': 'dashboard'},
);
```
---
### Set user context manually:
```dart
await getIt().setUserContext(UserContext(
userId: 'user_123',
userName: 'Sayed Moataz',
userEmail: 'sayedmoataz9@gmail.com',
userPhone: '+201147880178',
userRole: 'admin',
));
```
## 📁 Project Structure
```
lib/
├── services
│ └── analytics/
│ ├── analytics_helper.dart
│ ├── analytics_services.dart
│ └── analytics_extensions.dart
├── models/
│ └── user_context.dart
├── di.dart
└── main.dart
```
---
## 🛠️ Extend the Module
You can add more events in the `AnalyticsServiceExtensions`:
```dart
extension AnalyticsServiceExtensions on IAnalyticsService {
Future logPurchase({required double amount}) async {
final helper = getIt();
await logEvent(
helper.createCustomEvent(
eventName: 'purchase_made',
parameters: {'amount': amount},
),
);
}
}
```
---
## 🧩 Customization Notes
This module is designed to be flexible and easy to adapt to your project:
- **Crashlytics Logging**
The `CrashlyticsLogger.logMessage(...)` calls are project-specific.
You can:
- Replace them with your own logger
- Remove them entirely
- **Dependency Injection**
Although `getIt` is used here for DI, you're free to:
- Use any other DI solution (like Riverpod, Provider, injectable, etc.)
- Inject dependencies manually based on your app architecture
These components are decoupled from the core logic to allow maximum flexibility.