@@ -131,14 +131,20 @@ final class NewOrderViewModel: ObservableObject {
131131 ///
132132 @Published private( set) var paymentDataViewModel = PaymentDataViewModel ( )
133133
134+ /// Analytics engine.
135+ ///
136+ private let analytics : Analytics
137+
134138 init ( siteID: Int64 ,
135139 stores: StoresManager = ServiceLocator . stores,
136140 storageManager: StorageManagerType = ServiceLocator . storageManager,
137- currencySettings: CurrencySettings = ServiceLocator . currencySettings) {
141+ currencySettings: CurrencySettings = ServiceLocator . currencySettings,
142+ analytics: Analytics = ServiceLocator . analytics) {
138143 self . siteID = siteID
139144 self . stores = stores
140145 self . storageManager = storageManager
141146 self . currencyFormatter = CurrencyFormatter ( currencySettings: currencySettings)
147+ self . analytics = analytics
142148
143149 configureNavigationTrailingItem ( )
144150 configureStatusBadgeViewModel ( )
@@ -195,6 +201,7 @@ final class NewOrderViewModel: ObservableObject {
195201 onAddressUpdate: { [ weak self] updatedAddressData in
196202 self ? . orderDetails. billingAddress = updatedAddressData. billingAddress
197203 self ? . orderDetails. shippingAddress = updatedAddressData. shippingAddress
204+ self ? . trackCustomerDetailsAdded ( )
198205 } )
199206 }
200207
@@ -211,17 +218,28 @@ final class NewOrderViewModel: ObservableObject {
211218 switch result {
212219 case . success( let newOrder) :
213220 self . onOrderCreated ( newOrder)
221+ self . trackCreateOrderSuccess ( )
214222 case . failure( let error) :
215223 self . notice = NoticeFactory . createOrderCreationErrorNotice ( )
224+ self . trackCreateOrderFailure ( error: error)
216225 DDLogError ( " ⛔️ Error creating new order: \( error) " )
217226 }
218227 }
219228 stores. dispatch ( action)
229+ trackCreateButtonTapped ( )
220230 }
221231
222232 /// Assign this closure to be notified when a new order is created
223233 ///
224234 var onOrderCreated : ( Order ) -> Void = { _ in }
235+
236+ /// Updates the order status & tracks its event
237+ ///
238+ func updateOrderStatus( newStatus: OrderStatusEnum ) {
239+ let oldStatus = orderDetails. status
240+ orderDetails. status = newStatus
241+ analytics. track ( event: WooAnalyticsEvent . Orders. orderStatusChange ( flow: . creation, from: oldStatus, to: newStatus) )
242+ }
225243}
226244
227245// MARK: - Types
@@ -410,6 +428,8 @@ private extension NewOrderViewModel {
410428 let newOrderItem = NewOrderItem ( product: product, quantity: 1 )
411429 orderDetails. items. append ( newOrderItem)
412430 configureProductRowViewModels ( )
431+
432+ analytics. track ( event: WooAnalyticsEvent . Orders. orderProductAdd ( flow: . creation) )
413433 }
414434
415435 /// Adds a selected product variation (from the product list) to the order.
@@ -418,6 +438,8 @@ private extension NewOrderViewModel {
418438 let newOrderItem = NewOrderItem ( variation: variation, quantity: 1 )
419439 orderDetails. items. append ( newOrderItem)
420440 configureProductRowViewModels ( )
441+
442+ analytics. track ( event: WooAnalyticsEvent . Orders. orderProductAdd ( flow: . creation) )
421443 }
422444
423445 /// Configures product row view models for each item in `orderDetails`.
@@ -471,6 +493,44 @@ private extension NewOrderViewModel {
471493 }
472494 . assign ( to: & $paymentDataViewModel)
473495 }
496+
497+ /// Tracks when customer details have been added
498+ ///
499+ func trackCustomerDetailsAdded( ) {
500+ let areAddressesDifferent : Bool = {
501+ guard let billingAddress = orderDetails. billingAddress, let shippingAddress = orderDetails. shippingAddress else {
502+ return false
503+ }
504+ return billingAddress != shippingAddress
505+ } ( )
506+ analytics. track ( event: WooAnalyticsEvent . Orders. orderCustomerAdd ( flow: . creation, hasDifferentShippingDetails: areAddressesDifferent) )
507+ }
508+
509+ /// Tracks when the create order button is tapped.
510+ ///
511+ /// Warning: This methods assume that `orderDetails.items.count` is equal to the product count,
512+ /// As the module evolves to handle more types of items, we need to update the property to something like `itemsCount`
513+ /// or figure out a better way to get the product count.
514+ ///
515+ func trackCreateButtonTapped( ) {
516+ let hasCustomerDetails = orderDetails. billingAddress != nil || orderDetails. shippingAddress != nil
517+ analytics. track ( event: WooAnalyticsEvent . Orders. orderCreateButtonTapped ( status: orderDetails. status,
518+ productCount: orderDetails. items. count,
519+ hasCustomerDetails: hasCustomerDetails) )
520+ }
521+
522+ /// Tracks an order creation success
523+ ///
524+ func trackCreateOrderSuccess( ) {
525+ analytics. track ( event: WooAnalyticsEvent . Orders. orderCreationSuccess ( ) )
526+ }
527+
528+ /// Tracks an order creation failure
529+ ///
530+ func trackCreateOrderFailure( error: Error ) {
531+ analytics. track ( event: WooAnalyticsEvent . Orders. orderCreationFailed ( errorContext: String ( describing: error) ,
532+ errorDescription: error. localizedDescription) )
533+ }
474534}
475535
476536private extension NewOrderViewModel {
0 commit comments