Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: feat/identify users posthog #123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
<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
18 changes: 18 additions & 0 deletions lib/util/analytics.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:posthog_flutter/posthog_flutter.dart';

/// Initialize PostHog
Future<void> initAnalytics() async {
final config =
PostHogConfig('phc_maDGNEUn4Xpo0Nys1jNI2GnZjEQTVD2IIymbcRxBPta');
config.host = 'https://eu.i.posthog.com';
config.sendFeatureFlagEvents = false;
config.preloadFeatureFlags = false;
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_maDGNEUn4Xpo0Nys1jNI2GnZjEQTVD2IIymbcRxBPta', {
api_host:'https://eu.i.posthog.com',
person_profiles: 'identified_only'
})
</script>
</head>
<body>
<picture id="splash">
Expand Down