-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improvements to app lifecycle monitoring (#243)
- Loading branch information
Showing
3 changed files
with
114 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,69 @@ | ||
import Foundation | ||
|
||
public class DefaultEventUtils { | ||
|
||
private static var instanceNamesThatSentAppUpdatedInstalled: Set<String> = [] | ||
|
||
private weak var amplitude: Amplitude? | ||
|
||
public init(amplitude: Amplitude) { | ||
self.amplitude = amplitude | ||
} | ||
|
||
public func trackAppUpdatedInstalledEvent() { | ||
guard let amplitude = amplitude else { | ||
return | ||
} | ||
|
||
let info = Bundle.main.infoDictionary | ||
let currentBuild = info?["CFBundleVersion"] as? String | ||
let currentVersion = info?["CFBundleShortVersionString"] as? String | ||
let previousBuild: String? = amplitude?.storage.read(key: StorageKey.APP_BUILD) | ||
let previousVersion: String? = amplitude?.storage.read(key: StorageKey.APP_VERSION) | ||
|
||
if amplitude?.configuration.autocapture.contains(.appLifecycles) ?? false { | ||
let lastEventTime: Int64? = amplitude?.storage.read(key: StorageKey.LAST_EVENT_TIME) | ||
if lastEventTime == nil { | ||
self.amplitude?.track(eventType: Constants.AMP_APPLICATION_INSTALLED_EVENT, eventProperties: [ | ||
Constants.AMP_APP_BUILD_PROPERTY: currentBuild ?? "", | ||
Constants.AMP_APP_VERSION_PROPERTY: currentVersion ?? "", | ||
]) | ||
} else if currentBuild != previousBuild { | ||
self.amplitude?.track(eventType: Constants.AMP_APPLICATION_UPDATED_EVENT, eventProperties: [ | ||
Constants.AMP_APP_BUILD_PROPERTY: currentBuild ?? "", | ||
Constants.AMP_APP_VERSION_PROPERTY: currentVersion ?? "", | ||
Constants.AMP_APP_PREVIOUS_BUILD_PROPERTY: previousBuild ?? "", | ||
Constants.AMP_APP_PREVIOUS_VERSION_PROPERTY: previousVersion ?? "", | ||
]) | ||
} | ||
} | ||
let previousBuild: String? = amplitude.storage.read(key: StorageKey.APP_BUILD) | ||
let previousVersion: String? = amplitude.storage.read(key: StorageKey.APP_VERSION) | ||
|
||
if currentBuild != previousBuild { | ||
try? amplitude?.storage.write(key: StorageKey.APP_BUILD, value: currentBuild) | ||
try? amplitude.storage.write(key: StorageKey.APP_BUILD, value: currentBuild) | ||
} | ||
if currentVersion != previousVersion { | ||
try? amplitude?.storage.write(key: StorageKey.APP_VERSION, value: currentVersion) | ||
try? amplitude.storage.write(key: StorageKey.APP_VERSION, value: currentVersion) | ||
} | ||
|
||
guard amplitude.configuration.autocapture.contains(.appLifecycles), | ||
!Self.instanceNamesThatSentAppUpdatedInstalled.contains(amplitude.configuration.instanceName) else { | ||
return | ||
} | ||
// Only send one app installed / updated event per instance name, no matter how many times we are | ||
// reinitialized | ||
Self.instanceNamesThatSentAppUpdatedInstalled.insert(amplitude.configuration.instanceName) | ||
|
||
if previousBuild == nil || previousVersion == nil { | ||
amplitude.track(eventType: Constants.AMP_APPLICATION_INSTALLED_EVENT, eventProperties: [ | ||
Constants.AMP_APP_BUILD_PROPERTY: currentBuild ?? "", | ||
Constants.AMP_APP_VERSION_PROPERTY: currentVersion ?? "", | ||
]) | ||
} else if currentBuild != previousBuild || currentVersion != previousVersion { | ||
amplitude.track(eventType: Constants.AMP_APPLICATION_UPDATED_EVENT, eventProperties: [ | ||
Constants.AMP_APP_BUILD_PROPERTY: currentBuild ?? "", | ||
Constants.AMP_APP_VERSION_PROPERTY: currentVersion ?? "", | ||
Constants.AMP_APP_PREVIOUS_BUILD_PROPERTY: previousBuild ?? "", | ||
Constants.AMP_APP_PREVIOUS_VERSION_PROPERTY: previousVersion ?? "", | ||
]) | ||
} | ||
} | ||
|
||
func trackAppOpenedEvent(fromBackground: Bool = false) { | ||
guard let amplitude = amplitude, | ||
amplitude.configuration.autocapture.contains(.appLifecycles) else { | ||
return | ||
} | ||
|
||
let info = Bundle.main.infoDictionary | ||
let currentBuild = info?["CFBundleVersion"] as? String | ||
let currentVersion = info?["CFBundleShortVersionString"] as? String | ||
self.amplitude?.track(eventType: Constants.AMP_APPLICATION_OPENED_EVENT, eventProperties: [ | ||
Constants.AMP_APP_BUILD_PROPERTY: currentBuild ?? "", | ||
Constants.AMP_APP_VERSION_PROPERTY: currentVersion ?? "", | ||
Constants.AMP_APP_FROM_BACKGROUND_PROPERTY: fromBackground, | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters