Skip to content

Instantly share code, notes, and snippets.

@koke
Created February 16, 2022 08:41
Show Gist options
  • Save koke/80f96e5c9f253d225756d3d52c0f4374 to your computer and use it in GitHub Desktop.
Save koke/80f96e5c9f253d225756d3d52c0f4374 to your computer and use it in GitHub Desktop.
diff --git a/WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift b/WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift
index d913d37e1..e4b96334b 100644
--- a/WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift
+++ b/WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift
@@ -551,20 +551,14 @@ extension WooAnalyticsEvent {
static let softwareUpdateType = "software_update_type"
}
- static let unknownGatewayID = "unknown"
-
- static func gatewayID(forGatewayID: String?) -> String {
- forGatewayID ?? unknownGatewayID
- }
-
/// Tracked when card reader discovery fails
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `error` is the error to be included in the event properties.
///
- static func cardReaderDiscoveryFailed(forGatewayID: String?, error: Error) -> WooAnalyticsEvent {
+ static func cardReaderDiscoveryFailed(forGatewayID: String, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderDiscoveryFailed,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.errorDescription: error.localizedDescription
]
)
@@ -574,9 +568,9 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `batteryLevel` is the battery level (if available) to be included in the event properties in Tracks, e.g. 0.75 = 75%.
///
- static func cardReaderConnectionSuccess(forGatewayID: String?, batteryLevel: Float?) -> WooAnalyticsEvent {
+ static func cardReaderConnectionSuccess(forGatewayID: String, batteryLevel: Float?) -> WooAnalyticsEvent {
var properties = [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID)
+ Keys.gatewayID: forGatewayID
]
if let batteryLevel = batteryLevel {
@@ -590,10 +584,10 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `error` is the error to be included in the event properties.
///
- static func cardReaderConnectionFailed(forGatewayID: String?, error: Error) -> WooAnalyticsEvent {
+ static func cardReaderConnectionFailed(forGatewayID: String, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderConnectionFailed,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.errorDescription: error.localizedDescription
]
)
@@ -603,10 +597,10 @@ extension WooAnalyticsEvent {
/// Tracked when disconnecting from a card reader
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func cardReaderDisconnectTapped(forGatewayID: String?) -> WooAnalyticsEvent {
+ static func cardReaderDisconnectTapped(forGatewayID: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderDisconnectTapped,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID)
+ Keys.gatewayID: forGatewayID
]
)
}
@@ -614,10 +608,10 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `updateType` is `.required` or `.optional`
///
- static func cardReaderSoftwareUpdateTapped(forGatewayID: String?, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
+ static func cardReaderSoftwareUpdateTapped(forGatewayID: String, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateTapped,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue
]
)
@@ -627,10 +621,10 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `updateType` is `.required` or `.optional`
///
- static func cardReaderSoftwareUpdateStarted(forGatewayID: String?, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
+ static func cardReaderSoftwareUpdateStarted(forGatewayID: String, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateStarted,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue
]
)
@@ -642,11 +636,11 @@ extension WooAnalyticsEvent {
/// `error` is the error to be included in the event properties.
///
static func cardReaderSoftwareUpdateFailed(
- forGatewayID: String?, updateType: SoftwareUpdateTypeProperty, error: Error
+ forGatewayID: String, updateType: SoftwareUpdateTypeProperty, error: Error
) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateFailed,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue,
Keys.errorDescription: error.localizedDescription
]
@@ -657,10 +651,10 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `updateType` is `.required` or `.optional`
///
- static func cardReaderSoftwareUpdateSuccess(forGatewayID: String?, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
+ static func cardReaderSoftwareUpdateSuccess(forGatewayID: String, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateSuccess,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue
]
)
@@ -669,10 +663,10 @@ extension WooAnalyticsEvent {
/// Tracked when an update cancel button is tapped
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func cardReaderSoftwareUpdateCancelTapped(forGatewayID: String?, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
+ static func cardReaderSoftwareUpdateCancelTapped(forGatewayID: String, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateCancelTapped,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue
]
)
@@ -681,10 +675,10 @@ extension WooAnalyticsEvent {
/// Tracked when an update is cancelled
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func cardReaderSoftwareUpdateCanceled(forGatewayID: String?, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
+ static func cardReaderSoftwareUpdateCanceled(forGatewayID: String, updateType: SoftwareUpdateTypeProperty) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .cardReaderSoftwareUpdateCanceled,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.softwareUpdateType: updateType.rawValue
]
)
@@ -693,10 +687,10 @@ extension WooAnalyticsEvent {
/// Tracked when the user taps to collect a payment
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func collectPaymentTapped(forGatewayID: String?) -> WooAnalyticsEvent {
+ static func collectPaymentTapped(forGatewayID: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentTapped,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID)
+ Keys.gatewayID: forGatewayID
]
)
}
@@ -705,10 +699,10 @@ extension WooAnalyticsEvent {
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
/// `error` is the error to be included in the event properties.
///
- static func collectPaymentFailed(forGatewayID: String?, error: Error) -> WooAnalyticsEvent {
+ static func collectPaymentFailed(forGatewayID: String, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentFailed,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID),
+ Keys.gatewayID: forGatewayID,
Keys.errorDescription: error.localizedDescription
]
)
@@ -717,10 +711,10 @@ extension WooAnalyticsEvent {
/// Tracked when the payment collection is cancelled
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func collectPaymentCanceled(forGatewayID: String?) -> WooAnalyticsEvent {
+ static func collectPaymentCanceled(forGatewayID: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentCanceled,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID)
+ Keys.gatewayID: forGatewayID
]
)
}
@@ -728,10 +722,10 @@ extension WooAnalyticsEvent {
/// Tracked when payment collection succeeds
/// `forGatewayID` is the plugin (e.g. "woocommerce-payments" or "woocommerce-gateway-stripe") to be included in the event properties in Tracks.
///
- static func collectPaymentSuccess(forGatewayID: String?) -> WooAnalyticsEvent {
+ static func collectPaymentSuccess(forGatewayID: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentSuccess,
properties: [
- Keys.gatewayID: gatewayID(forGatewayID: forGatewayID)
+ Keys.gatewayID: forGatewayID
]
)
}
diff --git a/WooCommerce/Classes/ViewRelated/CardPresentPayments/CardReaderConnectionController.swift b/WooCommerce/Classes/ViewRelated/CardPresentPayments/CardReaderConnectionController.swift
index b557f084f..16e28f824 100644
--- a/WooCommerce/Classes/ViewRelated/CardPresentPayments/CardReaderConnectionController.swift
+++ b/WooCommerce/Classes/ViewRelated/CardPresentPayments/CardReaderConnectionController.swift
@@ -104,7 +104,7 @@ final class CardReaderConnectionController {
private var onCompletion: ((Result<Bool, Error>) -> Void)?
/// Gateway ID to include in tracks events
- private var gatewayID: String?
+ private var gatewayID: String
init(
forSiteID: Int64,
@@ -426,10 +426,11 @@ private extension CardReaderConnectionController {
let cancel = softwareUpdateCancelable.map { cancelable in
return { [weak self] in
- self?.state = .cancel
+ guard let self = self else { return }
+ self.state = .cancel
ServiceLocator.analytics.track(
event: WooAnalyticsEvent.InPersonPayments.cardReaderSoftwareUpdateCancelTapped(
- forGatewayID: self?.gatewayID,
+ forGatewayID: self.gatewayID,
updateType: .required
)
)
@@ -439,7 +440,7 @@ private extension CardReaderConnectionController {
} else {
ServiceLocator.analytics.track(
event: WooAnalyticsEvent.InPersonPayments.cardReaderSoftwareUpdateCanceled(
- forGatewayID: self?.gatewayID,
+ forGatewayID: self.gatewayID,
updateType: .required
)
)
diff --git a/WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewModel.swift b/WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewModel.swift
index 577fb6805..b4e4e2bd8 100644
--- a/WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewModel.swift
+++ b/WooCommerce/Classes/ViewRelated/Dashboard/Settings/CardReadersV2/CardReaderSettingsConnectedViewModel.swift
@@ -29,15 +29,13 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
var connectedReaderSoftwareVersion: String?
/// The connected gateway ID (plugin slug) - useful for the view controller's tracks events
- var connectedGatewayID: String?
+ var connectedGatewayID: String
let delayToShowUpdateSuccessMessage: DispatchTimeInterval
/// The datasource that will be used to help render the related screens
///
- private(set) lazy var dataSource: CardReaderSettingsDataSource = {
- return CardReaderSettingsDataSource(siteID: siteID)
- }()
+ let dataSource: CardReaderSettingsDataSource
var updateType: SoftwareUpdateTypeProperty {
optionalReaderUpdateAvailable ? .optional : .required
@@ -50,6 +48,9 @@ final class CardReaderSettingsConnectedViewModel: CardReaderSettingsPresentedVie
self.knownReaderProvider = knownReaderProvider
self.siteID = ServiceLocator.stores.sessionManager.defaultStoreID ?? Int64.min
self.delayToShowUpdateSuccessMessage = delayToShowUpdateSuccessMessage
+ let dataSource = CardReaderSettingsDataSource(siteID: siteID)
+ self.dataSource = dataSource
+ self.connectedGatewayID = dataSource.cardPresentPaymentGatewayID()
configureResultsControllers()
loadPaymentGatewayAccounts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment