Skip to content

Commit

Permalink
Add convenience methods for sending purchase preset signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed May 18, 2024
1 parent 3a95ed8 commit 2b837c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sources/TelemetryClient/Presets/TelemetryDeck+Purchases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if canImport(StoreKit)
import StoreKit
import Foundation

@available(iOS 15, macOS 12, tvOS 15, visionOS 1, watchOS 8, *)
extension TelemetryDeck {
public static func purchaseCompleted(
transaction: StoreKit.Transaction,
parameters: [String: String] = [:],
customUserID: String? = nil
) {
let priceValueInNativeCurrency = NSDecimalNumber(decimal: transaction.price ?? Decimal()).doubleValue

let priceValueInUSD: Double
if transaction.currency == Locale.Currency("USD") {
priceValueInUSD = priceValueInNativeCurrency
} else {
priceValueInUSD = priceValueInNativeCurrency * 1.0 // TODO: implement hard-coded lookup table
}

var purchaseParameters: [String: String] = [
"TelemetryDeck.Purchase.type": transaction.subscriptionGroupID != nil ? "subscription" : "one-time-purchase",
"TelemetryDeck.Purchase.countryCode": transaction.storefront.countryCode,
]

if let currency = transaction.currency {
purchaseParameters["TelemetryDeck.Purchase.currencyCode"] = currency.identifier
}

self.signal(
"TelemetryDeck.Purchase.completed",
parameters: purchaseParameters.merging(parameters) { $1 },
floatValue: priceValueInUSD,
customUserID: customUserID
)
}
}
#endif

0 comments on commit 2b837c2

Please sign in to comment.