From 232533d409eebff53fce612c7138e173a66acabe Mon Sep 17 00:00:00 2001 From: Ryan Tobin Date: Tue, 3 Dec 2024 09:22:05 -0800 Subject: [PATCH] Set new candidates date when autologging is off Summary: During testing, we noticed an edge case where if the app has autologging turned off and then re-enables it, all of a user's transaction history will be logged, which would likely result in over reporting of events. We should assume that these transactions have already been handled and only consider the transactions that occur after the advertiser enables autologging. To achieve this, we set the new candidates date to ```now``` whenever we observe that autologging is turned off. In doing this, we will only consider transactions that have occurred after this date. This diff implements this functionality. Reviewed By: jjiang10 Differential Revision: D66309371 fbshipit-source-id: b578b5782e8b4ead3bc58569a3bc3d9c17b1762e --- .../FBSDKCoreKit/AppEvents/FBSDKAppEvents.m | 1 + .../Internal/IAP/IAPTransactionCache.swift | 1 + .../Internal/AppEvents/AppEventsTests.swift | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/FBSDKCoreKit/FBSDKCoreKit/AppEvents/FBSDKAppEvents.m b/FBSDKCoreKit/FBSDKCoreKit/AppEvents/FBSDKAppEvents.m index 164ea0b7f..8dcb94bd2 100644 --- a/FBSDKCoreKit/FBSDKCoreKit/AppEvents/FBSDKAppEvents.m +++ b/FBSDKCoreKit/FBSDKCoreKit/AppEvents/FBSDKAppEvents.m @@ -991,6 +991,7 @@ - (void)fetchServerConfiguration:(FBSDKCodeBlock)callback } }]; } else { + [self.iapTransactionCache setNewCandidatesDate:[NSDate date]]; [self.iapDedupeProcessor disable]; [self.paymentObserver stopObservingTransactions]; [self.transactionObserver stopObserving]; diff --git a/FBSDKCoreKit/FBSDKCoreKit/AppEvents/Internal/IAP/IAPTransactionCache.swift b/FBSDKCoreKit/FBSDKCoreKit/AppEvents/Internal/IAP/IAPTransactionCache.swift index e2410c19a..2cfccfa87 100644 --- a/FBSDKCoreKit/FBSDKCoreKit/AppEvents/Internal/IAP/IAPTransactionCache.swift +++ b/FBSDKCoreKit/FBSDKCoreKit/AppEvents/Internal/IAP/IAPTransactionCache.swift @@ -130,6 +130,7 @@ extension IAPTransactionCache: _IAPTransactionCaching { return } guard let newValue else { + dependencies.dataStore.fb_removeObject(forKey: IAPConstants.newCandidatesDateCacheKey) return } dependencies.dataStore.fb_setObject(newValue, forKey: IAPConstants.newCandidatesDateCacheKey) diff --git a/FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/AppEventsTests.swift b/FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/AppEventsTests.swift index f24469d2e..bb310ebfa 100644 --- a/FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/AppEventsTests.swift +++ b/FBSDKCoreKit/FBSDKCoreKitTests/Internal/AppEvents/AppEventsTests.swift @@ -152,7 +152,7 @@ final class AppEventsTests: XCTestCase { iapDedupeProcessor = nil resetTestHelpers() - + IAPTransactionCache.shared.reset() super.tearDown() } @@ -1797,6 +1797,7 @@ final class AppEventsTests: XCTestCase { } func testFetchingConfigurationStopPaymentObservingIfAutoLogAppEventsDisabled() { + let now = Date() settings.isAutoLogAppEventsEnabled = false let serverConfiguration = ServerConfigurationFixtures.configuration( withDictionary: ["implicitPurchaseLoggingEnabled": true] @@ -1820,6 +1821,11 @@ final class AppEventsTests: XCTestCase { transactionObserver.didStopObserving, "Fetching a configuration should stop transaction observing if auto log app events is disabled" ) + guard let newCandidatesDate = IAPTransactionCache.shared.newCandidatesDate else { + XCTFail("newCandidatesDate should have been set") + return + } + XCTAssertTrue(newCandidatesDate > now) } func testEnablingIAPDedupeShouldEnableIAPDedupe() { @@ -1883,6 +1889,7 @@ final class AppEventsTests: XCTestCase { } func testEnablingIAPDedupeShouldNotEnableIAPDedupeWhenImplicitPurchaseIsDiabled() { + let now = Date() settings.isAutoLogAppEventsEnabled = true let serverConfiguration = ServerConfigurationFixtures.configuration( withDictionary: ["implicitPurchaseLoggingEnabled": 0] @@ -1895,6 +1902,11 @@ final class AppEventsTests: XCTestCase { XCTAssertFalse(iapDedupeProcessor.enableWasCalled) XCTAssertTrue(iapDedupeProcessor.disableWasCalled) + guard let newCandidatesDate = IAPTransactionCache.shared.newCandidatesDate else { + XCTFail("newCandidatesDate should have been set") + return + } + XCTAssertTrue(newCandidatesDate > now) } func testFetchingConfigurationIncludingSKAdNetworkIfSKAdNetworkReportEnabled() {