Skip to content

Commit 793b3ec

Browse files
committed
Update analytics
1 parent 758b5a6 commit 793b3ec

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

Packages/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ let package = Package(
162162
.target(
163163
name: "AnalyticsClient",
164164
dependencies: [
165+
"Models",
165166
"PersistenceKeys",
166167
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
167168
.product(name: "Mixpanel", package: "mixpanel-swift"),

Packages/Sources/AnalyticsClient/AnalyticsClient.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import Foundation
99
import ComposableArchitecture
1010
import PersistenceKeys
11+
import Models
1112
import Mixpanel
1213
import Sentry
1314
import OSLog
1415

1516
@DependencyClient
1617
public struct AnalyticsClient: Sendable {
1718
public var configure: @Sendable () -> Void
18-
public var identify: @Sendable (_ distinctId: String) -> Void
19+
public var identify: @Sendable (_ id: String) -> Void
1920
public var logout: @Sendable () -> Void
2021
public var log: @Sendable (any Event) -> Void
2122
public var capture: @Sendable (any Error) -> Void
@@ -83,6 +84,26 @@ extension AnalyticsClient {
8384
trackAutomaticEvents: true, // FIXME: LEGACY, REMOVE. https://docs.mixpanel.com/docs/tracking-methods/sdks/swift#legacy-automatically-tracked-events
8485
optOutTrackingByDefault: isDebug
8586
)
87+
88+
@Dependency(\.analyticsClient) var analytics
89+
@Shared(.userSession) var userSession
90+
91+
if let mixpanelUserId = Mixpanel.mainInstance().userId {
92+
if let userId = userSession?.userId {
93+
if String(userId) != mixpanelUserId {
94+
logger.warning("Mixpanel user ID mismatch, changing to \(userId)")
95+
analytics.identify(id: String(userId))
96+
} else {
97+
logger.info("Analytics configured successfully")
98+
}
99+
} else {
100+
logger.warning("Mixpanel user ID found without user session, logging out")
101+
analytics.logout()
102+
}
103+
} else {
104+
logger.info("Mixpanel user ID not found, defaulting to \(id)")
105+
Mixpanel.mainInstance().distinctId = id
106+
}
86107
}
87108

88109
private static func configureSentry(id: String) {

Packages/Sources/AnalyticsClient/Events/ProfileEvent.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
public enum ProfileEvent: Event {
1111
case logoutTapped
1212
case userLoaded(Int)
13+
case userLoadingFailed
1314

1415
public var name: String {
1516
return "Profile " + eventName(for: self).inProperCase

Packages/Sources/MenuFeature/Analytics/MenuFeature+Analytics.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ extension MenuFeature {
2020
var body: some ReducerOf<Self> {
2121
Reduce { state, action in
2222
switch action {
23-
case .onTask, .alert, .notImplementedFeatureTapped, ._subscribeToUpdates, ._loadUserResult:
23+
case .onTask, .alert, .notImplementedFeatureTapped, ._subscribeToUpdates, ._loadUserResult, .profileTapped:
2424
break
2525

26-
case .profileTapped:
27-
analyticsClient.log(MenuEvent.profileTapped)
28-
2926
case .settingsTapped:
3027
analyticsClient.log(MenuEvent.settingsTapped)
3128

Packages/Sources/ProfileFeature/Analytics/ProfileFeature+Analytics.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension ProfileFeature {
2020
var body: some ReducerOf<Self> {
2121
Reduce { state, action in
2222
switch action {
23-
case .onTask, .alert, ._userResponse(.failure):
23+
case .onTask, .alert:
2424
break
2525

2626
case .logoutButtonTapped:
@@ -29,6 +29,9 @@ extension ProfileFeature {
2929

3030
case let ._userResponse(.success(user)):
3131
analyticsClient.log(ProfileEvent.userLoaded(user.id))
32+
33+
case ._userResponse(.failure):
34+
analyticsClient.log(ProfileEvent.userLoadingFailed)
3235
}
3336
return .none
3437
}

Packages/Sources/ProfileFeature/ProfileFeature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public struct ProfileFeature: Sendable {
7575
case .onTask:
7676
return .run { [userId = state.userId] send in
7777
do {
78-
for try await user in try await apiClient.getUser(userId: userId) {
78+
for try await user in try await apiClient.getUser(userId) {
7979
await send(._userResponse(.success(user)))
8080
}
8181
} catch {

0 commit comments

Comments
 (0)