Skip to content

Commit

Permalink
Firebase Analytics (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelacaron committed Nov 12, 2023
1 parent c10d01b commit 74a5ca2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Basic-Car-Maintenance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
E58499682ACDDA9A00634660 /* ContributorsProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E58499672ACDDA9A00634660 /* ContributorsProfileView.swift */; };
E584996A2ACDDAFF00634660 /* Contributor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E58499692ACDDAFF00634660 /* Contributor.swift */; };
FF09FC912AB6FF44006BE61A /* AuthenticationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF09FC902AB6FF44006BE61A /* AuthenticationView.swift */; };
FF218EF62B00865F0025A533 /* AnalyticsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF218EF52B00865F0025A533 /* AnalyticsService.swift */; };
FF3DDF522AA4D28F009D91C4 /* DashboardViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF3DDF512AA4D28F009D91C4 /* DashboardViewModel.swift */; };
FF4E82BE2AD39863004949AF /* FirebaseRemoteConfig in Frameworks */ = {isa = PBXBuildFile; productRef = FF4E82BD2AD39863004949AF /* FirebaseRemoteConfig */; };
FF4E82C02AD39863004949AF /* FirebaseRemoteConfigSwift in Frameworks */ = {isa = PBXBuildFile; productRef = FF4E82BF2AD39863004949AF /* FirebaseRemoteConfigSwift */; };
Expand Down Expand Up @@ -126,6 +127,7 @@
FF0813572AD0A92700910EFA /* Widget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Widget.xcconfig; sourceTree = "<group>"; };
FF098EFA2AB3424E003EC0FE /* Basic-Car-Maintenance.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Basic-Car-Maintenance.xcconfig"; sourceTree = SOURCE_ROOT; };
FF09FC902AB6FF44006BE61A /* AuthenticationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationView.swift; sourceTree = "<group>"; };
FF218EF52B00865F0025A533 /* AnalyticsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsService.swift; sourceTree = "<group>"; };
FF3DDF512AA4D28F009D91C4 /* DashboardViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashboardViewModel.swift; sourceTree = "<group>"; };
FF5D13A32A86C2D600BC9BD6 /* Basic-Car-Maintenance.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Basic-Car-Maintenance.app"; sourceTree = BUILT_PRODUCTS_DIR; };
FF5D13A62A86C2D600BC9BD6 /* BasicCarMaintenanceApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicCarMaintenanceApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -392,6 +394,7 @@
FF755B452A90969D00F49A13 /* Bundle+extension.swift */,
154984A92AD9CAEE0015594C /* Constants.swift */,
637505472AEFFBAC00AA5D0B /* FirebaseAnalytics+Extension.swift */,
FF218EF52B00865F0025A533 /* AnalyticsService.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -713,6 +716,7 @@
FFBFE0932A98F212000A9BEB /* Vehicle.swift in Sources */,
FF5D13A72A86C2D600BC9BD6 /* BasicCarMaintenanceApp.swift in Sources */,
8014A4D12AD76034005B51F6 /* ChooseAppIconView.swift in Sources */,
FF218EF62B00865F0025A533 /* AnalyticsService.swift in Sources */,
FF748B5E2AB3589C004748A5 /* AuthenticationViewModel.swift in Sources */,
FF755B462A90969D00F49A13 /* Bundle+extension.swift in Sources */,
FFAA56ED2AC8905C000120EE /* Documentation.docc in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
ReferencedContainer = "container:Basic-Car-Maintenance.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-FIRDebugEnabled"
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class DashboardViewModel {
showAddErrorAlert.toggle()
errorMessage = error.localizedDescription
}

AnalyticsService.shared.logEvent(.maintenanceCreate)
}
}

Expand Down Expand Up @@ -107,6 +109,9 @@ class DashboardViewModel {
errorMessage = error.localizedDescription
}
}

AnalyticsService.shared.logEvent(.maintenanceUpdate)

await self.getMaintenanceEvents()
}

Expand All @@ -130,6 +135,8 @@ class DashboardViewModel {
showErrorAlert.toggle()
errorMessage = error.localizedDescription
}

AnalyticsService.shared.logEvent(.maintenanceDelete)
}

/// Fetches the user's vehicles from Firestore based on their unique user ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class OdometerViewModel {
.firestore()
.collection("odometer_readings")
.addDocument(from: readingToAdd)

AnalyticsService.shared.logEvent(.odometerCreate)
}
}

Expand All @@ -50,6 +52,8 @@ class OdometerViewModel {
if let eventIndex = readings.firstIndex(of: reading) {
readings.remove(at: eventIndex)
}

AnalyticsService.shared.logEvent(.odometerDelete)
}

func getOdometerReadings() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ final class AuthenticationViewModel {
print("User is signed in")
if let user = Auth.auth().currentUser {
self.user = user
AnalyticsService.shared.setUserID(user.uid)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ final class SettingsViewModel {
} catch {
throw error
}

AnalyticsService.shared.logEvent(.vehicleCreate)
}
}

Expand Down Expand Up @@ -129,5 +131,7 @@ final class SettingsViewModel {
} catch {
throw error
}

AnalyticsService.shared.logEvent(.vehicleDelete)
}
}
41 changes: 41 additions & 0 deletions Basic-Car-Maintenance/Shared/Utilities/AnalyticsService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// AnalyticsService.swift
// Basic-Car-Maintenance
//
// Created by Mikaela Caron on 11/11/23.
//

import Foundation
import FirebaseAnalytics

class AnalyticsService {
private init() { }

static let shared = AnalyticsService()

/// Log an `AnalyticEvent` that happened
/// - Parameter event: The event that happened
/// - Parameter parameters: Extra parameters for more information about this event, if needed
func logEvent(_ event: AnalyticEvent, with parameters: [String: String] = [:]) {
Analytics.logEvent(event.rawValue, parameters: parameters)
}

func setUserID(_ id: String) {
Analytics.setUserID(id)
}
}

enum AnalyticEvent: String {

case maintenanceCreate = "maintenance_create"
case maintenanceUpdate = "maintenance_update"
case maintenanceDelete = "maintenance_delete"

case odometerCreate = "odometer_create"
case odometerUpdate = "odometer_update"
case odometerDelete = "odometer_delete"

case vehicleCreate = "vehicle_create"
case vehicleUpdate = "vehicle_update"
case vehicleDelete = "vehicle_delete"
}

0 comments on commit 74a5ca2

Please sign in to comment.