@@ -116,7 +116,8 @@ extension ApphudInternal {
116116 transactionProductIdentifier: productID,
117117 transactionState: isRecentlyPurchased ? . purchased : nil ,
118118 receiptString: receipt,
119- notifyDelegate: true ) { _ in
119+ notifyDelegate: true ,
120+ fromScreen: false ) { _ in
120121 continuation. resume ( returning: true )
121122 }
122123 }
@@ -166,7 +167,7 @@ extension ApphudInternal {
166167 apphudLog ( " App Store receipt is missing, but got transaction. Will try to submit transaction instead.. " , forceDisplay: true )
167168 }
168169
169- self . submitReceipt ( product: nil , apphudProduct: nil , transaction: transaction, receiptString: receiptString, notifyDelegate: true , eligibilityCheck: true , callback: { error in
170+ self . submitReceipt ( product: nil , apphudProduct: nil , transaction: transaction, receiptString: receiptString, notifyDelegate: true , eligibilityCheck: true , fromScreen : false , callback: { error in
170171 let result = self . purchaseResult ( productId: transaction. payment. productIdentifier, transaction: transaction, error: error)
171172 callback ( result)
172173 } )
@@ -199,18 +200,18 @@ extension ApphudInternal {
199200
200201 performWhenUserRegistered {
201202
202- self . submitReceipt ( product: nil , apphudProduct: nil , transaction: transaction, receiptString: receiptString, notifyDelegate: true ) { error in
203+ self . submitReceipt ( product: nil , apphudProduct: nil , transaction: transaction, receiptString: receiptString, notifyDelegate: true , fromScreen : false ) { error in
203204 self . restorePurchasesCallback ? ( self . currentUser? . subscriptions, self . currentUser? . purchases, error)
204205 self . restorePurchasesCallback = nil
205206 }
206207 }
207208 }
208209
209- internal func submitReceipt( product: SKProduct , transaction: SKPaymentTransaction ? , apphudProduct: ApphudProduct ? = nil , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
210+ internal func submitReceipt( product: SKProduct , transaction: SKPaymentTransaction ? , apphudProduct: ApphudProduct ? = nil , fromScreen : Bool , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
210211
211212 let block : ( String ? ) -> Void = { receiptStr in
212213 if transaction != nil {
213- self . submitReceipt ( product: product, apphudProduct: apphudProduct, transaction: transaction, receiptString: receiptStr, notifyDelegate: true ) { error in
214+ self . submitReceipt ( product: product, apphudProduct: apphudProduct, transaction: transaction, receiptString: receiptStr, notifyDelegate: true , fromScreen : fromScreen ) { error in
214215 Task { @MainActor in
215216 let result = self . purchaseResult ( productId: product. productIdentifier, transaction: transaction, error: error)
216217 callback ? ( result)
@@ -242,7 +243,7 @@ extension ApphudInternal {
242243 }
243244 }
244245
245- internal func submitReceipt( product: SKProduct ? , apphudProduct: ApphudProduct ? , transaction: SKPaymentTransaction ? , receiptString: String ? , notifyDelegate: Bool , eligibilityCheck: Bool = false , callback: ApphudNSErrorCallback ? ) {
246+ internal func submitReceipt( product: SKProduct ? , apphudProduct: ApphudProduct ? , transaction: SKPaymentTransaction ? , receiptString: String ? , notifyDelegate: Bool , eligibilityCheck: Bool = false , fromScreen : Bool , callback: ApphudNSErrorCallback ? ) {
246247
247248 let productId = product? . productIdentifier ?? transaction? . payment. productIdentifier
248249 let finalProduct = product ?? ApphudStoreKitWrapper . shared. products. first ( where: { $0. productIdentifier == productId } )
@@ -257,6 +258,7 @@ extension ApphudInternal {
257258 receiptString: receiptString,
258259 notifyDelegate: notifyDelegate,
259260 eligibilityCheck: eligibilityCheck,
261+ fromScreen: fromScreen,
260262 callback: callback)
261263 }
262264 }
@@ -278,6 +280,7 @@ extension ApphudInternal {
278280 receiptString: String ? ,
279281 notifyDelegate: Bool ,
280282 eligibilityCheck: Bool = false ,
283+ fromScreen: Bool ,
281284 callback: ApphudNSErrorCallback ? ) async {
282285
283286 await MainActor . run {
@@ -322,6 +325,11 @@ extension ApphudInternal {
322325 }
323326
324327 if hasMadePurchase, let purchasedApphudProduct = apphudProduct ?? purchasingProduct, purchasedApphudProduct. productId == transactionProductIdentifier {
328+
329+ if fromScreen {
330+ params [ " screen_id " ] = purchasedApphudProduct. paywall? . screen? . id
331+ }
332+
325333 params [ " product_bundle_id " ] = purchasedApphudProduct. id
326334 params [ " paywall_id " ] = purchasedApphudProduct. paywallId
327335 params [ " placement_id " ] = purchasedApphudProduct. placementId
@@ -445,27 +453,27 @@ extension ApphudInternal {
445453
446454 @MainActor
447455 @available ( iOS 13 . 0 . 0 , macOS 11 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
448- internal func purchase( productId: String , product: ApphudProduct ? , validate: Bool , isPurchasing: Binding < Bool > ? = nil , value: Double ? = nil ) async -> ApphudPurchaseResult {
456+ internal func purchase( productId: String , product: ApphudProduct ? , validate: Bool , isPurchasing: Binding < Bool > ? = nil , fromScreen : Bool , value: Double ? = nil ) async -> ApphudPurchaseResult {
449457 await withUnsafeContinuation { continuation in
450458 isPurchasing? . wrappedValue = true
451- purchase ( productId: productId, product: product, validate: validate, callback: { result in
459+ purchase ( productId: productId, product: product, validate: validate, purchasingFromScreen : fromScreen , callback: { result in
452460 isPurchasing? . wrappedValue = false
453461 continuation. resume ( returning: result)
454462 } )
455463 }
456464 }
457465
458- @MainActor internal func purchase( productId: String , product: ApphudProduct ? , validate: Bool , value: Double ? = nil , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
459-
466+ @MainActor internal func purchase( productId: String , product: ApphudProduct ? , validate: Bool , purchasingFromScreen : Bool , value: Double ? = nil , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
467+
460468 let skProduct = product? . skProduct ?? ApphudStoreKitWrapper . shared. products. first ( where: { $0. productIdentifier == productId } )
461469
462470 if let skProduct = skProduct {
463- purchase ( product: skProduct, apphudProduct: product, validate: validate, value: value, callback: callback)
471+ purchase ( product: skProduct, apphudProduct: product, validate: validate, fromScreen : purchasingFromScreen , value: value, callback: callback)
464472 } else {
465473 apphudLog ( " Product with id \( productId) not found, re-fetching from App Store... " )
466474 ApphudStoreKitWrapper . shared. fetchProducts ( productIds: [ productId] ) { prds in
467475 if let sk = prds? . first ( where: { $0. productIdentifier == productId } ) {
468- self . purchase ( product: sk, apphudProduct: product, validate: validate, value: value, callback: callback)
476+ self . purchase ( product: sk, apphudProduct: product, validate: validate, fromScreen : purchasingFromScreen , value: value, callback: callback)
469477 } else {
470478 let message = " Unable to start payment because product identifier is invalid: [ \( [ productId] ) ] "
471479 apphudLog ( message, forceDisplay: true )
@@ -476,12 +484,12 @@ extension ApphudInternal {
476484 }
477485 }
478486
479- internal func purchasePromo( skProduct: SKProduct ? , apphudProduct: ApphudProduct ? , discountID: String , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
487+ internal func purchasePromo( skProduct: SKProduct ? , apphudProduct: ApphudProduct ? , discountID: String , fromScreen : Bool , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
480488
481489 let skCallback : ( ( SKProduct ) -> Void ) = { skProduct in
482490 self . signPromoOffer ( productID: skProduct. productIdentifier, discountID: discountID) { ( paymentDiscount, _) in
483491 if let paymentDiscount = paymentDiscount {
484- self . purchasePromo ( skProduct: skProduct, product: apphudProduct, discount: paymentDiscount, callback: callback)
492+ self . purchasePromo ( skProduct: skProduct, product: apphudProduct, discount: paymentDiscount, fromScreen : fromScreen , callback: callback)
485493 } else {
486494 callback ? ( ApphudPurchaseResult ( nil , nil , nil , ApphudError ( message: " Could not sign offer id: \( discountID) , product id: \( skProduct. productIdentifier) " ) ) )
487495 }
@@ -504,8 +512,11 @@ extension ApphudInternal {
504512
505513 // MARK: - Private purchase methods
506514
507- private func purchase( product: SKProduct , apphudProduct: ApphudProduct ? , validate: Bool , value: Double ? = nil , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
508- ApphudLoggerService . shared. paywallCheckoutInitiated ( apphudProduct: apphudProduct, productId: product. productIdentifier)
515+ private func purchase( product: SKProduct , apphudProduct: ApphudProduct ? , validate: Bool , fromScreen: Bool , value: Double ? = nil , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
516+
517+ let screenId = fromScreen ? apphudProduct? . paywall? . screen? . id : nil
518+
519+ ApphudLoggerService . shared. paywallCheckoutInitiated ( apphudProduct: apphudProduct, productId: product. productIdentifier, screenId: screenId)
509520
510521 purchasingProduct = apphudProduct
511522
@@ -517,16 +528,16 @@ extension ApphudInternal {
517528
518529 Task { @MainActor in
519530 if validate {
520- self . handleTransaction ( product: product, transaction: transaction, error: error, apphudProduct: apphudProduct, callback: callback)
531+ self . handleTransaction ( product: product, transaction: transaction, error: error, apphudProduct: apphudProduct, fromScreen : fromScreen , callback: callback)
521532 } else {
522- self . handleTransaction ( product: product, transaction: transaction, error: error, apphudProduct: apphudProduct, callback: nil )
533+ self . handleTransaction ( product: product, transaction: transaction, error: error, apphudProduct: apphudProduct, fromScreen : fromScreen , callback: nil )
523534 callback ? ( ApphudPurchaseResult ( nil , nil , transaction, error) )
524535 }
525536 }
526537 }
527538 }
528539
529- private func purchasePromo( skProduct: SKProduct , product: ApphudProduct ? , discount: SKPaymentDiscount , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
540+ private func purchasePromo( skProduct: SKProduct , product: ApphudProduct ? , discount: SKPaymentDiscount , fromScreen : Bool , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
530541
531542 purchasingProduct = product
532543
@@ -536,7 +547,7 @@ extension ApphudInternal {
536547 }
537548
538549 Task { @MainActor in
539- self . handleTransaction ( product: skProduct, transaction: transaction, error: error, apphudProduct: product, callback: callback)
550+ self . handleTransaction ( product: skProduct, transaction: transaction, error: error, apphudProduct: product, fromScreen : fromScreen , callback: callback)
540551 }
541552 }
542553 }
@@ -545,9 +556,9 @@ extension ApphudInternal {
545556 observerModePurchaseIdentifiers = ( paywallId, placementId)
546557 }
547558
548- @MainActor private func handleTransaction( product: SKProduct , transaction: SKPaymentTransaction , error: Error ? , apphudProduct: ApphudProduct ? , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
559+ @MainActor private func handleTransaction( product: SKProduct , transaction: SKPaymentTransaction , error: Error ? , apphudProduct: ApphudProduct ? , fromScreen : Bool , callback: ( ( ApphudPurchaseResult ) -> Void ) ? ) {
549560 if transaction. transactionState == . purchased || transaction. failedWithUnknownReason {
550- self . submitReceipt ( product: product, transaction: transaction, apphudProduct: apphudProduct) { ( result) in
561+ self . submitReceipt ( product: product, transaction: transaction, apphudProduct: apphudProduct, fromScreen : fromScreen ) { ( result) in
551562 ApphudStoreKitWrapper . shared. finishTransaction ( transaction)
552563 callback ? ( result)
553564 }
0 commit comments