From 6b1ca6f45d07d1f22e5316f2bea5a0634daf78b5 Mon Sep 17 00:00:00 2001 From: inpt333 Date: Mon, 23 Dec 2024 15:43:36 +0100 Subject: [PATCH] feat: integrate posthog --- android/app/src/main/AndroidManifest.xml | 2 ++ ios/Podfile | 2 +- ios/Runner/Info.plist | 2 ++ lib/epics/analytics.dart | 22 ++++++++++++++++++ lib/epics/epics.dart | 1 + lib/main.dart | 4 ++++ lib/util/analytics.dart | 23 +++++++++++++++++++ lib/util/util.dart | 1 + macos/Flutter/GeneratedPluginRegistrant.swift | 2 ++ macos/Runner/Info.plist | 2 ++ pubspec.lock | 8 +++++++ pubspec.yaml | 1 + web/index.html | 7 ++++++ 13 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lib/epics/analytics.dart create mode 100644 lib/util/analytics.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 78111576..d0d46544 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -65,5 +65,7 @@ + + diff --git a/ios/Podfile b/ios/Podfile index 164df534..3e44f9c6 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -1,5 +1,5 @@ # Uncomment this line to define a global platform for your project -platform :ios, '12.0' +platform :ios, '13.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 918ea494..d84376ed 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -70,5 +70,7 @@ UIStatusBarHidden + com.posthog.posthog.AUTO_INIT + diff --git a/lib/epics/analytics.dart b/lib/epics/analytics.dart new file mode 100644 index 00000000..038de3c3 --- /dev/null +++ b/lib/epics/analytics.dart @@ -0,0 +1,22 @@ +import 'package:parousia/actions/actions.dart'; +import 'package:parousia/state/state.dart'; +import 'package:parousia/util/util.dart'; +import 'package:redux_epics/redux_epics.dart'; +import 'package:rxdart/rxdart.dart'; + +/// All epics that handle analytics +Epic createAnalyticsEpics() => combineEpics([ + _identifyUserEpic, + ]); + +/// Identify the user in PostHog +Stream _identifyUserEpic( + Stream actions, EpicStore store) => + actions + .whereType() + .where((action) => store.state.auth.user != null) + .map((action) async { + final email = store.state.auth.user!.email; + await identifyUser( + store.state.auth.user!.id, email != null ? {'email': email} : null); + }); diff --git a/lib/epics/epics.dart b/lib/epics/epics.dart index 6cd2488e..758147c2 100644 --- a/lib/epics/epics.dart +++ b/lib/epics/epics.dart @@ -1,3 +1,4 @@ +export 'analytics.dart'; export 'auth.dart'; export 'default_rules.dart'; export 'groups.dart'; diff --git a/lib/main.dart b/lib/main.dart index 09fc9395..b9225da1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -36,6 +36,9 @@ Future main() async { yield LicenseEntryWithLineBreaks(['google_fonts'], license); }); + // Initialize analytics + await initAnalytics(); + final configService = ConfigService(); await configService.initialize(); @@ -79,6 +82,7 @@ Future> _initStore(SupabaseClient supabase) async { final epics = combineEpics([ createRouterEpics(router), createAuthEpics(), + createAnalyticsEpics(), createDefaultRulesEpics(defaultRulesRepository), createGroupsEpics(groupsRepository), createMembersEpic(membersRepository), diff --git a/lib/util/analytics.dart b/lib/util/analytics.dart new file mode 100644 index 00000000..c48f6ff1 --- /dev/null +++ b/lib/util/analytics.dart @@ -0,0 +1,23 @@ +import 'package:posthog_flutter/posthog_flutter.dart'; + +/// Initialize PostHog +Future initAnalytics() async { + final config = + PostHogConfig('phc_uXbU2zSWtnx3DYQFDturj5iiXGbOSkMWLukl6LtPbyT'); + config.debug = true; + config.captureApplicationLifecycleEvents = false; + config.host = 'https://us.i.posthog.com'; + config.sessionReplay = true; + config.sessionReplayConfig.maskAllTexts = false; + config.sessionReplayConfig.maskAllImages = false; + config.sessionReplayConfig.throttleDelay = const Duration(milliseconds: 1000); + config.flushAt = 1; + final posthog = Posthog(); + await posthog.setup(config); +} + +/// Identify user in PostHog +Future identifyUser( + String userId, Map? userProperties) async { + await Posthog().identify(userId: userId, userProperties: userProperties); +} diff --git a/lib/util/util.dart b/lib/util/util.dart index 89170d0f..d4485dd6 100644 --- a/lib/util/util.dart +++ b/lib/util/util.dart @@ -1,3 +1,4 @@ +export 'analytics.dart'; export 'base32.dart'; export 'colors.dart'; export 'datetime.dart'; diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index c2769b26..8b55c823 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -9,6 +9,7 @@ import app_links import file_selector_macos import google_sign_in_ios import path_provider_foundation +import posthog_flutter import share_plus import shared_preferences_foundation import sign_in_with_apple @@ -19,6 +20,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FLTGoogleSignInPlugin.register(with: registry.registrar(forPlugin: "FLTGoogleSignInPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + PosthogFlutterPlugin.register(with: registry.registrar(forPlugin: "PosthogFlutterPlugin")) SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SignInWithApplePlugin.register(with: registry.registrar(forPlugin: "SignInWithApplePlugin")) diff --git a/macos/Runner/Info.plist b/macos/Runner/Info.plist index 4789daa6..f0a25263 100644 --- a/macos/Runner/Info.plist +++ b/macos/Runner/Info.plist @@ -28,5 +28,7 @@ MainMenu NSPrincipalClass NSApplication + com.posthog.posthog.AUTO_INIT + diff --git a/pubspec.lock b/pubspec.lock index 1f7fe8da..6c312d46 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1081,6 +1081,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.4.0" + posthog_flutter: + dependency: "direct main" + description: + name: posthog_flutter + sha256: c2d45937331785d49f5be6583a73e7c011b8480e3949744edb9107ae7d41c2c1 + url: "https://pub.dev" + source: hosted + version: "4.9.0" process: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e07b1574..8276503f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -90,6 +90,7 @@ dependencies: google_fonts: ^6.2.1 mime: ^1.0.4 memoized: ^2.2.6 + posthog_flutter: ^4.9.0 universal_html: ^2.2.4 uuid: ^4.1.0 share_plus: ^10.1.2 diff --git a/web/index.html b/web/index.html index 18adb787..a7511f0e 100644 --- a/web/index.html +++ b/web/index.html @@ -110,6 +110,13 @@ document.body.style.background = "transparent"; } +