Skip to content

Commit cb5870a

Browse files
committed
Fix bolus and carb entry analytics
1 parent 4307256 commit cb5870a

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

Loop/Managers/AnalyticsServicesManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,16 @@ final class AnalyticsServicesManager {
164164
}
165165

166166

167-
func didAddCarbsFromWatch() {
168-
logEvent("Carb entry created", withProperties: ["source" : "Watch"], outOfSession: true)
167+
func didAddCarbs(source: String, amount: Double, inSession: Bool = false) {
168+
logEvent("Carb entry created", withProperties: ["source" : source, "amount": "\(amount)"], outOfSession: inSession)
169169
}
170170

171171
func didRetryBolus() {
172172
logEvent("Bolus Retry")
173173
}
174174

175-
func didSetBolusFromWatch(_ units: Double) {
176-
logEvent("Bolus set", withProperties: ["source" : "Watch"], outOfSession: true)
175+
func didBolus(source: String, units: Double, inSession: Bool = false) {
176+
logEvent("Bolus set", withProperties: ["source" : source, "units": "\(units)"], outOfSession: true)
177177
}
178178

179179
func didFetchNewCGMData() {

Loop/Managers/DeviceDataManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ extension DeviceDataManager {
14851485
UIApplication.shared.endBackgroundTask(backgroundTask)
14861486
}
14871487
completion(result)
1488+
self.analyticsServicesManager.didAddCarbs(source: "Remote", amount: carbEntry.quantity.doubleValue(for: .gram()))
14881489
}
14891490
}
14901491

@@ -1503,6 +1504,7 @@ extension DeviceDataManager {
15031504
UIApplication.shared.endBackgroundTask(backgroundTask)
15041505
}
15051506
completion(error)
1507+
self.analyticsServicesManager.didBolus(source: "Remote", units: units)
15061508
}
15071509
}
15081510

Loop/Managers/WatchDataManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ final class WatchDataManager: NSObject {
386386

387387
deviceManager.enactBolus(units: bolus.value, activationType: bolus.activationType) { (error) in
388388
if error == nil {
389-
self.deviceManager.analyticsServicesManager.didSetBolusFromWatch(bolus.value)
389+
self.deviceManager.analyticsServicesManager.didBolus(source: "Watch", units: bolus.value)
390390
}
391391

392392
// When we've successfully started the bolus, send a new context with our new prediction
@@ -401,7 +401,7 @@ final class WatchDataManager: NSObject {
401401
switch result {
402402
case .success(let storedCarbEntry):
403403
dosingDecision.carbEntry = storedCarbEntry
404-
self.deviceManager.analyticsServicesManager.didAddCarbsFromWatch()
404+
self.deviceManager.analyticsServicesManager.didAddCarbs(source: "Watch", amount: storedCarbEntry.quantity.doubleValue(for: .gram()))
405405
enactBolus()
406406
case .failure(let error):
407407
self.log.error("%{public}@", String(describing: error))

Loop/View Controllers/CarbEntryViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ final class CarbEntryViewController: LoopChartsTableViewController, Identifiable
513513
await viewModel.generateRecommendationAndStartObserving()
514514
}
515515

516+
viewModel.analyticsServicesManager = deviceManager.analyticsServicesManager
517+
516518
let bolusEntryView = BolusEntryView(viewModel: viewModel).environmentObject(deviceManager.displayGlucoseUnitObservable)
517519

518520
// After confirming a bolus, pop back to this controller's predecessor, i.e. all the way back out of the carb flow.

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ final class StatusTableViewController: LoopChartsTableViewController {
12961296
Task { @MainActor in
12971297
await viewModel.generateRecommendationAndStartObserving()
12981298
}
1299+
viewModel.analyticsServicesManager = deviceManager.analyticsServicesManager
12991300
let bolusEntryView = BolusEntryView(viewModel: viewModel).environmentObject(deviceManager.displayGlucoseUnitObservable)
13001301
hostingController = DismissibleHostingController(rootView: bolusEntryView, isModalInPresentation: false)
13011302
}

Loop/View Models/BolusEntryViewModel.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ final class BolusEntryViewModel: ObservableObject {
149149
private let debounceIntervalMilliseconds: Int
150150
private let uuidProvider: () -> String
151151
private let carbEntryDateFormatter: DateFormatter
152+
153+
var analyticsServicesManager: AnalyticsServicesManager?
152154

153155
// MARK: - Initialization
154156

@@ -400,6 +402,7 @@ final class BolusEntryViewModel: ObservableObject {
400402
}
401403
if let storedCarbEntry = await saveCarbEntry(carbEntry, replacingEntry: originalCarbEntry) {
402404
self.dosingDecision.carbEntry = storedCarbEntry
405+
self.analyticsServicesManager?.didAddCarbs(source: "Phone", amount: storedCarbEntry.quantity.doubleValue(for: .gram()))
403406
} else {
404407
self.presentAlert(.carbEntryPersistenceFailure)
405408
return false
@@ -413,7 +416,9 @@ final class BolusEntryViewModel: ObservableObject {
413416

414417
if amountToDeliver > 0 {
415418
savedPreMealOverride = nil
416-
delegate.enactBolus(units: amountToDeliver, activationType: activationType, completion: { _ in })
419+
delegate.enactBolus(units: amountToDeliver, activationType: activationType, completion: { _ in
420+
self.analyticsServicesManager?.didBolus(source: "Phone", units: amountToDeliver)
421+
})
417422
}
418423
return true
419424
}

0 commit comments

Comments
 (0)