Add PostHog analytics integration for Android and iOS
Implement comprehensive analytics tracking across both platforms: Android (Kotlin): - Add PostHog SDK dependency and initialization in MainActivity - Create expect/actual pattern for cross-platform analytics (commonMain/androidMain/iosMain/jvmMain/jsMain/wasmJsMain) - Track screen views: registration, login, residences, tasks, contractors, documents, notifications, profile - Track key events: user_registered, user_signed_in, residence_created, task_created, contractor_created, document_created - Track paywall events: contractor_paywall_shown, documents_paywall_shown - Track sharing events: residence_shared, contractor_shared - Track theme_changed event iOS (Swift): - Add PostHog iOS SDK via SPM - Create PostHogAnalytics wrapper and AnalyticsEvents constants - Initialize SDK in iOSApp with session replay support - Track same screen views and events as Android - Track user identification after login/registration 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
116
iosApp/iosApp/Analytics/PostHogAnalytics.swift
Normal file
116
iosApp/iosApp/Analytics/PostHogAnalytics.swift
Normal file
@@ -0,0 +1,116 @@
|
||||
import Foundation
|
||||
import PostHog
|
||||
|
||||
/// PostHog Analytics wrapper for iOS.
|
||||
/// Provides a simple interface for tracking events, screens, and user identification.
|
||||
final class PostHogAnalytics {
|
||||
static let shared = PostHogAnalytics()
|
||||
|
||||
// TODO: Replace with your actual PostHog API key
|
||||
private let apiKey = "phc_zAf8ZEwHtr4zB6UgheP1epStBTNKP8mDBMtwzQ0BzfU"
|
||||
private let host = "https://us.i.posthog.com"
|
||||
|
||||
private var isInitialized = false
|
||||
|
||||
private init() {}
|
||||
|
||||
/// Initialize PostHog SDK. Call this in App.init()
|
||||
func initialize() {
|
||||
guard !isInitialized else { return }
|
||||
|
||||
let config = PostHogConfig(apiKey: apiKey, host: host)
|
||||
config.captureScreenViews = false // We'll track screens manually for SwiftUI
|
||||
config.captureApplicationLifecycleEvents = true
|
||||
|
||||
#if DEBUG
|
||||
config.debug = true
|
||||
#endif
|
||||
|
||||
// Session Replay (required for SwiftUI: use screenshot mode)
|
||||
config.sessionReplay = true
|
||||
config.sessionReplayConfig.screenshotMode = true // Required for SwiftUI
|
||||
config.sessionReplayConfig.maskAllTextInputs = true // Privacy: mask text inputs
|
||||
config.sessionReplayConfig.maskAllImages = false
|
||||
|
||||
PostHogSDK.shared.setup(config)
|
||||
isInitialized = true
|
||||
}
|
||||
|
||||
/// Identify a user. Call this after successful login/registration.
|
||||
/// This links all future events to this user ID.
|
||||
func identify(_ userId: String, properties: [String: Any]? = nil) {
|
||||
guard isInitialized else { return }
|
||||
PostHogSDK.shared.identify(userId, userProperties: properties)
|
||||
}
|
||||
|
||||
/// Capture a custom event with optional properties.
|
||||
/// Use format: "object_action" (e.g., "residence_created", "task_completed")
|
||||
func capture(_ event: String, properties: [String: Any]? = nil) {
|
||||
guard isInitialized else { return }
|
||||
PostHogSDK.shared.capture(event, properties: properties)
|
||||
}
|
||||
|
||||
/// Track a screen view. Call this when a screen appears.
|
||||
func screen(_ screenName: String, properties: [String: Any]? = nil) {
|
||||
guard isInitialized else { return }
|
||||
PostHogSDK.shared.screen(screenName, properties: properties)
|
||||
}
|
||||
|
||||
/// Reset the user identity. Call this on logout.
|
||||
/// This starts a new anonymous session.
|
||||
func reset() {
|
||||
guard isInitialized else { return }
|
||||
PostHogSDK.shared.reset()
|
||||
}
|
||||
|
||||
/// Flush any queued events immediately.
|
||||
func flush() {
|
||||
guard isInitialized else { return }
|
||||
PostHogSDK.shared.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/// Analytics event names - use these constants for consistency across the app
|
||||
enum AnalyticsEvents {
|
||||
// Authentication
|
||||
static let registrationScreenShown = "registration_screen_shown"
|
||||
static let userRegistered = "user_registered"
|
||||
static let userSignedIn = "user_signed_in"
|
||||
static let userSignedInApple = "user_signed_in_apple"
|
||||
|
||||
// Residence
|
||||
static let residenceScreenShown = "residence_screen_shown"
|
||||
static let newResidenceScreenShown = "new_residence_screen_shown"
|
||||
static let residenceCreated = "residence_created"
|
||||
static let residenceLimitReached = "residence_limit_reached"
|
||||
|
||||
// Task
|
||||
static let taskScreenShown = "task_screen_shown"
|
||||
static let newTaskScreenShown = "new_task_screen_shown"
|
||||
static let taskCreated = "task_created"
|
||||
|
||||
// Contractor
|
||||
static let contractorScreenShown = "contractor_screen_shown"
|
||||
static let newContractorScreenShown = "new_contractor_screen_shown"
|
||||
static let contractorCreated = "contractor_created"
|
||||
static let contractorPaywallShown = "contractor_paywall_shown"
|
||||
|
||||
// Documents
|
||||
static let documentsScreenShown = "documents_screen_shown"
|
||||
static let newDocumentScreenShown = "new_document_screen_shown"
|
||||
static let documentCreated = "document_created"
|
||||
static let documentsPaywallShown = "documents_paywall_shown"
|
||||
|
||||
// Sharing
|
||||
static let shareResidenceScreenShown = "share_residence_screen_shown"
|
||||
static let residenceShared = "residence_shared"
|
||||
static let shareResidencePaywallShown = "share_residence_paywall_shown"
|
||||
static let shareContractorScreenShown = "share_contractor_screen_shown"
|
||||
static let contractorShared = "contractor_shared"
|
||||
static let shareContractorPaywallShown = "share_contractor_paywall_shown"
|
||||
|
||||
// Settings
|
||||
static let notificationSettingsScreenShown = "notification_settings_screen_shown"
|
||||
static let settingsScreenShown = "settings_screen_shown"
|
||||
static let themeChanged = "theme_changed"
|
||||
}
|
||||
@@ -284,6 +284,10 @@ struct ContractorFormSheet: View {
|
||||
specialtyPickerSheet
|
||||
}
|
||||
.onAppear {
|
||||
// Track screen view for new contractors
|
||||
if contractor == nil {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.newContractorScreenShown)
|
||||
}
|
||||
residenceViewModel.loadMyResidences()
|
||||
loadContractorData()
|
||||
}
|
||||
@@ -493,6 +497,8 @@ struct ContractorFormSheet: View {
|
||||
|
||||
viewModel.createContractor(request: request) { success in
|
||||
if success {
|
||||
// Track contractor creation
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.contractorCreated)
|
||||
onSave()
|
||||
dismiss()
|
||||
}
|
||||
|
||||
@@ -59,6 +59,8 @@ class ContractorSharingManager: ObservableObject {
|
||||
|
||||
do {
|
||||
try jsonData.write(to: tempURL)
|
||||
// Track contractor shared event
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.contractorShared)
|
||||
return tempURL
|
||||
} catch {
|
||||
print("ContractorSharingManager: Failed to write .casera file: \(error)")
|
||||
|
||||
@@ -144,7 +144,10 @@ struct ContractorsListView: View {
|
||||
|
||||
// Add Button (disabled when showing upgrade screen)
|
||||
Button(action: {
|
||||
if subscriptionCache.shouldShowUpgradePrompt(currentCount: viewModel.contractors.count, limitKey: "contractors") {
|
||||
let currentCount = viewModel.contractors.count
|
||||
if subscriptionCache.shouldShowUpgradePrompt(currentCount: currentCount, limitKey: "contractors") {
|
||||
// Track paywall shown
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.contractorPaywallShown, properties: ["current_count": currentCount])
|
||||
showingUpgradePrompt = true
|
||||
} else {
|
||||
showingAddSheet = true
|
||||
@@ -171,6 +174,7 @@ struct ContractorsListView: View {
|
||||
UpgradePromptView(triggerKey: "view_contractors", isPresented: $showingUpgradePrompt)
|
||||
}
|
||||
.onAppear {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.contractorScreenShown)
|
||||
loadContractors()
|
||||
}
|
||||
// No need for onChange on searchText - filtering is client-side
|
||||
|
||||
@@ -243,6 +243,11 @@ struct DocumentFormView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Track screen view for new documents
|
||||
if !isEditMode {
|
||||
let docType = isWarranty ? "warranty" : "document"
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.newDocumentScreenShown, properties: ["type": docType])
|
||||
}
|
||||
if needsResidenceSelection {
|
||||
residenceViewModel.loadMyResidences()
|
||||
}
|
||||
@@ -491,6 +496,9 @@ struct DocumentFormView: View {
|
||||
) { success, error in
|
||||
isProcessing = false
|
||||
if success {
|
||||
// Track document creation
|
||||
let docType = isWarranty ? "warranty" : "document"
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.documentCreated, properties: ["type": docType])
|
||||
// Reload documents to show new item
|
||||
documentViewModel.loadDocuments(residenceId: actualResidenceId)
|
||||
isPresented = false
|
||||
|
||||
@@ -179,6 +179,8 @@ struct DocumentsWarrantiesView: View {
|
||||
// Check LIVE document count before adding
|
||||
let currentCount = documentViewModel.documents.count
|
||||
if subscriptionCache.shouldShowUpgradePrompt(currentCount: currentCount, limitKey: "documents") {
|
||||
// Track paywall shown
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.documentsPaywallShown, properties: ["current_count": currentCount])
|
||||
showingUpgradePrompt = true
|
||||
} else {
|
||||
showAddSheet = true
|
||||
@@ -192,6 +194,8 @@ struct DocumentsWarrantiesView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Track screen view
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.documentsScreenShown)
|
||||
// Load all documents once - filtering is client-side
|
||||
loadAllDocuments()
|
||||
}
|
||||
|
||||
@@ -85,6 +85,15 @@ class AppleSignInViewModel: ObservableObject {
|
||||
// - Initializes lookups
|
||||
// - Prefetches all data
|
||||
|
||||
// Track Apple Sign In
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.userSignedInApple, properties: [
|
||||
"is_new_user": isNewUser
|
||||
])
|
||||
PostHogAnalytics.shared.identify(
|
||||
String(user.id),
|
||||
properties: ["email": user.email ?? "", "username": user.username]
|
||||
)
|
||||
|
||||
print("Apple Sign In successful! User: \(user.username), New user: \(isNewUser)")
|
||||
|
||||
// Call success callback with verification status
|
||||
|
||||
@@ -73,6 +73,13 @@ class LoginViewModel: ObservableObject {
|
||||
print("Login successful!")
|
||||
print("User: \(response.user.username ?? "unknown"), Verified: \(self.isVerified)")
|
||||
|
||||
// Track successful login
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.userSignedIn, properties: ["method": "email"])
|
||||
PostHogAnalytics.shared.identify(
|
||||
String(response.user.id),
|
||||
properties: ["email": response.user.email ?? "", "username": response.user.username ?? ""]
|
||||
)
|
||||
|
||||
// Initialize lookups via APILayer
|
||||
Task {
|
||||
_ = try? await APILayer.shared.initializeLookups()
|
||||
@@ -117,6 +124,9 @@ class LoginViewModel: ObservableObject {
|
||||
// APILayer.logout clears DataManager
|
||||
try? await APILayer.shared.logout()
|
||||
|
||||
// Reset PostHog user identity
|
||||
PostHogAnalytics.shared.reset()
|
||||
|
||||
// Clear widget task data
|
||||
WidgetDataManager.shared.clearCache()
|
||||
|
||||
|
||||
@@ -297,6 +297,8 @@ struct NotificationPreferencesView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Track screen view
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.notificationSettingsScreenShown)
|
||||
viewModel.loadPreferences()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +208,9 @@ struct ProfileTabView: View {
|
||||
} message: {
|
||||
Text(L10n.Profile.purchasesRestoredMessage)
|
||||
}
|
||||
.onAppear {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.settingsScreenShown)
|
||||
}
|
||||
}
|
||||
|
||||
private func sendSupportEmail() {
|
||||
|
||||
@@ -39,6 +39,9 @@ struct ThemeSelectionView: View {
|
||||
let generator = UIImpactFeedbackGenerator(style: .medium)
|
||||
generator.impactOccurred()
|
||||
|
||||
// Track theme change
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.themeChanged, properties: ["theme": theme.rawValue])
|
||||
|
||||
// Update theme with animation
|
||||
themeManager.setTheme(theme)
|
||||
}
|
||||
|
||||
@@ -152,6 +152,9 @@ struct RegisterView: View {
|
||||
error: viewModel.errorMessage,
|
||||
onRetry: { viewModel.register() }
|
||||
)
|
||||
.onAppear {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.registrationScreenShown)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,12 +49,19 @@ class RegisterViewModel: ObservableObject {
|
||||
let request = RegisterRequest(username: username, email: email, password: password, firstName: nil, lastName: nil)
|
||||
let result = try await APILayer.shared.register(request: request)
|
||||
|
||||
if let success = result as? ApiResultSuccess<AuthResponse>, let _ = success.data {
|
||||
if let success = result as? ApiResultSuccess<AuthResponse>, let response = success.data {
|
||||
// APILayer.register() now handles:
|
||||
// - Setting auth token in DataManager
|
||||
// - Storing token in TokenManager
|
||||
// - Initializing lookups
|
||||
|
||||
// Track successful registration
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.userRegistered, properties: ["method": "email"])
|
||||
PostHogAnalytics.shared.identify(
|
||||
String(response.user.id),
|
||||
properties: ["email": response.user.email ?? "", "username": response.user.username ?? ""]
|
||||
)
|
||||
|
||||
// Update AuthenticationManager - user is authenticated but NOT verified
|
||||
AuthenticationManager.shared.login(verified: false)
|
||||
|
||||
|
||||
@@ -89,6 +89,8 @@ class ResidenceSharingManager: ObservableObject {
|
||||
|
||||
do {
|
||||
try jsonData.write(to: tempURL)
|
||||
// Track residence shared event
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.residenceShared, properties: ["method": "file"])
|
||||
return tempURL
|
||||
} catch {
|
||||
print("ResidenceSharingManager: Failed to write .casera file: \(error)")
|
||||
|
||||
@@ -111,6 +111,7 @@ struct ResidencesListView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.residenceScreenShown)
|
||||
if authManager.isAuthenticated {
|
||||
viewModel.loadMyResidences()
|
||||
}
|
||||
|
||||
@@ -230,6 +230,9 @@ struct ResidenceFormView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if !isEditMode {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.newResidenceScreenShown)
|
||||
}
|
||||
loadResidenceTypes()
|
||||
initializeForm()
|
||||
if isEditMode && isCurrentUserOwner {
|
||||
@@ -368,6 +371,10 @@ struct ResidenceFormView: View {
|
||||
// Add mode
|
||||
viewModel.createResidence(request: request) { success in
|
||||
if success {
|
||||
// Track residence created
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.residenceCreated, properties: [
|
||||
"residence_type": selectedPropertyType?.name ?? "unknown"
|
||||
])
|
||||
onSuccess?()
|
||||
isPresented = false
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ struct AllTasksView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.taskScreenShown)
|
||||
loadAllTasks()
|
||||
residenceViewModel.loadMyResidences()
|
||||
}
|
||||
|
||||
@@ -312,6 +312,10 @@ struct TaskFormView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Track screen view for new tasks
|
||||
if !isEditMode {
|
||||
PostHogAnalytics.shared.screen(AnalyticsEvents.newTaskScreenShown)
|
||||
}
|
||||
// Set defaults when lookups are available
|
||||
if dataManager.lookupsInitialized {
|
||||
setDefaults()
|
||||
@@ -520,6 +524,8 @@ struct TaskFormView: View {
|
||||
|
||||
viewModel.createTask(request: request) { success in
|
||||
if success {
|
||||
// Track task creation
|
||||
PostHogAnalytics.shared.capture(AnalyticsEvents.taskCreated, properties: ["residence_id": actualResidenceId])
|
||||
// View will dismiss automatically via onChange
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ struct iOSApp: App {
|
||||
// Initialize TokenStorage once at app startup (legacy support)
|
||||
TokenStorage.shared.initialize(manager: TokenManager.Companion.shared.getInstance())
|
||||
|
||||
// Initialize PostHog Analytics
|
||||
PostHogAnalytics.shared.initialize()
|
||||
|
||||
// Initialize lookups at app start (public endpoints, no auth required)
|
||||
// This fetches /static_data/ and /upgrade-triggers/ immediately
|
||||
Task {
|
||||
|
||||
Reference in New Issue
Block a user