add firebase, add screen flow
This commit is contained in:
@@ -7,6 +7,7 @@ library;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:worker/core/services/analytics_service.dart';
|
||||
|
||||
import 'package:worker/features/account/domain/entities/address.dart';
|
||||
import 'package:worker/features/account/presentation/pages/address_form_page.dart';
|
||||
@@ -64,7 +65,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
return GoRouter(
|
||||
// Initial route - start with splash screen
|
||||
initialLocation: RouteNames.splash,
|
||||
|
||||
observers: [AnalyticsService.observer],
|
||||
// Redirect based on auth state
|
||||
redirect: (context, state) {
|
||||
final isLoading = authState.isLoading;
|
||||
@@ -131,16 +132,22 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
GoRoute(
|
||||
path: RouteNames.splash,
|
||||
name: RouteNames.splash,
|
||||
pageBuilder: (context, state) =>
|
||||
MaterialPage(key: state.pageKey, child: const SplashPage()),
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: RouteNames.splash,
|
||||
child: const SplashPage(),
|
||||
),
|
||||
),
|
||||
|
||||
// Authentication Routes
|
||||
GoRoute(
|
||||
path: RouteNames.login,
|
||||
name: RouteNames.login,
|
||||
pageBuilder: (context, state) =>
|
||||
MaterialPage(key: state.pageKey, child: const LoginPage()),
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: RouteNames.login,
|
||||
child: const LoginPage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: RouteNames.forgotPassword,
|
||||
@@ -192,16 +199,22 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
GoRoute(
|
||||
path: RouteNames.home,
|
||||
name: RouteNames.home,
|
||||
pageBuilder: (context, state) =>
|
||||
MaterialPage(key: state.pageKey, child: const MainScaffold()),
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'home',
|
||||
child: const MainScaffold(),
|
||||
),
|
||||
),
|
||||
|
||||
// Products Route (full screen, no bottom nav)
|
||||
GoRoute(
|
||||
path: RouteNames.products,
|
||||
name: RouteNames.products,
|
||||
pageBuilder: (context, state) =>
|
||||
MaterialPage(key: state.pageKey, child: const ProductsPage()),
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'products',
|
||||
child: const ProductsPage(),
|
||||
),
|
||||
),
|
||||
|
||||
// Product Detail Route
|
||||
@@ -212,6 +225,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
final productId = state.pathParameters['id'];
|
||||
return MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'product_detail',
|
||||
child: ProductDetailPage(productId: productId ?? ''),
|
||||
);
|
||||
},
|
||||
@@ -224,6 +238,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
final productId = state.pathParameters['id'];
|
||||
return MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'write_review',
|
||||
child: WriteReviewPage(productId: productId ?? ''),
|
||||
);
|
||||
},
|
||||
@@ -239,6 +254,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
final promotionId = state.pathParameters['id'];
|
||||
return MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'promotion_detail',
|
||||
child: PromotionDetailPage(promotionId: promotionId),
|
||||
);
|
||||
},
|
||||
@@ -248,8 +264,11 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
GoRoute(
|
||||
path: RouteNames.cart,
|
||||
name: RouteNames.cart,
|
||||
pageBuilder: (context, state) =>
|
||||
MaterialPage(key: state.pageKey, child: const CartPage()),
|
||||
pageBuilder: (context, state) => MaterialPage(
|
||||
key: state.pageKey,
|
||||
name: 'cart',
|
||||
child: const CartPage(),
|
||||
),
|
||||
),
|
||||
|
||||
// Checkout Route
|
||||
|
||||
88
lib/core/services/analytics_service.dart
Normal file
88
lib/core/services/analytics_service.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'package:firebase_analytics/firebase_analytics.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Firebase Analytics service for tracking user events across the app.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
/// // Log add to cart event
|
||||
/// AnalyticsService.logAddToCart(
|
||||
/// productId: 'SKU123',
|
||||
/// productName: 'Gạch men 60x60',
|
||||
/// price: 150000,
|
||||
/// quantity: 2,
|
||||
/// );
|
||||
/// ```
|
||||
class AnalyticsService {
|
||||
AnalyticsService._();
|
||||
|
||||
static final FirebaseAnalytics _analytics = FirebaseAnalytics.instance;
|
||||
|
||||
/// Get the analytics instance for NavigatorObserver
|
||||
static FirebaseAnalytics get instance => _analytics;
|
||||
|
||||
/// Get the observer for automatic screen tracking in GoRouter
|
||||
static FirebaseAnalyticsObserver get observer => FirebaseAnalyticsObserver(
|
||||
analytics: _analytics,
|
||||
nameExtractor: (settings) {
|
||||
// GoRouter uses the path as the route name
|
||||
final name = settings.name;
|
||||
if (name != null && name.isNotEmpty && name != '/') {
|
||||
return name;
|
||||
}
|
||||
return settings.name ?? '/';
|
||||
},
|
||||
routeFilter: (route) => route is PageRoute,
|
||||
);
|
||||
|
||||
/// Log screen view manually
|
||||
static Future<void> logScreenView({
|
||||
required String screenName,
|
||||
String? screenClass,
|
||||
}) async {
|
||||
try {
|
||||
await _analytics.logScreenView(
|
||||
screenName: screenName,
|
||||
screenClass: screenClass,
|
||||
);
|
||||
debugPrint('📊 Analytics: screen_view - $screenName');
|
||||
} catch (e) {
|
||||
debugPrint('📊 Analytics error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Log add to cart event
|
||||
///
|
||||
/// [productId] - Product SKU or ID
|
||||
/// [productName] - Product display name
|
||||
/// [price] - Unit price in VND
|
||||
/// [quantity] - Quantity added
|
||||
/// [category] - Optional product category
|
||||
static Future<void> logAddToCart({
|
||||
required String productId,
|
||||
required String productName,
|
||||
required double price,
|
||||
required int quantity,
|
||||
String? brand,
|
||||
}) async {
|
||||
try {
|
||||
await _analytics.logAddToCart(
|
||||
currency: 'VND',
|
||||
value: price * quantity,
|
||||
items: [
|
||||
AnalyticsEventItem(
|
||||
itemId: productId,
|
||||
itemName: productName,
|
||||
price: price,
|
||||
quantity: quantity,
|
||||
itemBrand: brand,
|
||||
),
|
||||
],
|
||||
);
|
||||
debugPrint('📊 Analytics: add_to_cart - $productName x$quantity');
|
||||
} catch (e) {
|
||||
debugPrint('📊 Analytics error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user