Created
December 23, 2024 07:44
PostHog Analytics Service for Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:posthog_flutter/posthog_flutter.dart'; | |
// Monitoring | |
import 'analytics_client_base.dart'; | |
class PosthogAnalyticsClient implements AnalyticsClientBase { | |
static final Posthog _posthog = Posthog(); | |
const PosthogAnalyticsClient(); | |
@override | |
Future<void> toggleAnalyticsCollection(bool enabled) { | |
if (enabled) { | |
return _posthog.enable(); | |
} else { | |
return _posthog.disable(); | |
} | |
} | |
@override | |
Future<void> identifyUser({ | |
required String userId, | |
required String email, | |
required String role, | |
}) async { | |
if (await _posthog.getDistinctId() == userId) return; | |
return _posthog.identify( | |
userId: userId, | |
userProperties: { | |
'email': email, | |
'role': role, | |
}, | |
); | |
} | |
@override | |
Future<void> resetUser() { | |
return _posthog.reset(); | |
} | |
@override | |
Future<void> trackEvent(String eventName, [Map<String, Object>? eventData]) { | |
return _posthog.capture(eventName: eventName, properties: eventData); | |
} | |
@override | |
Future<void> trackScreenView(String routeName, String action) { | |
return _posthog.screen( | |
screenName: routeName, | |
properties: {'action': action}, | |
); | |
} | |
@override | |
Future<void> trackNewAppOnboarding() { | |
return _posthog.capture(eventName: 'user_completed_onboarding'); | |
} | |
@override | |
Future<void> trackAppCreated() { | |
return _posthog.capture(eventName: 'app_created'); | |
} | |
@override | |
Future<void> trackAppUpdated() { | |
return _posthog.capture(eventName: 'app_updated'); | |
} | |
@override | |
Future<void> trackAppDeleted() { | |
return _posthog.capture(eventName: 'app_deleted'); | |
} | |
@override | |
Future<void> trackTaskCompleted(int completedCount) { | |
return _posthog.capture( | |
eventName: 'task_completed', | |
properties: { | |
'completed_count': completedCount, | |
}, | |
); | |
} | |
@override | |
Future<void> trackAppBackgrounded() { | |
return _posthog.capture(eventName: 'app_backgrounded'); | |
} | |
@override | |
Future<void> trackAppForegrounded() { | |
return _posthog.capture(eventName: 'app_foregrounded'); | |
} | |
@override | |
Future<void> trackBottomSheetView(String routeName, [Map<String, Object>? data]) { | |
return _posthog.capture( | |
eventName: 'bottom_sheet_view', | |
properties: { | |
'route_name': routeName, | |
...?data, | |
}, | |
); | |
} | |
@override | |
Future<void> trackDialogView(String dialogName, [Map<String, Object>? data]) { | |
return _posthog.capture( | |
eventName: 'dialog_view', | |
properties: { | |
'dialog_name': dialogName, | |
...?data, | |
}, | |
); | |
} | |
@override | |
Future<void> trackButtonPressed(String buttonName, [Map<String, Object>? data]) { | |
return _posthog.capture( | |
eventName: 'button_pressed', | |
properties: { | |
'button_name': buttonName, | |
...?data, | |
}, | |
); | |
} | |
@override | |
Future<void> trackPermissionRequest(String permission, String status) { | |
return _posthog.capture( | |
eventName: 'permission_requested', | |
properties: { | |
'permission': permission, | |
'status': status, | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment