Skip to content

Commit

Permalink
feat: integrate posthog
Browse files Browse the repository at this point in the history
  • Loading branch information
inpt333 committed Dec 23, 2024
1 parent 12eef5c commit 6b1ca6f
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@
<meta-data
android:name="flutterEmbedding"
android:value="2"/>
<!-- PostHog -->
<meta-data android:name="com.posthog.posthog.AUTO_INIT" android:value="false" />
</application>
</manifest>
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 2 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@
</array>
<key>UIStatusBarHidden</key>
<false/>
<key>com.posthog.posthog.AUTO_INIT</key>
<false/>
</dict>
</plist>
22 changes: 22 additions & 0 deletions lib/epics/analytics.dart
Original file line number Diff line number Diff line change
@@ -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<AppState> createAnalyticsEpics() => combineEpics<AppState>([
_identifyUserEpic,
]);

/// Identify the user in PostHog
Stream<dynamic> _identifyUserEpic(
Stream<dynamic> actions, EpicStore<AppState> store) =>
actions
.whereType<AuthStateChangedAction>()
.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);
});
1 change: 1 addition & 0 deletions lib/epics/epics.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'analytics.dart';
export 'auth.dart';
export 'default_rules.dart';
export 'groups.dart';
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Future<void> main() async {
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});

// Initialize analytics
await initAnalytics();

final configService = ConfigService();
await configService.initialize();

Expand Down Expand Up @@ -79,6 +82,7 @@ Future<Store<AppState>> _initStore(SupabaseClient supabase) async {
final epics = combineEpics<AppState>([
createRouterEpics(router),
createAuthEpics(),
createAnalyticsEpics(),
createDefaultRulesEpics(defaultRulesRepository),
createGroupsEpics(groupsRepository),
createMembersEpic(membersRepository),
Expand Down
23 changes: 23 additions & 0 deletions lib/util/analytics.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:posthog_flutter/posthog_flutter.dart';

/// Initialize PostHog
Future<void> 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<void> identifyUser(
String userId, Map<String, Object>? userProperties) async {
await Posthog().identify(userId: userId, userProperties: userProperties);
}
1 change: 1 addition & 0 deletions lib/util/util.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'analytics.dart';
export 'base32.dart';
export 'colors.dart';
export 'datetime.dart';
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down
2 changes: 2 additions & 0 deletions macos/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>com.posthog.posthog.AUTO_INIT</key>
<false/>
</dict>
</plist>
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
document.body.style.background = "transparent";
}
</script>
<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('phc_uXbU2zSWtnx3DYQFDturj5iiXGbOSkMWLukl6LtPbyT', {
api_host:'https://us.i.posthog.com',
person_profiles: 'identified_only'
})
</script>
</head>
<body>
<picture id="splash">
Expand Down

0 comments on commit 6b1ca6f

Please sign in to comment.