Skip to content

Commit

Permalink
remove Apple Privacy event for extensions
Browse files Browse the repository at this point in the history
commit_hash:77c5dc45d15e6d83776e0418ac873d0ae05521b6
  • Loading branch information
Sergey70 committed Dec 10, 2024
1 parent 42b36a8 commit a23678a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 14 deletions.
21 changes: 15 additions & 6 deletions AppMetricaCore/Sources/Reporter/AMAReporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,17 @@ - (instancetype)initWithApiKey:(NSString *)apiKey
[[AMASessionExpirationHandler alloc] initWithConfiguration:[AMAMetricaConfiguration sharedInstance]
APIKey:apiKey];

AMAMetrikaPrivacyTimerStorage *timerStorage =
[[AMAMetrikaPrivacyTimerStorage alloc] initWithReporterMetricaConfiguration:[AMAMetricaConfiguration sharedInstance]
stateStorage:reporterStorage.stateStorage];
AMAPrivacyTimer *privacyTimer = [[AMAPrivacyTimer alloc] initWithTimerStorage:timerStorage
delegateExecutor:executor
adProvider:adProvider];

AMAPrivacyTimer *privacyTimer = nil;
if ([AMAPlatformDescription isExtension] == NO) {
AMAMetrikaPrivacyTimerStorage *timerStorage =
[[AMAMetrikaPrivacyTimerStorage alloc] initWithReporterMetricaConfiguration:[AMAMetricaConfiguration sharedInstance]
stateStorage:reporterStorage.stateStorage];

privacyTimer = [[AMAPrivacyTimer alloc] initWithTimerStorage:timerStorage
delegateExecutor:executor
adProvider:adProvider];
}

AMAAdServicesDataProvider *adServicesDataProvider = nil;
if (@available(iOS 14.3, *)) {
Expand Down Expand Up @@ -1110,6 +1115,10 @@ - (void)clearAppEnvironment

- (void)privacyTimerDidFire:(AMAPrivacyTimer *)privacyTimer
{
if ([AMAPlatformDescription isExtension]) {
return;
}

BOOL needSent = [self.adProvider isAdvertisingTrackingEnabled] && self.privacyTimer.timerStorage.isResendPeriodOutdated;
AMALogInfo(@"send privacy event: %@ %d", self.apiKey, needSent);
if (needSent) {
Expand Down
114 changes: 106 additions & 8 deletions AppMetricaCore/Tests/AMAReporterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#import "AMAAdProvider.h"
#import "AMAPrivacyTimerStorageMock.h"
#import "AMAIdentifiersTestUtilities.h"
#import "AMAPrivacyTimer.h"

@interface AMAReporterStorage (Test)

Expand Down Expand Up @@ -1844,14 +1845,83 @@ @interface AMAReporterStorage (Test)
});
#endif
});

