@@ -39,8 +39,15 @@ import Yosemite
3939/// ~~~
4040///
4141public struct WooAnalyticsEvent {
42+ init ( statName: WooAnalyticsStat , properties: [ String : WooAnalyticsEventPropertyType ] , error: Error ? = nil ) {
43+ self . statName = statName
44+ self . properties = properties
45+ self . error = error
46+ }
47+
4248 let statName : WooAnalyticsStat
4349 let properties : [ String : WooAnalyticsEventPropertyType ]
50+ let error : Error ?
4451}
4552
4653// MARK: - In-app Feedback and Survey
@@ -569,6 +576,7 @@ extension WooAnalyticsEvent {
569576 static let countryCode = " country "
570577 static let gatewayID = " plugin_slug "
571578 static let errorDescription = " error_description "
579+ static let paymentMethodType = " payment_method_type "
572580 static let softwareUpdateType = " software_update_type "
573581 }
574582
@@ -779,13 +787,39 @@ extension WooAnalyticsEvent {
779787 /// - countryCode: the country code of the store.
780788 ///
781789 static func collectPaymentFailed( forGatewayID: String ? , error: Error , countryCode: String ) -> WooAnalyticsEvent {
782- WooAnalyticsEvent ( statName: . collectPaymentFailed,
783- properties: [
784- Keys . countryCode: countryCode,
785- Keys . gatewayID: gatewayID ( forGatewayID: forGatewayID) ,
786- Keys . errorDescription: error. localizedDescription
787- ]
788- )
790+ let paymentMethod : PaymentMethod ? = {
791+ guard case let CardReaderServiceError . paymentCaptureWithPaymentMethod( _, paymentMethod) = error else {
792+ return nil
793+ }
794+ return paymentMethod
795+ } ( )
796+ let errorDescription : String ? = {
797+ guard case let CardReaderServiceError . paymentCaptureWithPaymentMethod( underlyingError, paymentMethod) = error else {
798+ return error. localizedDescription
799+ }
800+ switch paymentMethod {
801+ case let . cardPresent( details) :
802+ return [
803+ " underlyingError " : underlyingError,
804+ " cardBrand " : details. brand
805+ ] . description
806+ case let . interacPresent( details) :
807+ return [
808+ " underlyingError " : underlyingError,
809+ " cardBrand " : details. brand
810+ ] . description
811+ default :
812+ return underlyingError. localizedDescription
813+ }
814+ } ( )
815+ let properties : [ String : WooAnalyticsEventPropertyType ] = [
816+ Keys . countryCode: countryCode,
817+ Keys . gatewayID: gatewayID ( forGatewayID: forGatewayID) ,
818+ Keys . paymentMethodType: paymentMethod? . analyticsValue,
819+ Keys . errorDescription: errorDescription
820+ ] . compactMapValues { $0 }
821+ return WooAnalyticsEvent ( statName: . collectPaymentFailed,
822+ properties: properties)
789823 }
790824
791825 /// Tracked when the payment collection is cancelled
@@ -808,12 +842,14 @@ extension WooAnalyticsEvent {
808842 /// - Parameters:
809843 /// - forGatewayID: the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
810844 /// - countryCode: the country code of the store.
845+ /// - paymentMethod: the payment method of the captured payment.
811846 ///
812- static func collectPaymentSuccess( forGatewayID: String ? , countryCode: String ) -> WooAnalyticsEvent {
847+ static func collectPaymentSuccess( forGatewayID: String ? , countryCode: String , paymentMethod : PaymentMethod ) -> WooAnalyticsEvent {
813848 WooAnalyticsEvent ( statName: . collectPaymentSuccess,
814849 properties: [
815850 Keys . countryCode: countryCode,
816- Keys . gatewayID: gatewayID ( forGatewayID: forGatewayID)
851+ Keys . gatewayID: gatewayID ( forGatewayID: forGatewayID) ,
852+ Keys . paymentMethodType: paymentMethod. analyticsValue
817853 ]
818854 )
819855 }
@@ -842,3 +878,16 @@ extension WooAnalyticsEvent {
842878 }
843879 }
844880}
881+
882+ private extension PaymentMethod {
883+ var analyticsValue : String {
884+ switch self {
885+ case . card, . cardPresent:
886+ return " card "
887+ case . interacPresent:
888+ return " card_interac "
889+ case . unknown:
890+ return " unknown "
891+ }
892+ }
893+ }
0 commit comments