diff --git a/ViteMaDose/Helpers/Utils/AppAnalytics.swift b/ViteMaDose/Helpers/Utils/AppAnalytics.swift index 16d23e28..07e9cac0 100644 --- a/ViteMaDose/Helpers/Utils/AppAnalytics.swift +++ b/ViteMaDose/Helpers/Utils/AppAnalytics.swift @@ -8,7 +8,7 @@ import Foundation import FirebaseAnalytics -struct AppAnalytics { +enum AppAnalytics { enum ScreenName: String { case home = "home" @@ -52,4 +52,31 @@ struct AppAnalytics { ]) } + static func trackSearchEvent( + searchResult: LocationSearchResult, + appointmentsCount: Int, + availableCentresCount: Int, + unAvailableCentresCount: Int, + sortOption: CentresListSortOption + ) { + let eventName = searchResult.coordinates == nil ? "search_by_departement" : "search_by_commune" + Analytics.logEvent(eventName, parameters: [ + "search_departement": "\(searchResult.departmentCode) - \(searchResult.name)", + "search_nb_doses": appointmentsCount as NSNumber, + "search_nb_lieu_vaccination": availableCentresCount as NSNumber, + "search_nb_lieu_vaccination_inactive": unAvailableCentresCount as NSNumber, + "search_filter_type": sortOption.analyticsValue + ]) + } +} + +private extension CentresListSortOption { + var analyticsValue: String { + switch self { + case .closest: + return "au plus proche" + case .fastest: + return "au plus tot" + } + } } diff --git a/ViteMaDose/Helpers/Utils/UserDefaultsUtils.swift b/ViteMaDose/Helpers/Utils/UserDefaultsUtils.swift index d2e7c176..600097c1 100644 --- a/ViteMaDose/Helpers/Utils/UserDefaultsUtils.swift +++ b/ViteMaDose/Helpers/Utils/UserDefaultsUtils.swift @@ -50,10 +50,11 @@ extension UserDefaults { guard case let .success(results) = searchResultData?.decode([LocationSearchResult].self) else { return [] } - return results + return results.unique(by: \.formattedName) } set { - guard let encoded = try? Self.encoder.encode(newValue.uniqued()) else { + let results = newValue.unique(by: \.formattedName) + guard let encoded = try? Self.encoder.encode(results) else { return } setValue(encoded, forKey: Key.lastSearchResults.rawValue) diff --git a/ViteMaDose/ViewModels/CentresList/CentresListViewModel.swift b/ViteMaDose/ViewModels/CentresList/CentresListViewModel.swift index 8bc64f74..85e7ac90 100644 --- a/ViteMaDose/ViewModels/CentresList/CentresListViewModel.swift +++ b/ViteMaDose/ViewModels/CentresList/CentresListViewModel.swift @@ -123,6 +123,14 @@ class CentresListViewModel { let centresCells = createVaccinationCentreCellsFor(for: vaccinationCentresList) let footerText = locationVaccinationCentres.first?.formattedLastUpdated + AppAnalytics.trackSearchEvent( + searchResult: searchResult, + appointmentsCount: availableCentres.allAppointmentsCount, + availableCentresCount: availableCentres.count, + unAvailableCentresCount: unavailableCentres.count, + sortOption: userDefaults.centresListSortOption + ) + delegate?.reloadTableView(with: headingCells, andCentresCells: centresCells, animated: animated) delegate?.reloadTableViewFooter(with: footerText) }