-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience methods for sending purchase preset signals
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
Sources/TelemetryClient/Presets/TelemetryDeck+Purchases.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |