From 6b30a1beacaf5e41dae2690e3b99da0a0673b1e1 Mon Sep 17 00:00:00 2001 From: Cesar de la Vega Date: Wed, 13 Nov 2024 15:24:01 +0100 Subject: [PATCH 1/2] add appSessionID to PaywallEventsManager --- Sources/Events/StoredEvent.swift | 18 +++++++++++++++++- .../Paywalls/Events/PaywallEventsManager.swift | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/Events/StoredEvent.swift b/Sources/Events/StoredEvent.swift index 79d7bd3d6a..1b72a9b53f 100644 --- a/Sources/Events/StoredEvent.swift +++ b/Sources/Events/StoredEvent.swift @@ -18,9 +18,10 @@ struct StoredEvent { private(set) var encodedEvent: AnyEncodable private(set) var userID: String + private(set) var appSessionID: UUID private(set) var feature: Feature - init?(event: T, userID: String, feature: Feature) { + init?(event: T, userID: String, appSessionID: UUID, feature: Feature) { guard let data = try? JSONEncoder.default.encode(value: event), let dictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil @@ -28,6 +29,7 @@ struct StoredEvent { self.encodedEvent = AnyEncodable(dictionary) self.userID = userID + self.appSessionID = appSessionID self.feature = feature } @@ -49,6 +51,7 @@ extension StoredEvent: Codable { case encodedEvent = "event" case userID = "userId" + case appSessionID = "appSessionID" case feature } @@ -64,6 +67,19 @@ extension StoredEvent: Codable { } else { self.feature = .paywalls } + if let appSessionID = try container.decodeIfPresent(UUID.self, forKey: .appSessionID) { + self.appSessionID = appSessionID + } else { + // Backward compatibility for PaywallEvents from before we started storing the user session ID + // Just use the paywall event's session ID + // Or generate a new one in the worst case + if let eventData = encodedEvent.value as? [String: Any], + let paywallEvent: PaywallEvent = try JSONDecoder.default.decode(dictionary: eventData) { + self.appSessionID = paywallEvent.data.sessionIdentifier + } else { + self.appSessionID = UUID() + } + } } } diff --git a/Sources/Paywalls/Events/PaywallEventsManager.swift b/Sources/Paywalls/Events/PaywallEventsManager.swift index 4dc096f871..9b763bac96 100644 --- a/Sources/Paywalls/Events/PaywallEventsManager.swift +++ b/Sources/Paywalls/Events/PaywallEventsManager.swift @@ -31,6 +31,7 @@ actor PaywallEventsManager: PaywallEventsManagerType { private let internalAPI: InternalAPI private let userProvider: CurrentUserProvider private let store: PaywallEventStoreType + private var appSessionID: UUID private var flushInProgress = false @@ -42,11 +43,17 @@ actor PaywallEventsManager: PaywallEventsManagerType { self.internalAPI = internalAPI self.userProvider = userProvider self.store = store + self.appSessionID = UUID() + } + + func resetAppSessionID() { + self.appSessionID = UUID() } func track(paywallEvent: PaywallEvent) async { guard let event: StoredEvent = .init(event: AnyEncodable(paywallEvent), userID: self.userProvider.currentAppUserID, + appSessionID: self.appSessionID, feature: .paywalls) else { Logger.error(Strings.paywalls.event_cannot_serialize) return From fe51ee6cadd6eff5ec93fe2e1d671c815658c01b Mon Sep 17 00:00:00 2001 From: Cesar de la Vega Date: Wed, 13 Nov 2024 17:51:20 +0100 Subject: [PATCH 2/2] resetAppSessionID --- Sources/Paywalls/Events/PaywallEventsManager.swift | 2 ++ Sources/Purchasing/Purchases/Purchases.swift | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/Sources/Paywalls/Events/PaywallEventsManager.swift b/Sources/Paywalls/Events/PaywallEventsManager.swift index 9b763bac96..6aada98ea1 100644 --- a/Sources/Paywalls/Events/PaywallEventsManager.swift +++ b/Sources/Paywalls/Events/PaywallEventsManager.swift @@ -23,6 +23,8 @@ protocol PaywallEventsManagerType { @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) func flushEvents(count: Int) async throws -> Int + func resetAppSessionID() async + } @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) diff --git a/Sources/Purchasing/Purchases/Purchases.swift b/Sources/Purchasing/Purchases/Purchases.swift index 7723add6c1..21eedf39be 100644 --- a/Sources/Purchasing/Purchases/Purchases.swift +++ b/Sources/Purchasing/Purchases/Purchases.swift @@ -820,6 +820,10 @@ public extension Purchases { self.systemInfo.isApplicationBackgrounded { isAppBackgrounded in self.updateOfferingsCache(isAppBackgrounded: isAppBackgrounded) } + + Task { + await self.paywallEventsManager?.resetAppSessionID() + } } } @@ -853,6 +857,10 @@ public extension Purchases { return } + Task { + await self.paywallEventsManager?.resetAppSessionID() + } + self.updateAllCaches { completion?($0.value, $0.error) }