feat(client/analytics): GA4 funnel instrumentation + unified home CTA
Add Firebase Analytics (GA4) funnel tracking to client_app: - AnalyticsService typed wrapper (enum-gated, no PII) + analyticsProvider - FirebaseAnalyticsObserver on GoRouter (screen_name via nameExtractor) - user_id = customer UUID, user_type property, set on auth resolve/upgrade - funnel events: curhat_start, curhat_repeat_start, auth_*, onboarding_usp_view, payment_view, payment_method_select, payment_started, pairing_matched/no_bestie - bottom-sheet events: verif_choice_view/select, bestie_choice_view/select, extension_offer_view, chat_extension_requested - payment_started carries app_instance_id + ga_session_id in the /payment-requests body for future server-side stitching (backend ignores) - curhat_mode_pick screen name disambiguates the chat/call mode picker (/payment/method-pick) from the payment-channel picker (/payment/method) - unify both home CTAs to "Aku Mau Curhat" Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_analytics/firebase_analytics.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'core/analytics/analytics_service.dart';
|
||||
import 'core/api/api_client_provider.dart';
|
||||
import 'core/auth/auth_notifier.dart';
|
||||
import 'core/auth/auth_providers_provider.dart';
|
||||
@@ -28,6 +30,12 @@ void main() async {
|
||||
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
|
||||
// Enable GA4 collection. Fire-and-forget so it never adds to cold-start
|
||||
// latency; the SDK queues events until collection is on.
|
||||
unawaited(
|
||||
FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true),
|
||||
);
|
||||
|
||||
final messaging = FirebaseMessaging.instance;
|
||||
await messaging.requestPermission();
|
||||
|
||||
@@ -131,9 +139,41 @@ class _AppState extends ConsumerState<App> with WidgetsBindingObserver {
|
||||
});
|
||||
}
|
||||
|
||||
// Tracks the last user_id pushed to GA4 so we don't re-issue identical
|
||||
// setUserId/user-property calls on every transient auth emission.
|
||||
String? _analyticsUserId;
|
||||
|
||||
/// Mirror auth state into GA4 identity (§4): opaque customer UUID as
|
||||
/// `user_id` + `user_type` property. Re-set on identity upgrade
|
||||
/// (anon→verified) so the same user continues. Never sets phone/name.
|
||||
void _syncAnalyticsIdentity(AuthData? data) {
|
||||
final analytics = ref.read(analyticsProvider);
|
||||
final (String? customerId, AnalyticsUserType? userType) = switch (data) {
|
||||
AuthAnonymousData d => (d.customerId, AnalyticsUserType.anonymous),
|
||||
AuthForceRegisterData d => (d.customerId, AnalyticsUserType.anonymous),
|
||||
AuthAuthenticatedData d => (
|
||||
d.profile['id'] as String?,
|
||||
AnalyticsUserType.verified,
|
||||
),
|
||||
AuthNeedsDisplayNameData d => (
|
||||
d.profile['id'] as String?,
|
||||
AnalyticsUserType.verified,
|
||||
),
|
||||
_ => (null, null),
|
||||
};
|
||||
if (customerId == _analyticsUserId) return;
|
||||
_analyticsUserId = customerId;
|
||||
// ignore: discarded_futures
|
||||
analytics.setUserId(customerId);
|
||||
if (userType != null) {
|
||||
// ignore: discarded_futures
|
||||
analytics.setUserType(userType);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// FCM registration on auth.
|
||||
// FCM registration + analytics identity on auth.
|
||||
ref.listen(authProvider, (prev, next) {
|
||||
final data = next.valueOrNull;
|
||||
if (data is AuthAuthenticatedData || data is AuthAnonymousData) {
|
||||
@@ -142,6 +182,7 @@ class _AppState extends ConsumerState<App> with WidgetsBindingObserver {
|
||||
// Logged out (or initial) — ensure the chat WS is closed.
|
||||
ref.read(chatProvider.notifier).disconnect();
|
||||
}
|
||||
_syncAnalyticsIdentity(data);
|
||||
});
|
||||
|
||||
// Global chat WebSocket lifecycle: connect whenever the user has an
|
||||
|
||||
Reference in New Issue
Block a user