Skip to content

Commit b7bdb25

Browse files
committed
Added updatePropertiesIfNeeded() to exclude blog_id for certain events
1 parent 326534f commit b7bdb25

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

WooCommerce/Classes/Analytics/TracksProvider.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ private extension TracksProvider {
5757
userProperties[UserProperties.platformKey] = "iOS"
5858
userProperties[UserProperties.voiceOverKey] = UIAccessibility.isVoiceOverRunning
5959
userProperties[UserProperties.rtlKey] = (UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft)
60-
if StoresManager.shared.isAuthenticated {
61-
let site = StoresManager.shared.sessionManager.defaultSite
62-
userProperties[UserProperties.blogIDKey] = site?.siteID
63-
userProperties[UserProperties.wpcomStoreKey] = site?.isWordPressStore
64-
}
6560
tracksService.userProperties.removeAllObjects()
6661
tracksService.userProperties.addEntries(from: userProperties)
6762
}
@@ -80,7 +75,5 @@ private extension TracksProvider {
8075
static let platformKey = "platform"
8176
static let voiceOverKey = "accessibility_voice_over_enabled"
8277
static let rtlKey = "is_rtl_language"
83-
static let blogIDKey = "blog_id"
84-
static let wpcomStoreKey = "is_wpcom_store"
8578
}
8679
}

WooCommerce/Classes/Analytics/WooAnalytics.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public extension WooAnalytics {
6666
/// - properties: a collection of properties related to the event
6767
///
6868
func track(_ stat: WooAnalyticsStat, withProperties properties: [AnyHashable: Any]?) {
69-
if let properties = properties {
70-
analyticsProvider.track(stat.rawValue, withProperties: properties)
69+
if let updatedProperties = updatePropertiesIfNeeded(for: stat, properties: properties) {
70+
analyticsProvider.track(stat.rawValue, withProperties: updatedProperties)
7171
} else {
7272
analyticsProvider.track(stat.rawValue)
7373
}
@@ -84,7 +84,8 @@ public extension WooAnalytics {
8484
let errorDictionary = [Constants.errorKeyCode: "\(err.code)",
8585
Constants.errorKeyDomain: err.domain,
8686
Constants.errorKeyDescription: err.description]
87-
analyticsProvider.track(stat.rawValue, withProperties: errorDictionary)
87+
let updatedProperties = updatePropertiesIfNeeded(for: stat, properties: errorDictionary)
88+
analyticsProvider.track(stat.rawValue, withProperties: updatedProperties)
8889
}
8990
}
9091

@@ -114,7 +115,21 @@ private extension WooAnalytics {
114115
}
115116

116117
let timeInApp = round(Date().timeIntervalSince(applicationOpenedTime))
117-
return [Constants.propertyKeyTimeInApp: timeInApp.description]
118+
return [PropertyKeys.propertyKeyTimeInApp: timeInApp.description]
119+
}
120+
121+
/// This function appends any additional properties to the provided properties dict if needed.
122+
///
123+
func updatePropertiesIfNeeded(for stat: WooAnalyticsStat, properties: [AnyHashable: Any]?) -> [AnyHashable: Any]? {
124+
guard stat.shouldSendSiteProperties, StoresManager.shared.isAuthenticated else {
125+
return properties
126+
}
127+
128+
var updatedProperties = properties ?? [:]
129+
let site = StoresManager.shared.sessionManager.defaultSite
130+
updatedProperties[PropertyKeys.blogIDKey] = site?.siteID
131+
updatedProperties[PropertyKeys.wpcomStoreKey] = site?.isWordPressStore
132+
return updatedProperties
118133
}
119134
}
120135

@@ -127,7 +142,11 @@ private extension WooAnalytics {
127142
static let errorKeyCode = "error_code"
128143
static let errorKeyDomain = "error_domain"
129144
static let errorKeyDescription = "error_description"
145+
}
130146

147+
enum PropertyKeys {
131148
static let propertyKeyTimeInApp = "time_in_app"
149+
static let blogIDKey = "blog_id"
150+
static let wpcomStoreKey = "is_wpcom_store"
132151
}
133152
}

WooCommerce/Classes/Analytics/WooAnalyticsStat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import WordPressShared
44
/// This enum contains all of the events we track in the app. Please reference the "Woo Mobile Events Draft i2"
55
/// spreadsheet for more details.
66
///
7-
/// Note: if you would like to exclude site properties (e.g. `blog_id`) for a given event, please
8-
/// add the event to the `shouldSendSiteProperties` var.
7+
/// Note: If you would like to exclude site properties (e.g. `blog_id`) for a given event, please
8+
/// add the event to the `WooAnalyticsStat.shouldSendSiteProperties` var.
99
///
1010
public enum WooAnalyticsStat: String {
1111

0 commit comments

Comments
 (0)