context(@"Creating", ^{
__block AMAReporter *reporter = nil;

__block AMAAdProvider *adProvider = nil;
__block AMAReporterStorage *reporterStorage = nil;
__block AMAEventBuilder *eventBuilder = nil;
__block AMACurrentQueueExecutor *executor = nil;
__block AMAInternalEventsReporter *internalEventsReporter = nil;
__block AMAMetrikaPrivacyTimerStorage *privacyTimerStorage = nil;
__block AMAPrivacyTimer *privacyTimer = nil;

beforeEach(^{
adProvider = [AMAAdProvider nullMock];
[AMAAdProvider stub:@selector(sharedInstance) andReturn:adProvider];

reporterStorage = [AMAReporterStorage nullMock];
eventBuilder = [AMAEventBuilder nullMock];
internalEventsReporter = [AMAInternalEventsReporter nullMock];
executor = [AMACurrentQueueExecutor nullMock];

privacyTimerStorage = [AMAMetrikaPrivacyTimerStorage nullMock];
[privacyTimerStorage stub:@selector(initWithReporterMetricaConfiguration:stateStorage:) andReturn:privacyTimerStorage];
[AMAMetrikaPrivacyTimerStorage stub:@selector(alloc) andReturn:privacyTimerStorage];

privacyTimer = [AMAPrivacyTimer nullMock];
[privacyTimer stub:@selector(initWithTimerStorage:delegateExecutor:adProvider:) andReturn:privacyTimer];
[AMAPrivacyTimer stub:@selector(alloc) andReturn:privacyTimer];
});
afterEach(^{
[AMAPrivacyTimer clearStubs];
[AMAAdProvider clearStubs];
[AMAPlatformDescription clearStubs];
[AMAMetrikaPrivacyTimerStorage clearStubs];
[AMAPrivacyTimer clearStubs];
});

it(@"Should create privacy timer in app", ^{
[AMAPlatformDescription stub:@selector(isExtension) andReturn:theValue(NO)];

[[AMAMetrikaPrivacyTimerStorage should] receive:@selector(alloc)];
[[AMAPrivacyTimer should] receive:@selector(alloc)];

reporter = [[AMAReporter alloc] initWithApiKey:apiKey
main:YES
reporterStorage:reporterStorage
eventBuilder:eventBuilder
internalReporter:internalEventsReporter
attributionCheckExecutor:executor];

[[[reporter performSelector:@selector(privacyTimer)] should] equal:privacyTimer];
});

it(@"Should not create privacy timer in extension", ^{
[AMAPlatformDescription stub:@selector(isExtension) andReturn:theValue(YES)];

[[AMAMetrikaPrivacyTimerStorage shouldNot] receive:@selector(alloc)];
[[AMAPrivacyTimer shouldNot] receive:@selector(alloc)];

reporter = [[AMAReporter alloc] initWithApiKey:apiKey
main:YES
reporterStorage:reporterStorage
eventBuilder:eventBuilder
internalReporter:internalEventsReporter
attributionCheckExecutor:executor];

[[[reporter performSelector:@selector(privacyTimer)] should] beNil];
});

});

context(@"Privacy manifest", ^{
NSUUID *newIDFA = [NSUUID UUID];

__block id reporter = nil;
__block AMAPrivacyTimerStorageMock *privacyStorageMock;
NSUUID *newIDFA = [NSUUID UUID];

beforeEach(^{
void (^prepareReporter)() = ^{
reporter = [reporterTestHelper appReporterForApiKey:apiKey];
privacyStorageMock = [reporterTestHelper privacyTimerStorageMockForApiKey:apiKey];

Expand All @@ -1865,14 +1935,42 @@ @interface AMAReporterStorage (Test)

[reporter resumeSession];
[reporter privacyTimerDidFire:[reporterTestHelper privacyTimerForApiKey:apiKey]];
});
};

void (^cleanReporter)() = ^{
[AMAIdentifiersTestUtilities clearStubs];
};

it(@"Should fire event if tracking is enabled", ^{
[[[eventStorage() amatest_savedEventWithType:AMAEventTypeApplePrivacy] should] beNonNil];
context(@"In application", ^{
beforeEach(^{
prepareReporter();
});
afterEach(^{
cleanReporter();
});

it(@"Should fire event if tracking is enabled", ^{
[[[eventStorage() amatest_savedEventWithType:AMAEventTypeApplePrivacy] should] beNonNil];
});

it(@"Should update IDFA if tracking enabled", ^{
[[[sessionStorage() lastSessionWithError:nil].appState.IFA should] equal:newIDFA.UUIDString];
});
});

it(@"Should update IDFA if tracking enabled", ^{
[[[sessionStorage() lastSessionWithError:nil].appState.IFA should] equal:newIDFA.UUIDString];
context(@"In extension", ^{
beforeEach(^{
[AMAPlatformDescription stub:@selector(isExtension) andReturn:theValue(YES)];
prepareReporter();
});
afterEach(^{
cleanReporter();
[AMAPlatformDescription clearStubs];
});

it(@"Should not fire", ^{
[[[eventStorage() amatest_savedEventWithType:AMAEventTypeApplePrivacy] should] beNil];
});
});
});

Expand Down

0 comments on commit a23678a

Please sign in to comment.