Skip to content

Commit c2d527e

Browse files
authored
fix(analytics): improve logTransaction iOS implementation (#968)
1 parent 34dfb6b commit c2d527e

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

packages/analytics/ios/Plugin/FirebaseAnalytics.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import FirebaseAnalytics
77

88
@objc public class FirebaseAnalytics: NSObject {
99

10-
override init() {
10+
private let plugin: FirebaseAnalyticsPlugin
11+
12+
init(plugin: FirebaseAnalyticsPlugin) {
13+
self.plugin = plugin
14+
super.init()
1115
if FirebaseApp.app() == nil {
1216
FirebaseApp.configure()
1317
}
@@ -62,22 +66,23 @@ import FirebaseAnalytics
6266
}
6367

6468
@available(iOS 15.0, *)
65-
public func logTransaction(transactionId: String) async throws {
66-
guard let id = UInt64(transactionId) else {
67-
throw NSError(domain: "FirebaseAnalytics", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid transaction identifier."])
68-
}
69-
var matchedTransaction: Transaction?
70-
for await result in Transaction.all {
71-
let transaction = FirebaseAnalyticsHelper.getTransaction(from: result)
72-
if transaction.id == id {
73-
matchedTransaction = transaction
74-
break
69+
public func logTransaction(transactionId: UInt64, completion: @escaping (String?) -> Void) {
70+
Task {
71+
var matchedTransaction: Transaction?
72+
for await result in Transaction.all {
73+
let transaction = FirebaseAnalyticsHelper.getTransaction(from: result)
74+
if transaction.id == transactionId {
75+
matchedTransaction = transaction
76+
break
77+
}
7578
}
79+
guard let transaction = matchedTransaction else {
80+
completion(self.plugin.errorTransactionNotFound)
81+
return
82+
}
83+
Analytics.logTransaction(transaction)
84+
completion(nil)
7685
}
77-
guard let transaction = matchedTransaction else {
78-
throw NSError(domain: "FirebaseAnalytics", code: 0, userInfo: [NSLocalizedDescriptionKey: "Transaction not found."])
79-
}
80-
Analytics.logTransaction(transaction)
8186
}
8287

8388
@objc public func initiateOnDeviceConversionMeasurement(email: String) {

packages/analytics/ios/Plugin/FirebaseAnalyticsPlugin.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class FirebaseAnalyticsPlugin: CAPPlugin, CAPBridgedPlugin {
3636
public let errorConsentTypeMissing = "consentType must be provided."
3737
public let errorConsentStatusMissing = "consentStatus must be provided."
3838
public let errorTransactionIdMissing = "transactionId must be provided."
39+
public let errorInvalidTransactionId = "transactionId is not a valid numeric identifier."
40+
public let errorTransactionNotFound = "Transaction not found."
3941
public let errorEmailAddressMissing = "emailAddress must be provided."
4042
public let errorInvalidEmailFormat = "Invalid email format. Please provide a valid email address."
4143
public let errorPhoneNumberMissing = "phoneNumber must be provided."
@@ -45,7 +47,7 @@ public class FirebaseAnalyticsPlugin: CAPPlugin, CAPBridgedPlugin {
4547
private var implementation: FirebaseAnalytics?
4648

4749
override public func load() {
48-
implementation = FirebaseAnalytics()
50+
implementation = FirebaseAnalytics(plugin: self)
4951
}
5052

5153
@objc func getAppInstanceId(_ call: CAPPluginCall) {
@@ -132,17 +134,20 @@ public class FirebaseAnalyticsPlugin: CAPPlugin, CAPBridgedPlugin {
132134
call.reject(errorTransactionIdMissing)
133135
return
134136
}
137+
guard let transactionIdUInt64 = UInt64(transactionId) else {
138+
call.reject(errorInvalidTransactionId)
139+
return
140+
}
135141
if #available(iOS 15.0, *) {
136-
Task {
137-
do {
138-
try await implementation?.logTransaction(transactionId: transactionId)
142+
implementation?.logTransaction(transactionId: transactionIdUInt64) { errorMessage in
143+
if let errorMessage = errorMessage {
144+
call.reject(errorMessage)
145+
} else {
139146
call.resolve()
140-
} catch {
141-
call.reject(error.localizedDescription)
142147
}
143148
}
144149
} else {
145-
call.unimplemented("Not implemented on iOS < 15.0.")
150+
call.unavailable("Not available on iOS < 15.0.")
146151
}
147152
}
148153

0 commit comments

Comments
 (0)