Skip to content

Commit

Permalink
♻️ (Analytics): Refactor analytics regarding auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Nov 25, 2024
1 parent d7a9a5b commit b26a217
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 0 additions & 4 deletions Apps/LekaApp/Sources/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0

import AccountKit
import AnalyticsKit
import DesignKit
import DeviceKit
import LocalizationKit
Expand Down Expand Up @@ -131,7 +130,6 @@ struct SettingsView: View {
self.authManager.signOut()
self.persistentDataManager.clearUserData()
self.reset()
AnalyticsManager.logEventLogout()
} label: {
Text(l10n.SettingsView.AccountSection.LogOut.alertButtonLabel)
}
Expand Down Expand Up @@ -172,7 +170,6 @@ struct SettingsView: View {
) {
self.dismiss()
self.authManager.deleteCurrentUser()
AnalyticsManager.logEventAccountDelete()
}
} message: {
Text(l10n.SettingsView.AccountSection.DeleteAccount.alertMessage)
Expand Down Expand Up @@ -258,7 +255,6 @@ struct SettingsView: View {
self.rootAccountViewModel.resetData()
self.styleManager.accentColor = DesignKitAsset.Colors.lekaDarkBlue.swiftUIColor
self.styleManager.colorScheme = .light
AnalyticsManager.clearDefaultEventParameters()
}

private var errorAlertTitle: String {
Expand Down
26 changes: 25 additions & 1 deletion Modules/AccountKit/Sources/Authentication/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import AnalyticsKit
import Combine
import FirebaseAuth
import FirebaseAuthCombineSwift
Expand Down Expand Up @@ -60,6 +61,9 @@ public class AuthManager {
log.info("User \(result.user.uid) signed-up successfully. 🎉")
self?.authenticationState.send(.loggedIn)
self?.sendEmailVerification()
AnalyticsManager.setUserID(result.user.uid)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: true)
AnalyticsManager.setDefaultEventParameterRootOwnerUid(result.user.uid)
})
.store(in: &self.cancellables)
}
Expand All @@ -75,15 +79,22 @@ public class AuthManager {
log.info("User \(user.uid) signed-in successfully. 🎉")
self?.authenticationState.send(.loggedIn)
self?.emailVerificationState.send(user.isEmailVerified)
AnalyticsManager.setUserID(user.uid)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: true)
AnalyticsManager.setDefaultEventParameterRootOwnerUid(user.uid)
}
}
}

public func signOut() {
do {
try self.auth.signOut()
self.authenticationState.send(.loggedOut)
log.info("User was successfully signed out.")
self.authenticationState.send(.loggedOut)
AnalyticsManager.logEventLogout()
AnalyticsManager.setUserID(nil)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: false)
AnalyticsManager.clearDefaultEventParameters()
} catch {
log.error("Sign out failed: \(error.localizedDescription)")
self.authenticationError.send(error)
Expand Down Expand Up @@ -147,6 +158,10 @@ public class AuthManager {
} else {
log.info("Account deleted successfully.")
self?.authenticationState.send(.loggedOut)
AnalyticsManager.logEventAccountDelete()
AnalyticsManager.setUserID(nil)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: false)
AnalyticsManager.clearDefaultEventParameters()
}
}
}
Expand Down Expand Up @@ -186,10 +201,19 @@ public class AuthManager {

private func updateAuthState(for user: User?) {
guard let user else {
log.info("⛓️‍💥User is logged out.")
self.authenticationState.send(.loggedOut)
AnalyticsManager.setUserID(nil)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: false)
AnalyticsManager.clearDefaultEventParameters()
return
}

log.info("🔗️ User is logged in.")
self.authenticationState.send(.loggedIn)
self.emailVerificationState.send(user.isEmailVerified)
AnalyticsManager.setUserID(user.uid)
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: true)
AnalyticsManager.setDefaultEventParameterRootOwnerUid(user.uid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ public class AuthManagerViewModel: ObservableObject {
self.showActionRequestAlert = true
}
self.resetErrorMessage()
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: true)
case .loggedOut:
self.resetState()
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: false)
case .unknown:
AnalyticsManager.setUserPropertyUserIsLoggedIn(value: false)
self.resetState()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class RootAccountManager {
}, receiveValue: { [weak self] rootAccount in
guard let self else { return }
self.currentRootAccount.send(rootAccount)
AnalyticsManager.setDefaultEventParameterRootOwnerUid(rootAccount.rootOwnerUid)
})
.store(in: &self.cancellables)
}
Expand Down Expand Up @@ -200,7 +199,6 @@ public class RootAccountManager {
self.dbOps.clearAllListeners()
self.cancellables.forEach { $0.cancel() }
self.cancellables.removeAll()
AnalyticsManager.setDefaultEventParameterRootOwnerUid(nil)
}

// MARK: Internal
Expand Down

0 comments on commit b26a217

Please sign in to comment.