Skip to content

Commit 1ca4cbe

Browse files
committed
Add country property to receipt events.
1 parent 6f79e8d commit 1ca4cbe

File tree

6 files changed

+104
-20
lines changed

6 files changed

+104
-20
lines changed

WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,84 @@ extension WooAnalyticsEvent {
876876
"reason": reason
877877
])
878878
}
879+
880+
/// Tracked when the user taps on the "See Receipt" button to view a receipt.
881+
/// - Parameter countryCode: the country code of the store.
882+
///
883+
static func receiptViewTapped(countryCode: String) -> WooAnalyticsEvent {
884+
WooAnalyticsEvent(statName: .receiptViewTapped,
885+
properties: [Keys.countryCode: countryCode])
886+
}
887+
888+
/// Tracked when the user taps on the "Email receipt" button after successfully collecting a payment to email a receipt.
889+
/// - Parameter countryCode: the country code of the store.
890+
///
891+
static func receiptEmailTapped(countryCode: String) -> WooAnalyticsEvent {
892+
WooAnalyticsEvent(statName: .receiptEmailTapped,
893+
properties: [Keys.countryCode: countryCode])
894+
}
895+
896+
/// Tracked when sending or saving the receipt email failed.
897+
/// - Parameters:
898+
/// - error: the error to be included in the event properties.
899+
/// - countryCode: the country code of the store.
900+
///
901+
static func receiptEmailFailed(error: Error, countryCode: String) -> WooAnalyticsEvent {
902+
WooAnalyticsEvent(statName: .receiptEmailFailed,
903+
properties: [Keys.countryCode: countryCode],
904+
error: error)
905+
}
906+
907+
/// Tracked when the user canceled sending the receipt by email.
908+
/// - Parameter countryCode: the country code of the store.
909+
///
910+
static func receiptEmailCanceled(countryCode: String) -> WooAnalyticsEvent {
911+
WooAnalyticsEvent(statName: .receiptEmailCanceled,
912+
properties: [Keys.countryCode: countryCode])
913+
}
914+
915+
/// Tracked when the receipt was sent by email.
916+
/// - Parameter countryCode: the country code of the store.
917+
///
918+
static func receiptEmailSuccess(countryCode: String) -> WooAnalyticsEvent {
919+
WooAnalyticsEvent(statName: .receiptEmailSuccess,
920+
properties: [Keys.countryCode: countryCode])
921+
}
922+
923+
/// Tracked when the user tapped on the button to print a receipt.
924+
/// - Parameter countryCode: the country code of the store.
925+
///
926+
static func receiptPrintTapped(countryCode: String) -> WooAnalyticsEvent {
927+
WooAnalyticsEvent(statName: .receiptPrintTapped,
928+
properties: [Keys.countryCode: countryCode])
929+
}
930+
931+
/// Tracked when printing the receipt failed.
932+
/// - Parameters:
933+
/// - error: the error to be included in the event properties.
934+
/// - countryCode: the country code of the store.
935+
///
936+
static func receiptPrintFailed(error: Error, countryCode: String) -> WooAnalyticsEvent {
937+
WooAnalyticsEvent(statName: .receiptPrintFailed,
938+
properties: [Keys.countryCode: countryCode],
939+
error: error)
940+
}
941+
942+
/// Tracked when the user canceled printing the receipt.
943+
/// - Parameter countryCode: the country code of the store.
944+
///
945+
static func receiptPrintCanceled(countryCode: String) -> WooAnalyticsEvent {
946+
WooAnalyticsEvent(statName: .receiptPrintCanceled,
947+
properties: [Keys.countryCode: countryCode])
948+
}
949+
950+
/// Tracked when the receipt was successfully sent to the printer. iOS won't guarantee that the receipt has actually printed.
951+
/// - Parameter countryCode: the country code of the store.
952+
///
953+
static func receiptPrintSuccess(countryCode: String) -> WooAnalyticsEvent {
954+
WooAnalyticsEvent(statName: .receiptPrintSuccess,
955+
properties: [Keys.countryCode: countryCode])
956+
}
879957
}
880958
}
881959

