Skip to content

Commit

Permalink
fix: Improved keys and remove typo (#127)
Browse files Browse the repository at this point in the history
* fix: Improved keys and remove typo

* fix: lint fix in test
  • Loading branch information
fabriziodemaria committed May 27, 2024
1 parent a49a393 commit f31fe85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Sources/Confidence/ConfidenceAppLifecycleProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
UIApplication.didBecomeActiveNotification
]

private static var versionNameKey = "CONFIDENCE_VERSION_NAME_KEY"
private static var buildNameKey = "CONFIDENCE_VERSIONN_KEY"
private let appLaunchedEventName = "app-launched"
// Storage Keys
private static var userDefaultVersionNameKey = "CONFIDENCE_VERSION_NAME_KEY"
private static var userDefaultBuildNameKey = "CONFIDENCE_BUILD_NUMBER_KEY"
// Context Keys
private static var versionNameContextKey = "app_version"
private static var buildNumberContextKey = "app_build"
private static var isForegroundContextKey = "is_foreground"
// Event Names
private static let appLaunchedEventName = "app-launched"
private static let appInstalledEventName = "app-installed"
private static let appUpdatedEventName = "app-updated"


public init() {
for notification in appNotifications {
Expand All @@ -30,8 +39,8 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
let currentVersion: String = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
let currentBuild: String = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
let context: ConfidenceStruct = [
"version": .init(string: currentVersion),
"build": .init(string: currentBuild)
Self.versionNameContextKey: .init(string: currentVersion),
Self.buildNumberContextKey: .init(string: currentBuild)
]

self.currentProducedContext.send(context)
Expand All @@ -52,28 +61,28 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
}

private func track(eventName: String) {
let previousBuild: String? = UserDefaults.standard.string(forKey: Self.buildNameKey)
let previousVersion: String? = UserDefaults.standard.string(forKey: Self.versionNameKey)
let previousBuild: String? = UserDefaults.standard.string(forKey: Self.userDefaultBuildNameKey)
let previousVersion: String? = UserDefaults.standard.string(forKey: Self.userDefaultVersionNameKey)

let currentVersion: String = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
let currentBuild: String = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""

let message: ConfidenceStruct = [
"version": .init(string: currentVersion),
"build": .init(string: currentBuild)
Self.versionNameContextKey: .init(string: currentVersion),
Self.buildNumberContextKey: .init(string: currentBuild)
]

if eventName == self.appLaunchedEventName {
if eventName == Self.appLaunchedEventName {
if previousBuild == nil && previousVersion == nil {
events.send(Event(name: "app-installed", message: message))
events.send(Event(name: ConfidenceAppLifecycleProducer.appInstalledEventName, message: message))
} else if previousBuild != currentBuild || previousVersion != currentVersion {
events.send(Event(name: "app-updated", message: message))
events.send(Event(name: ConfidenceAppLifecycleProducer.appUpdatedEventName, message: message))
}
}
events.send(Event(name: eventName, message: message))

UserDefaults.standard.setValue(currentVersion, forKey: Self.versionNameKey)
UserDefaults.standard.setValue(currentBuild, forKey: Self.buildNameKey)
UserDefaults.standard.setValue(currentVersion, forKey: Self.userDefaultVersionNameKey)
UserDefaults.standard.setValue(currentBuild, forKey: Self.userDefaultBuildNameKey)
}

private func updateContext(isForeground: Bool) {
Expand All @@ -82,7 +91,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
return
}
var context = self.currentProducedContext.value
context.updateValue(.init(boolean: isForeground), forKey: "is_foreground")
context.updateValue(.init(boolean: isForeground), forKey: Self.isForegroundContextKey)
self.currentProducedContext.send(context)
}
}
Expand All @@ -100,7 +109,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
case UIApplication.willEnterForegroundNotification:
updateContext(isForeground: true)
case UIApplication.didBecomeActiveNotification:
track(eventName: appLaunchedEventName)
track(eventName: Self.appLaunchedEventName)
default:
break
}
Expand Down
1 change: 1 addition & 0 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final class EventSenderEngineTest: XCTestCase {
writeQueue = DispatchQueue(label: "ConfidenceWriteQueue")
uploaderMock = EventUploaderMock()
storageMock = EventStorageMock()
try await super.setUp()
}

func testPayloadOnEmit() throws {
Expand Down

0 comments on commit f31fe85

Please sign in to comment.