Skip to content

Commit

Permalink
Merge pull request #9 from devtodollars/add-posthog
Browse files Browse the repository at this point in the history
feat: add posthog
  • Loading branch information
matthewwong525 authored Mar 3, 2024
2 parents 1b76cd7 + 5c0a7ad commit 90f6df6
Show file tree
Hide file tree
Showing 28 changed files with 333 additions and 31 deletions.
2 changes: 1 addition & 1 deletion flutter/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
applicationId "com.example.test"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
7 changes: 7 additions & 0 deletions flutter/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!-- Posthog-->
<meta-data android:name="com.posthog.posthog.API_KEY" android:value="phc_brFjanMrA2KXPYyvFjLdwJC9VJvl4VG7mG9yKqvgCCY" />
<meta-data android:name="com.posthog.posthog.POSTHOG_HOST" android:value="https://app.posthog.com" />
<meta-data android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS" android:value="true" />
<meta-data android:name="com.posthog.posthog.DEBUG" android:value="true" />

<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
2 changes: 1 addition & 1 deletion flutter/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
18 changes: 16 additions & 2 deletions flutter/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ PODS:
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- PostHog (3.2.2)
- posthog_flutter (0.0.1):
- Flutter
- FlutterMacOS
- PostHog (~> 3.0)
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand All @@ -15,16 +20,23 @@ DEPENDENCIES:
- app_links (from `.symlinks/plugins/app_links/ios`)
- Flutter (from `Flutter`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- posthog_flutter (from `.symlinks/plugins/posthog_flutter/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)

SPEC REPOS:
trunk:
- PostHog

EXTERNAL SOURCES:
app_links:
:path: ".symlinks/plugins/app_links/ios"
Flutter:
:path: Flutter
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
posthog_flutter:
:path: ".symlinks/plugins/posthog_flutter/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
url_launcher_ios:
Expand All @@ -34,9 +46,11 @@ SPEC CHECKSUMS:
app_links: 5ef33d0d295a89d9d16bb81b0e3b0d5f70d6c875
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
PostHog: bccb2874bf6037af2e81efd0f0f93a2959f1b4f9
posthog_flutter: 4f3764b99e5bb7faa7217cb0836e896e5c32339a
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
url_launcher_ios: 6116280ddcfe98ab8820085d8d76ae7449447586

PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796
PODFILE CHECKSUM: a57f30d18f102dd3ce366b1d62a55ecbef2158e5

COCOAPODS: 1.11.3
COCOAPODS: 1.15.2
4 changes: 3 additions & 1 deletion flutter/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
8ADCD8B923F851F2216E2D6A /* Pods-RunnerTests.release.xcconfig */,
F7E1ED4E714A323AF5AB2900 /* Pods-RunnerTests.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -472,6 +471,7 @@
DEVELOPMENT_TEAM = 4MLDMH9QYL;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -651,6 +651,7 @@
DEVELOPMENT_TEAM = 4MLDMH9QYL;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -674,6 +675,7 @@
DEVELOPMENT_TEAM = 4MLDMH9QYL;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
12 changes: 11 additions & 1 deletion flutter/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>test</string>
<string>DevToDollars</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
Expand Down Expand Up @@ -45,5 +45,15 @@
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>

<!-- Posthog -->
<key>com.posthog.posthog.API_KEY</key>
<string>phc_brFjanMrA2KXPYyvFjLdwJC9VJvl4VG7mG9yKqvgCCY</string>
<key>com.posthog.posthog.POSTHOG_HOST</key>
<string>https://app.posthog.com</string>
<key>com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS</key>
<true/>
<key>com.posthog.posthog.DEBUG</key>
<true/>
</dict>
</plist>
2 changes: 2 additions & 0 deletions flutter/lib/models/app_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ class AppUser {
Session session;
AuthChangeEvent? authEvent;
List<String> activeProducts;
String? stripeCustomerId;

AppUser({
required this.session,
this.authEvent,
this.activeProducts = const [],
this.stripeCustomerId,
});
}
24 changes: 21 additions & 3 deletions flutter/lib/services/auth_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:supabase_flutter/supabase_flutter.dart' as supa;
import 'package:devtodollars/models/app_user.dart';
Expand All @@ -20,7 +21,19 @@ class Auth extends _$Auth {
@override
Stream<AppUser?> build() {
final streamSub = client.auth.onAuthStateChange.listen((authState) async {
await refreshUser(authState);
final appUser = await refreshUser(authState);

// capture posthog events for analytics
if (appUser != null) {
await Posthog()
.identify(userId: appUser.session.user.id, userProperties: {
"email": appUser.session.user.email ?? "",
"active_products": appUser.activeProducts,
"stripe_customer_id": appUser.stripeCustomerId ?? "",
});
} else {
await Posthog().reset();
}
});

ref.onDispose(() {
Expand All @@ -33,9 +46,12 @@ class Auth extends _$Auth {
supa.SupabaseClient get client => supa.Supabase.instance.client;
supa.Session? get currentSession => client.auth.currentSession;

Future<void> refreshUser(supa.AuthState state) async {
Future<AppUser?> refreshUser(supa.AuthState state) async {
final session = state.session;
if (session == null) return authStateController.add(null);
if (session == null) {
authStateController.add(null);
return null;
}

final metadata = await client
.from("stripe")
Expand All @@ -46,8 +62,10 @@ class Auth extends _$Auth {
session: session,
authEvent: state.event,
activeProducts: List<String>.from(metadata?["active_products"] ?? []),
stripeCustomerId: metadata?["stripe_customer_id"],
);
authStateController.add(user);
return user;
}

Future<void> signInWithPassword(String email, String password) async {
Expand Down
2 changes: 2 additions & 0 deletions flutter/lib/services/router_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:devtodollars/components/dialog_page.dart';
import 'package:devtodollars/components/reset_password_dialog.dart';
Expand All @@ -21,6 +22,7 @@ GoRouter router(RouterRef ref) {
return GoRouter(
initialLocation: initUrl?.path, // DO NOT REMOVE
navigatorKey: navigatorKey,
observers: [PosthogObserver()],
redirect: (context, state) async {
return authState.when(
data: (user) {
Expand Down
2 changes: 2 additions & 0 deletions flutter/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import Foundation

import app_links
import path_provider_foundation
import posthog_flutter
import shared_preferences_foundation
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
PosthogFlutterPlugin.register(with: registry.registrar(forPlugin: "PosthogFlutterPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
2 changes: 1 addition & 1 deletion flutter/macos/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :osx, '10.14'
platform :osx, '10.15'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
56 changes: 56 additions & 0 deletions flutter/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
PODS:
- app_links (1.0.0):
- FlutterMacOS
- FlutterMacOS (1.0.0)
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- PostHog (3.2.2)
- posthog_flutter (0.0.1):
- Flutter
- FlutterMacOS
- PostHog (~> 3.0)
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- url_launcher_macos (0.0.1):
- FlutterMacOS

DEPENDENCIES:
- app_links (from `Flutter/ephemeral/.symlinks/plugins/app_links/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- posthog_flutter (from `Flutter/ephemeral/.symlinks/plugins/posthog_flutter/macos`)
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)

SPEC REPOS:
trunk:
- PostHog

EXTERNAL SOURCES:
app_links:
:path: Flutter/ephemeral/.symlinks/plugins/app_links/macos
FlutterMacOS:
:path: Flutter/ephemeral
path_provider_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
posthog_flutter:
:path: Flutter/ephemeral/.symlinks/plugins/posthog_flutter/macos
shared_preferences_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos

SPEC CHECKSUMS:
app_links: 4481ed4d71f384b0c3ae5016f4633aa73d32ff67
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
PostHog: bccb2874bf6037af2e81efd0f0f93a2959f1b4f9
posthog_flutter: 4f3764b99e5bb7faa7217cb0836e896e5c32339a
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
url_launcher_macos: d2691c7dd33ed713bf3544850a623080ec693d95

PODFILE CHECKSUM: 9ebaf0ce3d369aaa26a9ea0e159195ed94724cf3

COCOAPODS: 1.15.2
Loading

0 comments on commit 90f6df6

Please sign in to comment.