Skip to content

Commit

Permalink
♻️ Refactor AutoPropertyDecorator to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt authored and iujames committed Sep 17, 2024
1 parent 9ebc4c8 commit 380ae5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Sources/AppcuesKit/Data/Analytics/AnalyticsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 17 additions & 14 deletions Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,38 +68,28 @@ 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,
"_sessionPageviews": sessionPageviews,
"_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,
"_lastContentShownAt": storage.lastContentShownAt,
"_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)
Expand All @@ -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
Expand Down Expand Up @@ -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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 380ae5a

Please sign in to comment.