Skip to content

Commit 10e4312

Browse files
authored
Merge pull request #365 from woocommerce/issue/361-analytics-blogid-fix
Analytics: exclude site information for some tracks events
2 parents 0eb7c4e + b7bdb25 commit 10e4312

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ 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 `WooAnalyticsStat.shouldSendSiteProperties` var.
9+
///
710
public enum WooAnalyticsStat: String {
811

912
// Application Events
@@ -124,6 +127,35 @@ public enum WooAnalyticsStat: String {
124127

125128
public extension WooAnalyticsStat {
126129

130+
131+
/// Indicates if site information should be included with this event when it's sent to the tracks server.
132+
/// Returns `true` if it should be included, `false` otherwise.
133+
///
134+
/// Note: Currently all application-level and authentication events will return false. If you wish
135+
/// to include additional no-site-info events, please add them here.
136+
///
137+
var shouldSendSiteProperties: Bool {
138+
switch self {
139+
// Application events
140+
case .applicationClosed, .applicationOpened, .applicationUpgraded, .applicationInstalled:
141+
return false
142+
// Authentication Events
143+
case .signedIn, .logout, .openedLogin, .loginFailed,
144+
.loginAutoFillCredentialsFilled, .loginAutoFillCredentialsUpdated, .loginEmailFormViewed, .loginMagicLinkOpenEmailClientViewed,
145+
.loginMagicLinkRequestFormViewed, .loginMagicLinkExited, .loginMagicLinkFailed, .loginMagicLinkOpened,
146+
.loginMagicLinkRequested, .loginMagicLinkSucceeded, .loginPasswordFormViewed, .loginURLFormViewed,
147+
.loginURLHelpScreenViewed, .loginUsernamePasswordFormViewed, .loginTwoFactorFormViewed, .loginEpilogueViewed,
148+
.loginEpilogueStoresShown, .loginEpilogueContinueTapped, .loginProloguePaged, .loginPrologueViewed,
149+
.loginPrologueContinueTapped, .loginPrologueJetpackInstructions, .loginForgotPasswordClicked, .loginSocialButtonClick,
150+
.loginSocialButtonFailure, .loginSocialConnectSuccess, .loginSocialConnectFailure, .loginSocialSuccess,
151+
.loginSocialFailure, .loginSocial2faNeeded, .loginSocialAccountsNeedConnecting, .loginSocialErrorUnknownUser,
152+
.onePasswordFailed, .onePasswordLogin, .onePasswordSignup, .twoFactorCodeRequested, .twoFactorSentSMS:
153+
return false
154+
default:
155+
return true
156+
}
157+
}
158+
127159
/// Converts the provided WPAnalyticsStat into a WooAnalyticsStat.
128160
/// This whole process kinda stinks, but we need this for the `WordPressAuthenticatorDelegate`
129161
/// implementation. ☹️ Feel free to refactor later on!

0 commit comments

Comments
 (0)