Skip to content

Commit 752f905

Browse files
authored
Merge pull request #6545 from woocommerce/feat/5984-add-country-prop-to-receipt-events
IPP analytics: add `country` property to receipt events
2 parents f71b724 + 1ca4cbe commit 752f905

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
@@ -896,6 +896,84 @@ extension WooAnalyticsEvent {
896896
"reason": reason
897897
])
898898
}
899+
900+
/// Tracked when the user taps on the "See Receipt" button to view a receipt.
901+
/// - Parameter countryCode: the country code of the store.
902+
///
903+
static func receiptViewTapped(countryCode: String) -> WooAnalyticsEvent {
904+
WooAnalyticsEvent(statName: .receiptViewTapped,
905+
properties: [Keys.countryCode: countryCode])
906+
}
907+
908+
/// Tracked when the user taps on the "Email receipt" button after successfully collecting a payment to email a receipt.
909+
/// - Parameter countryCode: the country code of the store.
910+
///
911+
static func receiptEmailTapped(countryCode: String) -> WooAnalyticsEvent {
912+
WooAnalyticsEvent(statName: .receiptEmailTapped,
913+
properties: [Keys.countryCode: countryCode])
914+
}
915+
916+
/// Tracked when sending or saving the receipt email failed.
917+
/// - Parameters:
918+
/// - error: the error to be included in the event properties.
919+
/// - countryCode: the country code of the store.
920+
///
921+
static func receiptEmailFailed(error: Error, countryCode: String) -> WooAnalyticsEvent {
922+
WooAnalyticsEvent(statName: .receiptEmailFailed,
923+
properties: [Keys.countryCode: countryCode],
924+
error: error)
925+
}
926+
927+
/// Tracked when the user canceled sending the receipt by email.
928+
/// - Parameter countryCode: the country code of the store.
929+
///
930+
static func receiptEmailCanceled(countryCode: String) -> WooAnalyticsEvent {
931+
WooAnalyticsEvent(statName: .receiptEmailCanceled,
932+
properties: [Keys.countryCode: countryCode])
933+
}
934+
935+
/// Tracked when the receipt was sent by email.
936+
/// - Parameter countryCode: the country code of the store.
937+
///
938+
static func receiptEmailSuccess(countryCode: String) -> WooAnalyticsEvent {
939+
WooAnalyticsEvent(statName: .receiptEmailSuccess,
940+
properties: [Keys.countryCode: countryCode])
941+
}
942+
943+
/// Tracked when the user tapped on the button to print a receipt.
944+
/// - Parameter countryCode: the country code of the store.
945+
///
946+
static func receiptPrintTapped(countryCode: String) -> WooAnalyticsEvent {
947+
WooAnalyticsEvent(statName: .receiptPrintTapped,
948+
properties: [Keys.countryCode: countryCode])
949+
}
950+
951+
/// Tracked when printing the receipt failed.
952+
/// - Parameters:
953+
/// - error: the error to be included in the event properties.
954+
/// - countryCode: the country code of the store.
955+
///
956+
static func receiptPrintFailed(error: Error, countryCode: String) -> WooAnalyticsEvent {
957+
WooAnalyticsEvent(statName: .receiptPrintFailed,
958+
properties: [Keys.countryCode: countryCode],
959+
error: error)
960+
}
961+
962+
/// Tracked when the user canceled printing the receipt.
963+
/// - Parameter countryCode: the country code of the store.
964+
///
965+
static func receiptPrintCanceled(countryCode: String) -> WooAnalyticsEvent {
966+
WooAnalyticsEvent(statName: .receiptPrintCanceled,
967+
properties: [Keys.countryCode: countryCode])
968+
}
969+
970+
/// Tracked when the receipt was successfully sent to the printer. iOS won't guarantee that the receipt has actually printed.
971+
/// - Parameter countryCode: the country code of the store.
972+
///
973+
static func receiptPrintSuccess(countryCode: String) -> WooAnalyticsEvent {
974+
WooAnalyticsEvent(statName: .receiptPrintSuccess,
975+
properties: [Keys.countryCode: countryCode])
976+
}
899977
}
900978
}
901979

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
@@ -274,16 +274,16 @@ private extension CollectOrderPaymentUseCase {
274274
///
275275
func presentReceiptAlert(receiptParameters: CardPresentReceiptParameters, backButtonTitle: String, onCompleted: @escaping () -> ()) {
276276
// Present receipt alert
277-
alerts.success(printReceipt: { [order] in
277+
alerts.success(printReceipt: { [order, configurationLoader] in
278278
// Inform about flow completion.
279279
onCompleted()
280280

281281
// Delegate print action
282-
ReceiptActionCoordinator.printReceipt(for: order, params: receiptParameters)
282+
ReceiptActionCoordinator.printReceipt(for: order, params: receiptParameters, countryCode: configurationLoader.configuration.countryCode)
283283

284-
}, emailReceipt: { [order, analytics, paymentOrchestrator] in
284+
}, emailReceipt: { [order, analytics, paymentOrchestrator, configurationLoader] in
285285
// Record button tapped
286-
analytics.track(.receiptEmailTapped)
286+
analytics.track(event: .InPersonPayments.receiptEmailTapped(countryCode: configurationLoader.configuration.countryCode))
287287

288288
// Request & present email
289289
paymentOrchestrator.emailReceipt(for: order, params: receiptParameters) { [weak self] emailContent in
@@ -333,11 +333,13 @@ extension CollectOrderPaymentUseCase: MFMailComposeViewControllerDelegate {
333333
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
334334
switch result {
335335
case .cancelled:
336-
analytics.track(.receiptEmailCanceled)
336+
analytics.track(event: .InPersonPayments.receiptEmailCanceled(countryCode: configurationLoader.configuration.countryCode))
337337
case .sent, .saved:
338-
analytics.track(.receiptEmailSuccess)
338+
analytics.track(event: .InPersonPayments.receiptEmailSuccess(countryCode: configurationLoader.configuration.countryCode))
339339
case .failed:
340-
analytics.track(.receiptEmailFailed, withError: error ?? UnknownEmailError())
340+
analytics.track(event: .InPersonPayments
341+
.receiptEmailFailed(error: error ?? UnknownEmailError(),
342+
countryCode: configurationLoader.configuration.countryCode))
341343
@unknown default:
342344
assertionFailure("MFMailComposeViewController finished with an unknown result type")
343345
}

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)