Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ladislas/feature/1597 analytics use screen view extension #1606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
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 @@
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 @@
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 @@
}
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 Expand Up @@ -445,4 +402,4 @@

#Preview {
MainView()
}

Check warning on line 405 in Apps/LekaApp/Sources/Views/MainView/MainView.swift

View workflow job for this annotation

GitHub Actions / swiftlint

File should contain 400 lines or less: currently contains 405 (file_length)
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,13 +7,14 @@
public extension AnalyticsManager {
static func setUserID(_ userID: String?) {
Analytics.setUserID(userID)
CrashlyticsManager.setUserID(userID)
}

static func setUserPropertyUserIsLoggedIn(value: Bool) {
setUserProperty(value: value.description, forName: "user_is_logged_in")
}

// TODO: (@ladislas) fix property length being too long

Check warning on line 17 in Modules/AnalyticsKit/Sources/Analytics/AnalyticsManager+UserProperties.swift

View workflow job for this annotation

GitHub Actions / swiftlint

TODOs should be resolved ((@ladislas) fix property lengt...) (todo)
static func setUserPropertyCaregiverProfessions(values: [String]) {
let professions = values.joined(separator: ",")

Expand Down
Loading
Loading