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
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
94
lib/core/analytics/posthog_service.dart
Normal file
94
lib/core/analytics/posthog_service.dart
Normal file
@@ -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<void> 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<String, dynamic>? 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<String, dynamic>? 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<String, dynamic>? 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<String, dynamic> 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');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<ReviewSection> {
|
||||
|
||||
Future<void> _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
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ContributeScreen>
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
PostHogService.instance.screen('Contribute');
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<GamificationProvider>().loadAll();
|
||||
});
|
||||
|
||||
@@ -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<HomeScreen> with SingleTickerProviderStateM
|
||||
_heroPageNotifier = ValueNotifier(0);
|
||||
_loadUserDataAndEvents();
|
||||
_startAutoScroll();
|
||||
PostHogService.instance.screen('Home');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1382,6 +1384,11 @@ class _HomeScreenState extends State<HomeScreen> 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)));
|
||||
}
|
||||
|
||||
@@ -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<LearnMoreScreen> 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<LearnMoreScreen> 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);
|
||||
|
||||
@@ -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<ProfileScreen>
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
PostHogService.instance.screen('Profile');
|
||||
|
||||
// Animation controller for EXP bar + stat counters
|
||||
_animController = AnimationController(
|
||||
|
||||
Reference in New Issue
Block a user