Skip to content

Commit

Permalink
Fix decimal point issue
Browse files Browse the repository at this point in the history
Summary:
We observed that sometimes when logging the value of a transaction, it was logged with an excess number of decimal points (i.e. 4.99 was 4.9900000000000002 or 1.99 was 1.9899999999999998). This can hurt the event quality.

The root cause is because doubles do not have as much precision as decimals and when converting the price from a decimal to a double, we lost precision. To fix it, we first round the number to 3 decimal places (this is the most any currency has) and then we log it as an ```NSDecimalNumber``` as opposed to a ```Double``` to maintain precision.

Reviewed By: jjiang10

Differential Revision: D64154628

fbshipit-source-id: 9d15b2eb1d94e3631731403fb9ad4a49293f90cd
  • Loading branch information
ryantobinmeta authored and facebook-github-bot committed Oct 22, 2024
1 parent 5b36751 commit 1b82e1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension IAPTransactionLogger {
if event.hasIntroductoryOffer {
parameters[.hasFreeTrial] = event.hasFreeTrial ? "1" : "0"
parameters[.trialPeriod] = durationOfSubscriptionPeriod(event.introductoryOfferSubscriptionPeriod)
parameters[.trialPrice] = event.introductoryOfferPrice?.doubleValue
parameters[.trialPrice] = event.introductoryOfferPrice?.currencyNumber
}
} else {
parameters[.inAppPurchaseType] = IAPType.product.rawValue
Expand Down Expand Up @@ -106,7 +106,7 @@ extension IAPTransactionLogger {
let parameters = getParameters(for: event)
logImplicitTransactionEvent(
eventName: event.eventName,
valueToSum: event.amount.doubleValue,
valueToSum: event.amount,
parameters: parameters
)
}
Expand All @@ -119,7 +119,7 @@ extension IAPTransactionLogger {
let parameters = getParameters(for: event)
logImplicitTransactionEvent(
eventName: event.eventName,
valueToSum: event.amount.doubleValue,
valueToSum: event.amount,
parameters: parameters
)
}
Expand All @@ -128,20 +128,25 @@ extension IAPTransactionLogger {
let parameters = getParameters(for: event)
logImplicitTransactionEvent(
eventName: event.eventName,
valueToSum: event.amount.doubleValue,
valueToSum: event.amount,
parameters: parameters
)
}

private func logImplicitTransactionEvent(
eventName: AppEvents.Name,
valueToSum: Double,
valueToSum: Decimal,
parameters: [AppEvents.ParameterName: Any]
) {
guard let dependencies = try? Self.getDependencies() else {
return
}
dependencies.eventLogger.logEvent(eventName, valueToSum: valueToSum, parameters: parameters)
dependencies.eventLogger.logEvent(
eventName,
valueToSum: valueToSum.currencyNumber,
parameters: parameters,
accessToken: nil
)
if dependencies.eventLogger.flushBehavior != .explicitOnly {
dependencies.eventLogger.flush(for: .eagerlyFlushingEvent)
}
Expand Down Expand Up @@ -207,7 +212,17 @@ enum IAPType: String {
// MARK: - Decimal

extension Decimal {
var doubleValue: Double {
NSDecimalNumber(decimal: self).doubleValue
var currencyNumber: NSDecimalNumber {
var decimalNumber = NSDecimalNumber(decimal: self)
let behavior = NSDecimalNumberHandler(
roundingMode: .plain,
scale: 3,
raiseOnExactness: false,
raiseOnOverflow: false,
raiseOnUnderflow: false,
raiseOnDivideByZero: false
)
decimalNumber = decimalNumber.rounding(accordingToBehavior: behavior)
return decimalNumber
}
}
5 changes: 5 additions & 0 deletions FBSDKCoreKit/FBSDKCoreKit/include/FBSDKEventLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ NS_SWIFT_NAME(EventLogging)
valueToSum:(double)valueToSum
parameters:(nullable NSDictionary<FBSDKAppEventParameterName, id> *)parameters;

- (void)logEvent:(FBSDKAppEventName)eventName
valueToSum:(nullable NSNumber *)valueToSum
parameters:(nullable NSDictionary<FBSDKAppEventParameterName, id> *)parameters
accessToken:(nullable FBSDKAccessToken *)accessToken;

- (void)logInternalEvent:(FBSDKAppEventName)eventName
isImplicitlyLogged:(BOOL)isImplicitlyLogged;

Expand Down
11 changes: 11 additions & 0 deletions TestTools/TestTools/TestEventLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,15 @@ open class TestEventLogger: NSObject, EventLogging { // swiftlint:disable:this p
) {
capturedEventName = eventName
}

public func logEvent(
_ eventName: AppEvents.Name,
valueToSum: NSNumber?,
parameters: [AppEvents.ParameterName: Any]?,
accessToken: AccessToken?
) {
capturedEventName = eventName
capturedValueToSum = valueToSum?.doubleValue
capturedParameters = parameters
}
}

0 comments on commit 1b82e1f

Please sign in to comment.