Skip to content

Commit

Permalink
🔀️ Merge branch 'ladislas/feature/1597-analytics-use-screen-view-exte…
Browse files Browse the repository at this point in the history
…nsion' into develop
  • Loading branch information
ladislas committed Nov 29, 2024
2 parents cffa0d3 + 968d921 commit 235014d
Show file tree
Hide file tree
Showing 28 changed files with 365 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extension AccountCreationProcess {
self.selectedTab = .carereceiverCreation
}
})
.logEventScreenView(screenName: "caregiver_create", context: .context("account_creation_sheet"))
.navigationBarTitleDisplayMode(.inline)
.interactiveDismissDisabled()
}
Expand Down
101 changes: 29 additions & 72 deletions Apps/LekaApp/Sources/Views/MainView/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,69 +157,47 @@ struct MainView: View {
switch self.navigation.selectedCategory {
case .home:
CategoryHome()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_home")
}
.logEventScreenView(screenName: "home", context: .splitView)

case .search:
CategorySearchView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_search")
}
.logEventScreenView(screenName: "search", context: .splitView)

case .resourcesFirstSteps:
CategoryResourcesFirstStepsView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_resources_first_steps")
}
.logEventScreenView(screenName: "resources_first_steps", context: .splitView)

case .resourcesVideo:
CategoryResourcesVideosView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_resources_video")
}
.logEventScreenView(screenName: "resources_video", context: .splitView)

case .resourcesDeepDive:
CategoryResourcesDeepDiveView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_resources_deep_dive")
}
.logEventScreenView(screenName: "resources_deep_dive", context: .splitView)

case .curriculums:
CategoryCurriculumsView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_curriculums")
}
.logEventScreenView(screenName: "curriculums", context: .splitView)

case .educationalGames:
CategoryEducationalGamesView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_educational_games")
}
.logEventScreenView(screenName: "educational_games", context: .splitView)

case .stories:
CategoryStoriesView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_stories")
}
.logEventScreenView(screenName: "stories", context: .splitView)

case .gamepads:
CategoryGamepadsView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_gamepads")
}
.logEventScreenView(screenName: "gamepads", context: .splitView)

case .caregivers:
CaregiverList()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_caregivers")
}
.logEventScreenView(screenName: "caregivers", context: .splitView)

case .carereceivers:
CarereceiverList()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_carereceivers")
}
.logEventScreenView(screenName: "carereceivers", context: .splitView)

// ? DEVELOPER_MODE + TESTFLIGHT_BUILD
case .allPublishedActivities:
Expand Down Expand Up @@ -247,21 +225,15 @@ struct MainView: View {

case .libraryCurriculums:
CategoryLibraryView(category: .libraryCurriculums)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_library_curriculums")
}
.logEventScreenView(screenName: "library_curriculums", context: .splitView)

case .libraryActivities:
CategoryLibraryView(category: .libraryActivities)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_library_activities")
}
.logEventScreenView(screenName: "library_activities", context: .splitView)

case .libraryStories:
CategoryLibraryView(category: .libraryStories)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_category_library_stories")
}
.logEventScreenView(screenName: "library_stories", context: .splitView)

case .none:
Text(l10n.MainView.Sidebar.CategoryLabel.home)
Expand All @@ -279,19 +251,15 @@ struct MainView: View {
switch content {
case .welcomeView:
WelcomeView()
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_welcome")
}
.logEventScreenView(screenName: "welcome", context: .fullScreenCover)

case let .activityView(carereceivers):
ActivityView(activity: self.navigation.currentActivity!, reinforcer: carereceivers.first?.reinforcer ?? .rainbow)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_activity")
}
.logEventScreenView(screenName: "activity", context: .fullScreenCover)