WooCommerce/Classes/ViewModels/CardPresentPayments/ReceiptActionCoordinator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import Foundation
22
import Yosemite
33

44
struct ReceiptActionCoordinator {
5-
static func printReceipt(for order: Order, params: CardPresentReceiptParameters) {
6-
ServiceLocator.analytics.track(.receiptPrintTapped)
5+
static func printReceipt(for order: Order, params: CardPresentReceiptParameters, countryCode: String) {
6+
ServiceLocator.analytics.track(event: .InPersonPayments.receiptPrintTapped(countryCode: countryCode))
77

88
let action = ReceiptAction.print(order: order, parameters: params) { (result) in
99
switch result {
1010
case .success:
11-
ServiceLocator.analytics.track(.receiptPrintSuccess)
11+
ServiceLocator.analytics.track(event: .InPersonPayments.receiptPrintSuccess(countryCode: countryCode))
1212
case .cancel:
13-
ServiceLocator.analytics.track(.receiptPrintCanceled)
13+
ServiceLocator.analytics.track(event: .InPersonPayments.receiptPrintCanceled(countryCode: countryCode))
1414
case .failure(let error):
15-
ServiceLocator.analytics.track(.receiptPrintFailed, withError: error)
15+
ServiceLocator.analytics.track(event: .InPersonPayments.receiptPrintFailed(error: error, countryCode: countryCode))
1616
DDLogError("⛔️ Failed to print receipt: \(error.localizedDescription)")
1717
}
1818
}

WooCommerce/Classes/ViewModels/CardPresentPayments/ReceiptViewModel.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import Yosemite
44
final class ReceiptViewModel {
55
private let order: Order
66
private let receipt: CardPresentReceiptParameters
7+
private let countryCode: String
78

89

910
/// Initializer
1011
/// - Parameters:
11-
/// - order: The order associated with the receipt
12+
/// - order: the order associated with the receipt
1213
/// - receipt: the receipt metadata
13-
init(order: Order, receipt: CardPresentReceiptParameters) {
14+
/// - countryCode: the country code of the store
15+
init(order: Order, receipt: CardPresentReceiptParameters, countryCode: String) {
1416
self.order = order
1517
self.receipt = receipt
18+
self.countryCode = countryCode
1619
}
1720

1821

@@ -29,6 +32,6 @@ final class ReceiptViewModel {
2932

3033
/// Prints the receipt
3134
func printReceipt() {
32-
ReceiptActionCoordinator.printReceipt(for: order, params: receipt)
35+
ReceiptActionCoordinator.printReceipt(for: order, params: receipt, countryCode: countryCode)
3336
}
3437
}

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsViewModel.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ extension OrderDetailsViewModel {
264264
let billingInformationViewController = BillingInformationViewController(order: order, editingEnabled: true)
265265
viewController.navigationController?.pushViewController(billingInformationViewController, animated: true)
266266
case .seeReceipt:
267-
ServiceLocator.analytics.track(.receiptViewTapped)
267+
let countryCode = configurationLoader.configuration.countryCode
268+
ServiceLocator.analytics.track(event: .InPersonPayments.receiptViewTapped(countryCode: countryCode))
268269
guard let receipt = receipt else {
269270
return
270271
}
271-
let viewModel = ReceiptViewModel(order: order, receipt: receipt)
272+
let viewModel = ReceiptViewModel(order: order, receipt: receipt, countryCode: countryCode)
272273
let receiptViewController = ReceiptViewController(viewModel: viewModel)
273274
viewController.navigationController?.pushViewController(receiptViewController, animated: true)
274275
case .refund:

WooCommerce/Classes/ViewRelated/Orders/Collect Payments/CollectOrderPaymentUseCase.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,16 @@ private extension CollectOrderPaymentUseCase {
265265
///
266266
func presentReceiptAlert(receiptParameters: CardPresentReceiptParameters, backButtonTitle: String, onCompleted: @escaping () -> ()) {
267267
// Present receipt alert
268-
alerts.success(printReceipt: { [order] in
268+
alerts.success(printReceipt: { [order, configurationLoader] in
269269
// Inform about flow completion.
270270
onCompleted()
271271

272272
// Delegate print action
273-
ReceiptActionCoordinator.printReceipt(for: order, params: receiptParameters)
273+
ReceiptActionCoordinator.printReceipt(for: order, params: receiptParameters, countryCode: configurationLoader.configuration.countryCode)
274274

275-
}, emailReceipt: { [order, analytics, paymentOrchestrator] in
275+
}, emailReceipt: { [order, analytics, paymentOrchestrator, configurationLoader] in
276276
// Record button tapped
277-
analytics.track(.receiptEmailTapped)
277+
analytics.track(event: .InPersonPayments.receiptEmailTapped(countryCode: configurationLoader.configuration.countryCode))
278278

279279
// Request & present email
280280
paymentOrchestrator.emailReceipt(for: order, params: receiptParameters) { [weak self] emailContent in
@@ -314,11 +314,13 @@ extension CollectOrderPaymentUseCase: MFMailComposeViewControllerDelegate {
314314
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
315315
switch result {
316316
case .cancelled:
317-
analytics.track(.receiptEmailCanceled)
317+
analytics.track(event: .InPersonPayments.receiptEmailCanceled(countryCode: configurationLoader.configuration.countryCode))
318318
case .sent, .saved:
319-
analytics.track(.receiptEmailSuccess)
319+
analytics.track(event: .InPersonPayments.receiptEmailSuccess(countryCode: configurationLoader.configuration.countryCode))
320320
case .failed:
321-
analytics.track(.receiptEmailFailed, withError: error ?? UnknownEmailError())
321+
analytics.track(event: .InPersonPayments
322+
.receiptEmailFailed(error: error ?? UnknownEmailError(),
323+
countryCode: configurationLoader.configuration.countryCode))
322324
@unknown default:
323325
assertionFailure("MFMailComposeViewController finished with an unknown result type")
324326
}

WooCommerce/WooCommerceTests/ViewRelated/CardPresentPayments/ReceiptActionCoordinatorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ReceiptActionCoordinatorTests: XCTestCase {
1313
let params = CardPresentReceiptParameters.makeParams()
1414

1515
// When
16-
ReceiptActionCoordinator.printReceipt(for: order, params: params)
16+
ReceiptActionCoordinator.printReceipt(for: order, params: params, countryCode: "CA")
1717

1818
// Then
1919
let analytics = ServiceLocator.analytics.analyticsProvider as! MockAnalyticsProvider
@@ -35,7 +35,7 @@ class ReceiptActionCoordinatorTests: XCTestCase {
3535
assertEmpty(storesManager.receivedActions)
3636

3737
// When
38-
ReceiptActionCoordinator.printReceipt(for: order, params: params)
38+
ReceiptActionCoordinator.printReceipt(for: order, params: params, countryCode: "CA")
3939

4040
//Then
4141
XCTAssertEqual(storesManager.receivedActions.count, 1)
@@ -75,7 +75,7 @@ extension ReceiptActionCoordinatorTests {
7575
ServiceLocator.setAnalytics(WooAnalytics(analyticsProvider: MockAnalyticsProvider()))
7676

7777
// When
78-
ReceiptActionCoordinator.printReceipt(for: order, params: params)
78+
ReceiptActionCoordinator.printReceipt(for: order, params: params, countryCode: "CA")
7979

8080
//Then
8181
let action = try XCTUnwrap(storesManager.receivedActions.first as? ReceiptAction)

0 commit comments

Comments
 (0)