import 'package:flutter/material.dart'; import 'package:logging/logging.dart'; import 'package:posthog_flutter/posthog_flutter.dart'; import 'package:pshared/config/constants.dart'; import 'package:pshared/models/account/account.dart'; import 'package:pshared/models/payment/type.dart'; import 'package:pshared/models/recipient/status.dart'; import 'package:pshared/models/recipient/type.dart'; import 'package:pweb/widgets/sidebar/destinations.dart'; class PosthogService { static final _logger = Logger('service.posthog'); static String? _identifiedUserId; static bool _initialized = false; static bool get isEnabled => _initialized; static Future initialize() async { final apiKey = Constants.posthogApiKey; if (apiKey.isEmpty) { _logger.warning('PostHog API key is not configured, analytics disabled.'); return; } try { final config = PostHogConfig(apiKey) ..host = Constants.posthogHost ..captureApplicationLifecycleEvents = true; await Posthog().setup(config); await Posthog().register('client_id', Constants.clientId); _initialized = true; _logger.info('PostHog initialized with host ${Constants.posthogHost}'); } catch (e, st) { _initialized = false; _logger.warning('Failed to initialize PostHog: $e', e, st); } } static Future identify(Account account) async { if (!_initialized) return; if (_identifiedUserId == account.id) return; await Posthog().identify( userId: account.id, userProperties: { 'email': account.login, 'name': account.name, 'locale': account.locale, 'created_at': account.createdAt.toIso8601String(), }, ); _identifiedUserId = account.id; } static Future reset() async { if (!_initialized) return; _identifiedUserId = null; await Posthog().reset(); } static Future login({required bool pending}) async { if (!_initialized) return; await _capture( 'login', properties: { 'result': pending ? 'pending' : 'success', }, ); } static Future pageOpened(PayoutDestination page, {String? path, String? uiSource}) async { if (!_initialized) return; return _capture( 'pageOpened', properties: { 'page': page.name, if (path != null) 'path': path, if (uiSource != null) 'uiSource': uiSource, }, ); } static Future localeChanged(Locale locale) async { if (!_initialized) return; return _capture( 'localeChanged', properties: {'locale': locale.toLanguageTag()}, ); } static Future recipientAddCompleted( RecipientType type, RecipientStatus status, Set methods, ) async { if (!_initialized) return; return _capture( 'recipientAddCompleted', properties: { 'methods': methods.map((m) => m.name).toList(), 'type': type.name, 'status': status.name, }, ); } static Future paymentInitiated({PaymentType? method}) async { if (!_initialized) return; return _capture( 'paymentInitiated', properties: { if (method != null) 'method': method.name, }, ); } static Future _capture( String eventName, { Map? properties, }) async { if (!_initialized) return; final filtered = {}; if (properties != null) { for (final entry in properties.entries) { final value = entry.value; if (value != null) filtered[entry.key] = value; } } await Posthog().capture(eventName: eventName, properties: filtered.isEmpty ? null : filtered); } }