@@ -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}
0 commit comments