From 380ae5a58985de4bf37fb3f84cc488857167ee63 Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Thu, 15 Feb 2024 15:39:49 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20AutoPropertyDec?= =?UTF-8?q?orator=20to=20improve=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Analytics/AnalyticsTracker.swift | 4 +-- .../Analytics/AutoPropertyDecorator.swift | 31 ++++++++++--------- .../Debugger/Panel/LoggedEvent.swift | 6 ++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Sources/AppcuesKit/Data/Analytics/AnalyticsTracker.swift b/Sources/AppcuesKit/Data/Analytics/AnalyticsTracker.swift index 1bb556f3c..fa562be44 100644 --- a/Sources/AppcuesKit/Data/Analytics/AnalyticsTracker.swift +++ b/Sources/AppcuesKit/Data/Analytics/AnalyticsTracker.swift @@ -228,7 +228,7 @@ extension Activity { sessionID: sessionID.appcuesFormatted, userID: storage.userID, events: [Event(name: name, attributes: update.properties, context: update.context, logger: config.logger)], - profileUpdate: update.eventAutoProperties, + profileUpdate: update.identityAutoProperties, groupID: storage.groupID, userSignature: storage.userSignature, logger: config.logger @@ -240,7 +240,7 @@ extension Activity { sessionID: sessionID.appcuesFormatted, userID: storage.userID, events: [Event(screen: title, attributes: update.properties, context: update.context, logger: config.logger)], - profileUpdate: update.eventAutoProperties, + profileUpdate: update.identityAutoProperties, groupID: storage.groupID, userSignature: storage.userSignature, logger: config.logger diff --git a/Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift b/Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift index f14670227..fb3087e9e 100644 --- a/Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift +++ b/Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift @@ -39,10 +39,23 @@ internal class AutoPropertyDecorator: AnalyticsDecorating { } func decorate(_ tracking: TrackingUpdate) -> TrackingUpdate { + // Early return for group updates + if case let .group(id) = tracking.type { + if id == nil { + // removing from a group should not have any auto props + return tracking + } else { + // group updates only have this single auto prop, so add that and return early + var decorated = tracking + decorated.properties = (decorated.properties ?? [:]).merging(["_lastSeenAt": Date()]) + return decorated + } + } var context = self.contextProperties var decorated = tracking + // Update values switch tracking.type { case let .screen(title): previousScreen = currentScreen @@ -55,20 +68,13 @@ internal class AutoPropertyDecorator: AnalyticsDecorating { sessionLatestUserProperties = [:] currentScreen = nil previousScreen = nil - case .group(nil): - // removing from a group should not have any auto props - return tracking - case .group: - // group updates only have this single auto prop, so add that and return early - decorated.properties = (decorated.properties ?? [:]).merging(["_lastSeenAt": Date()]) - return decorated default: break } let now = Date() - var sessionProperties: [String: Any?] = [ + let sessionProperties: [String: Any?] = [ "userId": storage.userID, "_isAnonymous": storage.isAnonymous, "_localId": storage.deviceID, @@ -76,6 +82,7 @@ internal class AutoPropertyDecorator: AnalyticsDecorating { "_sessionRandomizer": sessionRandomizer, "_currentScreenTitle": currentScreen, "_lastScreenTitle": previousScreen, + "_lastBrowserLanguage": Bundle.main.preferredLocalizations.first, // _lastSeenAt deprecates _updatedAt which can't be entirely removed since it's used for targeting "_lastSeenAt": now, "_updatedAt": now, @@ -83,10 +90,6 @@ internal class AutoPropertyDecorator: AnalyticsDecorating { "_sessionId": appcues?.sessionID?.appcuesFormatted ] - if let bundleLanguageID = Bundle.main.preferredLocalizations.first { - sessionProperties["_lastBrowserLanguage"] = bundleLanguageID - } - // Note: additional (custom) go first, as they may be overwritten by merged system items let merged = config.additionalAutoProperties .merging(sessionLatestUserProperties) @@ -101,7 +104,7 @@ internal class AutoPropertyDecorator: AnalyticsDecorating { sessionLatestUserProperties = tracking.properties?.compactMapValues { $0 } ?? [:] } else { // all other events have auto props within attributes._identity - decorated.eventAutoProperties = merged + decorated.identityAutoProperties = merged } decorated.context = context @@ -149,7 +152,7 @@ extension UIDevice { extension TrackingUpdate { // events have auto props nested inside an _identity object - var eventAutoProperties: [String: Any]? { + var identityAutoProperties: [String: Any]? { get { return properties?["_identity"] as? [String: Any] } diff --git a/Sources/AppcuesKit/Presentation/Debugger/Panel/LoggedEvent.swift b/Sources/AppcuesKit/Presentation/Debugger/Panel/LoggedEvent.swift index 8f05d0431..2d0410f6a 100644 --- a/Sources/AppcuesKit/Presentation/Debugger/Panel/LoggedEvent.swift +++ b/Sources/AppcuesKit/Presentation/Debugger/Panel/LoggedEvent.swift @@ -34,7 +34,7 @@ internal struct LoggedEvent: Identifiable { var groups: [(title: String, items: [Pair])] = [] // flatten the nested `_identity` auto-properties into individual top level items. - let autoProps = (properties["_identity"] as? [String: Any] ?? [:]) + let identityAutoProps = (properties["_identity"] as? [String: Any] ?? [:]) .sortedWithAutoProperties() .map { ($0.key, String(describing: $0.value)) } properties["_identity"] = nil @@ -71,8 +71,8 @@ internal struct LoggedEvent: Identifiable { groups.append(("Interaction Data: Form Response", formResponse)) } - if !autoProps.isEmpty { - groups.append(("Identity Auto-properties", autoProps)) + if !identityAutoProps.isEmpty { + groups.append(("Identity Auto-properties", identityAutoProps)) } if !metricProps.isEmpty {