case .storyView:
StoryView(story: self.navigation.currentStory!)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_story")
}
.logEventScreenView(screenName: "story", context: .fullScreenCover)
}
}
}
Expand All @@ -302,34 +270,28 @@ struct MainView: View {
switch content {
case .robotConnection:
RobotConnectionView(viewModel: RobotConnectionViewModel())
.logEventScreenView(screenName: "robot_connection", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_robot_connection")
}

case .settings:
SettingsView()
.logEventScreenView(screenName: "settings", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_settings")
}

case .editCaregiver:
EditCaregiverView(caregiver: self.caregiverManagerViewModel.currentCaregiver!)
.logEventScreenView(screenName: "caregiver_edit", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_edit_caregiver")
}

case .createCaregiver:
CreateCaregiverView()
.logEventScreenView(screenName: "caregiver_create", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_create_caregiver")
}

case .caregiverPicker:
CaregiverPicker()
.logEventScreenView(screenName: "caregiver_picker", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_caregiver_picker")
}
.onDisappear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
if case .appUpdateAvailable = UpdateManager.shared.appUpdateStatus {
Expand Down Expand Up @@ -361,10 +323,8 @@ struct MainView: View {
self.navigation.fullScreenCoverContent = .storyView(carereceivers: [])
}
})
.logEventScreenView(screenName: "carereceiver_picker", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_carereceiver_picker")
}
}
}
}
Expand All @@ -374,9 +334,6 @@ struct MainView: View {
}
self.persistentDataManager.checkInactivity()
}
.onAppear {
AnalyticsManager.logEventScreenView(screenName: "view_main_navigation_split_view")
}
.onChange(of: self.scenePhase) { newPhase in
guard self.authManagerViewModel.userAuthenticationState == .loggedIn else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct CarereceiverList: View {
.sheet(isPresented: self.$isCarereceiverCreationPresented) {
NavigationStack {
CreateCarereceiverView()
.logEventScreenView(screenName: "carereceiver_create", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct CarereceiverView: View {
.sheet(isPresented: self.$isEditCarereceiverViewPresented) {
NavigationStack {
EditCarereceiverView(modifiedCarereceiver: self.$carereceiver)
.logEventScreenView(screenName: "carereceiver_edit", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ extension l10n {
CreateCarereceiverView(onClose: {
print("Care receiver creation canceled")
})
// , onCreated: {
// print("Carereceiver \($0.username) created")
// })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct CaregiverList: View {
.sheet(isPresented: self.$isCaregiverCreationPresented) {
NavigationStack {
CreateCaregiverView()
.logEventScreenView(screenName: "caregiver_create", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
}
}
Expand Down
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 LocalizationKit
import SwiftUI
Expand Down Expand Up @@ -35,6 +34,7 @@ struct CaregiverPicker: View {
.sheet(isPresented: self.$isCaregiverCreationPresented) {
NavigationStack {
CreateCaregiverView()
.logEventScreenView(screenName: "caregiver_create", context: .sheet)
.navigationBarTitleDisplayMode(.inline)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import AnalyticsKit
import Combine
import Foundation
import LocalizationKit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

import AnalyticsKit
import Combine

public class RootAccountManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,83 @@ public extension AnalyticsManager {

logEvent(.screenView, parameters: params)
}
}

public extension View {
func logEventScreenView(screenName: String, screenClass: String? = nil, parameters: [String: Any] = [:]) -> some View {
let screenClass = screenClass ?? String(describing: type(of: self))
return self.onAppear {
AnalyticsManager.logEventScreenView(screenName: screenName, screenClass: screenClass, parameters: parameters)
enum ScreenViewContext {
case splitView
case sheet
case fullScreenCover
case context(String)

// MARK: Internal

var description: String {
switch self {
case .splitView:
"splitview"
case .sheet:
"sheet"
case .fullScreenCover:
"fullscreen"
case let .context(value):
"\(value)"
}
}
}
}

// MARK: - MyCustomView
// MARK: - AnalyticsLogScreenViewViewModifier

struct AnalyticsLogScreenViewViewModifier: ViewModifier {
// MARK: Lifecycle

init(
screenClass: String,
screenName: String,
context: AnalyticsManager.ScreenViewContext? = nil,
parameters: [String: Any] = [:]
) {
self.screenName = screenName
self.screenClass = screenClass
self.context = context
self.parameters = parameters
}

// MARK: Internal

let screenClass: String
let screenName: String
let context: AnalyticsManager.ScreenViewContext?
let parameters: [String: Any]

func body(content: Content) -> some View {
content
.onAppear {
let params: [String: Any] = [
"lk_context": context?.description ?? NSNull(),
].merging(self.parameters) { _, new in new }

AnalyticsManager.logEventScreenView(screenName: self.screenName, screenClass: self.screenClass, parameters: params)
}
}
}

public extension View {
func logEventScreenView(
screenClass: String? = nil,
screenName: String,
context: AnalyticsManager.ScreenViewContext? = nil,
parameters: [String: Any] = [:]
) -> some View {
self.modifier(
AnalyticsLogScreenViewViewModifier(
screenClass: screenClass ?? String(describing: type(of: self)),
screenName: screenName,
context: context,
parameters: parameters
)
)
}
}

#Preview {
struct MyCustomView: View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FirebaseAnalytics
public extension AnalyticsManager {
static func setUserID(_ userID: String?) {
Analytics.setUserID(userID)
CrashlyticsManager.setUserID(userID)
}

static func setUserPropertyUserIsLoggedIn(value: Bool) {
Expand Down
Loading

0 comments on commit 235014d

Please sign in to comment.