From 2c2e357d738d2adffc0da3ede80a33a5cc9df486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Mata?= Date: Sun, 24 May 2026 22:39:41 +0200 Subject: [PATCH] Document PostHog dart-define configuration --- mobile/README.md | 14 ++++++++++ mobile/lib/main.dart | 2 ++ mobile/lib/services/posthog_service.dart | 33 ++++++++++++++++++++++++ mobile/pubspec.yaml | 1 + 4 files changed, 50 insertions(+) create mode 100644 mobile/lib/services/posthog_service.dart diff --git a/mobile/README.md b/mobile/README.md index b7a843467..8730361ff 100644 --- a/mobile/README.md +++ b/mobile/README.md @@ -67,6 +67,20 @@ static String _baseUrl = 'http://localhost:3000'; static String _baseUrl = 'https://your-sure-server.com'; ``` +### 6. (Optional) Configure PostHog Session Recording + +PostHog is configured with **compile-time** Dart defines (not `.env`): + +```bash +flutter run \ + --dart-define=POSTHOG_API_KEY=phc_your_project_key \ + --dart-define=POSTHOG_HOST=https://us.i.posthog.com +``` + +- `POSTHOG_API_KEY` is required to enable PostHog. +- `POSTHOG_HOST` is optional and defaults to `https://us.i.posthog.com`. +- If `POSTHOG_API_KEY` is not passed, the app logs that PostHog is disabled and continues normally. + ### 5. Run the App ```bash diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index bc1b4afc6..605c97bf3 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -17,10 +17,12 @@ import 'services/api_config.dart'; import 'services/connectivity_service.dart'; import 'services/log_service.dart'; import 'services/preferences_service.dart'; +import 'services/posthog_service.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await ApiConfig.initialize(); + await PostHogService.initialize(); // Add initial log entry LogService.instance.info('App', 'Sure app starting...'); diff --git a/mobile/lib/services/posthog_service.dart b/mobile/lib/services/posthog_service.dart new file mode 100644 index 000000000..f309851d0 --- /dev/null +++ b/mobile/lib/services/posthog_service.dart @@ -0,0 +1,33 @@ +import 'package:posthog_flutter/posthog_flutter.dart'; + +import 'log_service.dart'; + +class PostHogService { + PostHogService._(); + + static const String _apiKey = String.fromEnvironment('POSTHOG_API_KEY'); + static const String _host = String.fromEnvironment( + 'POSTHOG_HOST', + defaultValue: 'https://us.i.posthog.com', + ); + + static Future initialize() async { + if (_apiKey.isEmpty) { + LogService.instance.info( + 'PostHog', + 'PostHog disabled: POSTHOG_API_KEY is not configured.', + ); + return; + } + + final config = PostHogConfig(_apiKey, host: _host); + config.sessionReplay = true; + + await Posthog().setup(config); + + LogService.instance.info( + 'PostHog', + 'PostHog initialized with session replay enabled.', + ); + } +} diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yaml index efcb72b9c..67cbbdc24 100644 --- a/mobile/pubspec.yaml +++ b/mobile/pubspec.yaml @@ -26,6 +26,7 @@ dependencies: package_info_plus: ^8.0.0 local_auth: ^2.3.0 flutter_markdown: ^0.7.2 + posthog_flutter: ^5.24.2 dev_dependencies: flutter_test: