Skip to content

Commit

Permalink
fix missing IDFA
Browse files Browse the repository at this point in the history
c933cebae6b2b1af5ce984f8b9ab6fb6c1319331
  • Loading branch information
alex-oleynik committed May 22, 2024
1 parent b70e855 commit 7c31414
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions AppMetricaCore/Sources/Model/Reporter/AMASessionStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

- (BOOL)saveSessionAsLastSession:(AMASession *)session error:(NSError **)error;
- (BOOL)updateSession:(AMASession *)session pauseTime:(NSDate *)pauseTime error:(NSError **)error;
- (BOOL)updateSession:(AMASession *)session appState:(AMAApplicationState *)appState error:(NSError **)error;
- (BOOL)finishSession:(AMASession *)session atDate:(NSDate *)date error:(NSError **)error;

@end
18 changes: 18 additions & 0 deletions AppMetricaCore/Sources/Model/Reporter/AMASessionStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ - (BOOL)updateSession:(AMASession *)session pauseTime:(NSDate *)pauseTime error:
}];
}

- (BOOL)updateSession:(AMASession *)session appState:(AMAApplicationState *)appState error:(NSError **)error
{
NSError *internalError = nil;
session.appState = appState;
NSData *data = [self.serializer commonDataForSession:session error:&internalError];

if (data == nil || internalError != nil) {
AMALogError(@"Failed to serialize session data: %@", internalError);
if (error != nil) {
*error = internalError;
}
return NO;
}

NSDictionary *updateDictionary = @{ kAMACommonTableFieldData: data };
return [self updateSessionFields:updateDictionary forSession:session error:error onSuccess:nil];
}

- (BOOL)finishSession:(AMASession *)session atDate:(NSDate *)date error:(NSError **)error
{
NSDate *pauseTime = date ?: session.pauseTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface AMASessionSerializer : NSObject

- (NSData *)commonDataForSession:(AMASession *)session error:(NSError **)error;
- (NSDictionary *)dictionaryForSession:(AMASession *)session error:(NSError **)error;
- (AMASession *)sessionForDictionary:(NSDictionary *)dictionary error:(NSError **)error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (NSDictionary *)dictionaryForSession:(AMASession *)session error:(NSError **)e
dictionary[kAMACommonTableFieldDataEncryptionType] = @(self.encryptionType);

NSError *internalError = nil;
dictionary[kAMACommonTableFieldData] = [self.encoder encodeData:[self dataForSession:session] error:&internalError];
dictionary[kAMACommonTableFieldData] = [self commonDataForSession:session error:&internalError];

if (internalError != nil) {
AMALogError(@"Failed to serialize session: %@", internalError);
Expand All @@ -67,6 +67,11 @@ - (NSDictionary *)dictionaryForSession:(AMASession *)session error:(NSError **)e
return [dictionary copy];
}

- (NSData *)commonDataForSession:(AMASession *)session error:(NSError **)error
{
return [self.encoder encodeData:[self dataForSession:session] error:error];
}

- (NSNumber *)numberForDate:(NSDate *)date
{
if (date == nil) {
Expand Down
11 changes: 11 additions & 0 deletions AppMetricaCore/Sources/Reporter/AMAReporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,17 @@ - (void)privacyTimerDidFire:(AMAPrivacyTimer *)privacyTimer
BOOL needSent = [self.adProvider isAdvertisingTrackingEnabled] && self.privacyTimer.timerStorage.isResendPeriodOutdated;
AMALogInfo(@"send privacy event: %@ %d", self.apiKey, needSent);
if (needSent) {
NSError *error = nil;
AMASession *session = [self lastSession];
AMAApplicationState *newState = [AMAApplicationStateManager applicationState];
[self.reporterStorage.sessionStorage updateSession:session
appState:newState
error:&error];

if (error != nil) {
AMALogError(@"Failed to update session %@ with appState %@ : %@", session, newState, error);
}

[self reportEventWithType:AMAEventTypeApplePrivacy
name:nil
value:nil
Expand Down
20 changes: 20 additions & 0 deletions AppMetricaCore/Tests/AMASessionStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "AMADatabaseHelper.h"
#import "AMAEnvironmentContainer.h"
#import "AMAAppStateManagerTestHelper.h"
#import "AMAApplicationStateManager.h"
#import "AMAMetricaConfigurationTestUtilities.h"
#import <AppMetricaTestUtils/AppMetricaTestUtils.h>

Expand Down Expand Up @@ -483,6 +484,25 @@
[[session.appState should] equal:appState];
});
});
context(@"App state", ^{
NSString *newIDFA = @"3264429A-3997-4786-AC2A-1790482363BC";
AMAMutableApplicationState *newState = [[AMAApplicationStateManager applicationState] mutableCopy];
newState.IFA = newIDFA;

beforeEach(^{
NSError *error = nil;
BOOL result = [storage updateSession:session appState:newState error:&error];
[[error should] beNil];
[[theValue(result) should] equal:theValue(YES)];

session = [storage lastSessionWithError:nil];
});

it(@"should update IDFA", ^{
[[session.appState should] equal:newState];
[[session.appState.IFA should] equal:newIDFA];
});
});
});
});
context(@"Error", ^{
Expand Down

0 comments on commit 7c31414

Please sign in to comment.