diff --git a/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved index 3b5e304..66cb95f 100644 --- a/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/BasicExample/BasicExample.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,17 +5,17 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/segmentio/analytics-swift", "state" : { - "revision" : "7eeb2abf8452153af056baae5369679589f10936", - "version" : "1.5.9" + "revision" : "338be8f55c83309f10d2cfd0193505117b4ab2d5", + "version" : "1.6.0" } }, { - "identity" : "jsonsafeencoder-swift", + "identity" : "jsonsafeencoding-swift", "kind" : "remoteSourceControl", - "location" : "https://github.com/segmentio/jsonsafeencoder-swift.git", + "location" : "https://github.com/segmentio/jsonsafeencoding-swift.git", "state" : { - "revision" : "8b70dc8c01b7b041912e30e29d2b488a43f782ac", - "version" : "1.0.1" + "revision" : "af6a8b360984085e36c6341b21ecb35c12f47ebd", + "version" : "2.0.0" } }, { diff --git a/Package.resolved b/Package.resolved index 56536d2..faa3f95 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,17 +5,17 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/segmentio/analytics-swift.git", "state" : { - "revision" : "7eeb2abf8452153af056baae5369679589f10936", - "version" : "1.5.9" + "revision" : "338be8f55c83309f10d2cfd0193505117b4ab2d5", + "version" : "1.6.0" } }, { - "identity" : "jsonsafeencoder-swift", + "identity" : "jsonsafeencoding-swift", "kind" : "remoteSourceControl", - "location" : "https://github.com/segmentio/jsonsafeencoder-swift.git", + "location" : "https://github.com/segmentio/jsonsafeencoding-swift.git", "state" : { - "revision" : "8b70dc8c01b7b041912e30e29d2b488a43f782ac", - "version" : "1.0.1" + "revision" : "af6a8b360984085e36c6341b21ecb35c12f47ebd", + "version" : "2.0.0" } }, { diff --git a/Package.swift b/Package.swift index c90c8c2..0b56621 100644 --- a/Package.swift +++ b/Package.swift @@ -19,7 +19,7 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/segmentio/analytics-swift.git", from: "1.5.9") + .package(url: "https://github.com/segmentio/analytics-swift.git", from: "1.6.0") ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/Sources/SegmentAmplitude/AmplitudeSession.swift b/Sources/SegmentAmplitude/AmplitudeSession.swift index 6847027..7fe5a31 100644 --- a/Sources/SegmentAmplitude/AmplitudeSession.swift +++ b/Sources/SegmentAmplitude/AmplitudeSession.swift @@ -48,22 +48,23 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { private var storage = Storage() internal var eventSessionID: Int64 = -1 - @Atomic internal var sessionID: Int64 { + @Atomic internal var sessionID: Int64 = -1 { didSet { storage.write(key: Storage.Constants.previousSessionID, value: sessionID) debugLog("sessionID set to: \(sessionID)") } } - @Atomic internal var lastEventTime: Int64 { + @Atomic internal var lastEventTime: Int64 = -1 { didSet { storage.write(key: Storage.Constants.lastEventTime, value: lastEventTime) } } public init() { - self.sessionID = storage.read(key: Storage.Constants.previousSessionID) ?? -1 - self.lastEventTime = storage.read(key: Storage.Constants.lastEventTime) ?? -1 + _sessionID.set(storage.read(key: Storage.Constants.previousSessionID) ?? -1) + _lastEventTime.set(storage.read(key: Storage.Constants.lastEventTime) ?? -1) + debugLog("startup sessionID = \(sessionID)") } @@ -81,9 +82,9 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { if type != .initial { return } if settings.hasIntegrationSettings(key: key) { - active = true + _active.set(true) } else { - active = false + _active.set(false) } } @@ -118,7 +119,7 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { // if it's a start event, set a new sessionID if eventName == Constants.ampSessionStartEvent { - resetPending = false + _resetPending.set(false) eventSessionID = sessionID debugLog("NewSession = \(eventSessionID)") } @@ -137,7 +138,7 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { workingEvent = trackEvent as? T } - lastEventTime = newTimestamp() + _lastEventTime.set(newTimestamp()) return workingEvent } @@ -152,7 +153,7 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle { public func applicationWillResignActive(application: UIApplication?) { debugLog("Background: \(eventSessionID)") - lastEventTime = newTimestamp() + _lastEventTime.set(newTimestamp()) } } @@ -192,8 +193,8 @@ extension AmplitudeSession { private func startNewSession() { if resetPending { return } - resetPending = true - sessionID = newTimestamp() + _resetPending.set(true) + _sessionID.set(newTimestamp()) if eventSessionID == -1 { // we only wanna do this if we had nothing before, so each // event actually HAS a sessionID of some kind associated. @@ -217,7 +218,7 @@ extension AmplitudeSession { } // we'll consider this our new lastEventTime - lastEventTime = timestamp + _lastEventTime.set(timestamp) // end previous session endSession() // start new session diff --git a/Sources/SegmentAmplitude/Session.swift b/Sources/SegmentAmplitude/Session.swift deleted file mode 100644 index 88dbf05..0000000 --- a/Sources/SegmentAmplitude/Session.swift +++ /dev/null @@ -1,108 +0,0 @@ -// -// SessionTracker.swift -// -// -// Created by Brandon Sneed on 4/22/24. -// - -import Foundation -import Segment - -// MARK: - Amplitude Session Management - -internal class Session { - @Atomic var sessionID: Int64 { - didSet { - storage.write(key: Storage.Constants.previousSessionID, value: sessionID) - } - } - - @Atomic var lastEventTime: Int64 { - didSet { - storage.write(key: Storage.Constants.lastEventTime, value: lastEventTime) - } - } - - private var inForeground: Bool = true - private var storage = Storage() - - init() { - self.sessionID = storage.read(key: Storage.Constants.previousSessionID) ?? -1 - self.lastEventTime = storage.read(key: Storage.Constants.lastEventTime) ?? -1 - } - - - - func startNewSession(analytics: Analytics) { - let timestamp = newTimestamp() - if sessionID >= 0 && (inForeground || withinMinSessionTime(timestamp: timestamp)) { - return - } - // end previous session - analytics.track(name: Constants.ampSessionEndEvent) - // start new session - sessionID = timestamp - analytics.track(name: Constants.ampSessionStartEvent) - } -} - -// MARK: - Session helper functions - -extension Session { - private func newTimestamp() -> Int64 { - return Int64(Date().timeIntervalSince1970 * 1000) - } - - private func withinMinSessionTime(timestamp: Int64) -> Bool { - let minMilisecondsBetweenSessions = 300_000 - let timeDelta = timestamp - self.lastEventTime - return timeDelta < minMilisecondsBetweenSessions - } -} - -// MARK: - Session Storage - -extension Session { - internal class Storage { - internal struct Constants { - static let lastEventID = "last_event_id" - static let previousSessionID = "previous_session_id" - static let lastEventTime = "last_event_time" - } - - private var userDefaults = UserDefaults(suiteName: "com.segment.amplitude.session") - private func isBasicType(value: T?) -> Bool { - var result = false - if value == nil { - result = true - } else { - switch value { - // NSNull is not valid for UserDefaults - //case is NSNull: - // fallthrough - case is Decimal: - fallthrough - case is NSNumber: - fallthrough - case is Bool: - fallthrough - case is String: - result = true - default: - break - } - } - return result - } - - func read(key: String) -> T? { - return userDefaults?.value(forKey: key) as? T - } - - func write(key: String, value: T?) { - if let value, isBasicType(value: value) { - userDefaults?.setValue(value, forKey: key) - } - } - } -}