From 42b71beae2eff0b5d737a7b362972a9e4ef6d3b3 Mon Sep 17 00:00:00 2001 From: Sicherhaven Date: Sat, 4 Apr 2026 18:45:19 +0530 Subject: [PATCH] feat: PostHog analytics wiring across all key screens - Commit untracked posthog_service.dart (fire-and-forget HTTP client, EU data residency, already used by auth for identify/reset) - screen() calls: Home, Contribute, Profile, EventDetail (with event_id) - capture('event_tapped') on hero carousel card tap (source: hero_carousel) - capture('book_now_tapped') in _navigateToCheckout (event_id + name) - capture('review_submitted') in _handleSubmit (event_id + rating) - Covers all 4 expansion items from security audit finding 8.2 --- lib/core/analytics/posthog_service.dart | 94 +++++++++++++++++++ .../reviews/widgets/review_section.dart | 5 + lib/screens/contribute_screen.dart | 2 + lib/screens/home_screen.dart | 7 ++ lib/screens/learn_more_screen.dart | 6 ++ lib/screens/profile_screen.dart | 2 + 6 files changed, 116 insertions(+) create mode 100644 lib/core/analytics/posthog_service.dart diff --git a/lib/core/analytics/posthog_service.dart b/lib/core/analytics/posthog_service.dart new file mode 100644 index 0000000..cbc4515 --- /dev/null +++ b/lib/core/analytics/posthog_service.dart @@ -0,0 +1,94 @@ +// lib/core/analytics/posthog_service.dart +import 'dart:convert'; +import 'dart:io' show Platform; +import 'package:flutter/foundation.dart'; +import 'package:http/http.dart' as http; +import 'package:shared_preferences/shared_preferences.dart'; + +/// Lightweight PostHog analytics client using the HTTP API. +/// Works with Dart 2.x (no posthog_flutter SDK needed). +class PostHogService { + static const String _apiKey = 'phc_xXxn0COAwWRj3AU7fspsTuesCIK0aBGXb3zaIIJRgZA'; + static const String _host = 'https://eu.i.posthog.com'; + static const String _distinctIdKey = 'posthog_distinct_id'; + + static PostHogService? _instance; + String? _distinctId; + + PostHogService._(); + + static PostHogService get instance { + _instance ??= PostHogService._(); + return _instance!; + } + + /// Initialize and load or generate a distinct ID. + Future init() async { + final prefs = await SharedPreferences.getInstance(); + _distinctId = prefs.getString(_distinctIdKey); + if (_distinctId == null) { + _distinctId = DateTime.now().millisecondsSinceEpoch.toRadixString(36) + + UniqueKey().toString().hashCode.toRadixString(36); + await prefs.setString(_distinctIdKey, _distinctId!); + } + } + + /// Identify a user (call after login). + void identify(String userId, {Map? properties}) { + _distinctId = userId; + SharedPreferences.getInstance().then((prefs) { + prefs.setString(_distinctIdKey, userId); + }); + _send('identify', { + 'distinct_id': userId, + if (properties != null) '\$set': properties, + }); + } + + /// Capture a custom event. + void capture(String event, {Map? properties}) { + _send('capture', { + 'event': event, + 'distinct_id': _distinctId ?? 'anonymous', + 'properties': { + ...?properties, + '\$lib': 'flutter', + '\$lib_version': '1.0.0', + }, + }); + } + + /// Capture a screen view. + void screen(String screenName, {Map? properties}) { + capture('\$screen', properties: { + '\$screen_name': screenName, + ...?properties, + }); + } + + /// Reset identity (call on logout). + void reset() { + _distinctId = null; + SharedPreferences.getInstance().then((prefs) { + prefs.remove(_distinctIdKey); + }); + } + + /// Send event to PostHog API (fire-and-forget). + void _send(String endpoint, Map body) { + final payload = { + 'api_key': _apiKey, + 'timestamp': DateTime.now().toUtc().toIso8601String(), + ...body, + }; + + // Fire and forget — don't block the UI + http.post( + Uri.parse('$_host/$endpoint/'), + headers: {'Content-Type': 'application/json'}, + body: jsonEncode(payload), + ).catchError((e) { + if (kDebugMode) debugPrint('PostHog error: $e'); + }); + } +} diff --git a/lib/features/reviews/widgets/review_section.dart b/lib/features/reviews/widgets/review_section.dart index 1e92e10..76d5014 100644 --- a/lib/features/reviews/widgets/review_section.dart +++ b/lib/features/reviews/widgets/review_section.dart @@ -6,6 +6,7 @@ import '../../../widgets/bouncing_loader.dart'; import '../../../core/utils/error_utils.dart'; import '../models/review_models.dart'; import '../services/review_service.dart'; +import '../../../core/analytics/posthog_service.dart'; import 'review_summary.dart'; import 'review_form.dart'; import 'review_card.dart'; @@ -82,6 +83,10 @@ class _ReviewSectionState extends State { Future _handleSubmit(int rating, String? comment) async { await _service.submitReview(widget.eventId, rating, comment); + PostHogService.instance.capture('review_submitted', properties: { + 'event_id': widget.eventId, + 'rating': rating, + }); await _loadReviews(); // Refresh to get updated stats + review list } diff --git a/lib/screens/contribute_screen.dart b/lib/screens/contribute_screen.dart index b955245..3391d31 100644 --- a/lib/screens/contribute_screen.dart +++ b/lib/screens/contribute_screen.dart @@ -21,6 +21,7 @@ import '../widgets/landscape_section_header.dart'; import '../widgets/tier_avatar_ring.dart'; import '../features/share/share_rank_card.dart'; import 'contributor_profile_screen.dart'; +import '../core/analytics/posthog_service.dart'; // ───────────────────────────────────────────────────────────────────────────── // Tier colour map @@ -102,6 +103,7 @@ class _ContributeScreenState extends State @override void initState() { super.initState(); + PostHogService.instance.screen('Contribute'); WidgetsBinding.instance.addPostFrameCallback((_) { context.read().loadAll(); }); diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 75d2d27..d9327b4 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -24,6 +24,7 @@ import '../features/gamification/providers/gamification_provider.dart'; import '../features/notifications/widgets/notification_bell.dart'; import '../features/notifications/providers/notification_provider.dart'; import '../widgets/skeleton_loader.dart'; +import '../core/analytics/posthog_service.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({Key? key}) : super(key: key); @@ -62,6 +63,7 @@ class _HomeScreenState extends State with SingleTickerProviderStateM _heroPageNotifier = ValueNotifier(0); _loadUserDataAndEvents(); _startAutoScroll(); + PostHogService.instance.screen('Home'); } @override @@ -1382,6 +1384,11 @@ class _HomeScreenState extends State with SingleTickerProviderStateM return GestureDetector( onTap: () { if (event.id != null) { + PostHogService.instance.capture('event_tapped', properties: { + 'event_id': event.id, + 'title': event.title ?? event.name ?? '', + 'source': 'hero_carousel', + }); Navigator.push(context, MaterialPageRoute(builder: (_) => LearnMoreScreen(eventId: event.id, initialEvent: event))); } diff --git a/lib/screens/learn_more_screen.dart b/lib/screens/learn_more_screen.dart index a213ba7..8b9c32c 100644 --- a/lib/screens/learn_more_screen.dart +++ b/lib/screens/learn_more_screen.dart @@ -18,6 +18,7 @@ import '../features/reviews/widgets/review_section.dart'; import '../widgets/tier_avatar_ring.dart'; import 'contributor_profile_screen.dart'; import 'checkout_screen.dart'; +import '../core/analytics/posthog_service.dart'; class LearnMoreScreen extends StatefulWidget { final int eventId; @@ -37,6 +38,10 @@ class _LearnMoreScreenState extends State with SingleTickerProv void _navigateToCheckout() { if (!AuthGuard.requireLogin(context, reason: 'Sign in to book this event.')) return; if (_event == null) return; + PostHogService.instance.capture('book_now_tapped', properties: { + 'event_id': _event!.id, + 'event_name': _event!.name ?? _event!.title ?? '', + }); Navigator.of(context).push(MaterialPageRoute( builder: (_) => CheckoutScreen( eventId: _event!.id, @@ -71,6 +76,7 @@ class _LearnMoreScreenState extends State with SingleTickerProv @override void initState() { super.initState(); + PostHogService.instance.screen('EventDetail', properties: {'event_id': widget.eventId}); _fadeController = AnimationController(vsync: this, duration: const Duration(milliseconds: 350)); _fade = CurvedAnimation(parent: _fadeController, curve: Curves.easeIn); _pageNotifier = ValueNotifier(0); diff --git a/lib/screens/profile_screen.dart b/lib/screens/profile_screen.dart index 1be9b7f..1138fdf 100644 --- a/lib/screens/profile_screen.dart +++ b/lib/screens/profile_screen.dart @@ -24,6 +24,7 @@ import '../core/app_decoration.dart'; import '../core/constants.dart'; import '../widgets/landscape_section_header.dart'; import '../features/share/share_rank_card.dart'; +import '../core/analytics/posthog_service.dart'; class ProfileScreen extends StatefulWidget { const ProfileScreen({Key? key}) : super(key: key); @@ -99,6 +100,7 @@ class _ProfileScreenState extends State @override void initState() { super.initState(); + PostHogService.instance.screen('Profile'); // Animation controller for EXP bar + stat counters _animController = AnimationController(