Add PostHog analytics to replace removed Firebase
Wire PostHog iOS SDK into existing EventLogger pattern so all 60+ call sites flow to self-hosted PostHog instance with zero changes. Sets subscription person properties for segmentation on foreground. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,14 +6,17 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
// import Firebase // Firebase removed
|
||||
#if canImport(PostHog)
|
||||
import PostHog
|
||||
#endif
|
||||
|
||||
class EventLogger {
|
||||
static func log(event: String, withData data: [String: Any]? = nil) {
|
||||
// Firebase Analytics disabled
|
||||
// Analytics.logEvent(event, parameters: data)
|
||||
#if DEBUG
|
||||
print("[EventLogger] \(event)", data ?? "")
|
||||
#endif
|
||||
#if canImport(PostHog)
|
||||
PostHogSDK.shared.capture(event, properties: data)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import SwiftUI
|
||||
import SwiftData
|
||||
import BackgroundTasks
|
||||
import WidgetKit
|
||||
import PostHog
|
||||
|
||||
@main
|
||||
struct FeelsApp: App {
|
||||
@@ -23,6 +24,13 @@ struct FeelsApp: App {
|
||||
@State private var showSubscriptionFromWidget = false
|
||||
|
||||
init() {
|
||||
// Initialize PostHog analytics
|
||||
let posthogConfig = PostHogConfig(apiKey: "phc_3GsB3oqNft8Ykg2bJfE9MaJktzLAwr2EPMXQgwEFzAs", host: "https://analytics.88oakapps.com")
|
||||
#if DEBUG
|
||||
posthogConfig.debug = true
|
||||
#endif
|
||||
PostHogSDK.shared.setup(posthogConfig)
|
||||
|
||||
BGTaskScheduler.shared.cancelAllTaskRequests()
|
||||
BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTask.updateDBMissingID, using: nil) { (task) in
|
||||
BGTask.runFillInMissingDatesTask(task: task as! BGProcessingTask)
|
||||
@@ -115,6 +123,36 @@ struct FeelsApp: App {
|
||||
// Check subscription status (network call) - throttled
|
||||
Task.detached(priority: .background) {
|
||||
await iapManager.checkSubscriptionStatus()
|
||||
|
||||
// Set PostHog person properties for subscription segmentation
|
||||
let state = await iapManager.state
|
||||
let subscriptionStatus: String
|
||||
let subscriptionType: String
|
||||
|
||||
switch state {
|
||||
case .subscribed:
|
||||
subscriptionStatus = "subscribed"
|
||||
subscriptionType = await iapManager.currentProduct?.id.contains("yearly") == true ? "yearly" : "monthly"
|
||||
case .inTrial:
|
||||
subscriptionStatus = "trial"
|
||||
subscriptionType = "none"
|
||||
case .trialExpired:
|
||||
subscriptionStatus = "trial_expired"
|
||||
subscriptionType = "none"
|
||||
case .expired:
|
||||
subscriptionStatus = "expired"
|
||||
subscriptionType = "none"
|
||||
case .unknown:
|
||||
subscriptionStatus = "unknown"
|
||||
subscriptionType = "none"
|
||||
}
|
||||
|
||||
PostHogSDK.shared.capture("$set", properties: [
|
||||
"$set": [
|
||||
"subscription_status": subscriptionStatus,
|
||||
"subscription_type": subscriptionType
|
||||
]
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user