@@ -928,7 +928,7 @@ - (void)handlePushNotification:(NSDictionary *)userInfo {
928928 }
929929}
930930
931- # pragma mark - Apple Search Ad check
931+ #pragma mark - Apple Search Ad Check
932932
933933- (void )delayInitToCheckForSearchAds {
934934 self.delayForAppleAds = YES ;
@@ -977,55 +977,64 @@ - (BOOL)checkAppleSearchAdsAttribution {
977977 _Atomic __block BOOL hasBeenCalled = NO ;
978978 void (^__nullable completionBlock)(NSDictionary *attrDetails, NSError *error) =
979979 ^ void (NSDictionary *__nullable attrDetails, NSError *__nullable error) {
980- BNCLogDebug (@" Elapsed Apple Search Ad callback time: %1.3f s." , - [startDate timeIntervalSinceNow ]);
981- BOOL localHasBeenCalled = atomic_exchange (&hasBeenCalled, YES );
982- if (localHasBeenCalled) return ;
983- if (error) BNCLogError (@" Error while getting Apple Search Ad attribution: %@ ." , error);
984-
985- self.asyncRequestCount --;
986-
987- // If searchAdsDebugMode is on then force the result to a set value for testing:
988- if (self.searchAdsDebugMode ) {
989- // Round down to one day for testing.
990- NSTimeInterval const kOneHour = (60.0 *60.0 *1.0 );
991- NSTimeInterval t = trunc ([[NSDate date ] timeIntervalSince1970 ] / kOneHour ) * kOneHour ;
992- NSDate *date = [NSDate dateWithTimeIntervalSince1970: t];
993-
994- attrDetails = @{
995- @" Version3.1" : @{
996- @" iad-adgroup-id" : @1234567890 ,
997- @" iad-adgroup-name" : @" AdGroupName" ,
998- @" iad-attribution" : (id )kCFBooleanTrue ,
999- @" iad-campaign-id" : @1234567890 ,
1000- @" iad-campaign-name" : @" CampaignName" ,
1001- @" iad-click-date" : date,
1002- @" iad-conversion-date" : date,
1003- @" iad-creative-id" : @1234567890 ,
1004- @" iad-creative-name" : @" CreativeName" ,
1005- @" iad-keyword" : @" Keyword" ,
1006- @" iad-lineitem-id" : @1234567890 ,
1007- @" iad-lineitem-name" : @" LineName" ,
1008- @" iad-org-name" : @" OrgName"
1009- }
1010- };
1011- }
980+ @synchronized (self) {
981+ NSTimeInterval elapsedSeconds = - [startDate timeIntervalSinceNow ];
982+ BNCLogDebugSDK (@" Elapsed Apple Search Ad callback time: %1.3f s." , elapsedSeconds);
1012983
1013- if (attrDetails == nil ) attrDetails = @{};
1014- if (self.preferenceHelper .appleSearchAdDetails == nil )
1015- self.preferenceHelper .appleSearchAdDetails = @{};
1016- if (![self .preferenceHelper.appleSearchAdDetails isEqualToDictionary: attrDetails]) {
1017- self.preferenceHelper .appleSearchAdDetails = attrDetails;
1018- self.preferenceHelper .appleSearchAdNeedsSend = YES ;
1019- }
984+ if (attrDetails.count > 0 && !error) {
985+ [self .preferenceHelper addInstrumentationDictionaryKey: @" apple_search_ad"
986+ value: [[NSNumber numberWithInteger: elapsedSeconds*1000 ] stringValue ]];
987+ }
988+ if (self.searchAdsDebugMode ) {
989+ // If searchAdsDebugMode is on then force the result to a set value for testing.
990+ NSTimeInterval const kOneHour = (60.0 *60.0 *1.0 ); // Round down to one day for testing.
991+ NSTimeInterval t = trunc ([[NSDate date ] timeIntervalSince1970 ] / kOneHour ) * kOneHour ;
992+ NSDate *date = [NSDate dateWithTimeIntervalSince1970: t];
993+ attrDetails = @{
994+ @" Version3.1" : @{
995+ @" iad-adgroup-id" : @1234567890 ,
996+ @" iad-adgroup-name" : @" AdGroupName" ,
997+ @" iad-attribution" : (id )kCFBooleanTrue ,
998+ @" iad-campaign-id" : @1234567890 ,
999+ @" iad-campaign-name" : @" CampaignName" ,
1000+ @" iad-click-date" : date,
1001+ @" iad-conversion-date" : date,
1002+ @" iad-creative-id" : @1234567890 ,
1003+ @" iad-creative-name" : @" CreativeName" ,
1004+ @" iad-keyword" : @" Keyword" ,
1005+ @" iad-lineitem-id" : @1234567890 ,
1006+ @" iad-lineitem-name" : @" LineName" ,
1007+ @" iad-org-name" : @" OrgName"
1008+ }
1009+ };
1010+ }
1011+ if (!error) {
1012+ if (attrDetails == nil )
1013+ attrDetails = @{};
1014+ if (self.preferenceHelper .appleSearchAdDetails == nil )
1015+ self.preferenceHelper .appleSearchAdDetails = @{};
1016+ if (![self .preferenceHelper.appleSearchAdDetails isEqualToDictionary: attrDetails]) {
1017+ self.preferenceHelper .appleSearchAdDetails = attrDetails;
1018+ self.preferenceHelper .appleSearchAdNeedsSend = YES ;
1019+ }
1020+ }
1021+ // Only continue with initialization once:
1022+ BOOL localHasBeenCalled = atomic_exchange (&hasBeenCalled, YES );
1023+ if (localHasBeenCalled) return ;
10201024
1021- // If there's another async attribution check in flight, don't continue with init
1022- if (self.asyncRequestCount > 0 ) { return ; }
1025+ if (error) {
1026+ BNCLogError (@" Error while getting Apple Search Ad attribution: %@ ." , error);
1027+ }
10231028
1024- self.preferenceHelper .shouldWaitForInit = NO ;
1029+ self.asyncRequestCount --;
1030+ // If there's another async attribution check in flight, don't continue with init:
1031+ if (self.asyncRequestCount > 0 ) return ;
10251032
1026- dispatch_async (dispatch_get_main_queue (), ^{
1027- [self initUserSessionAndCallCallback: !self .isInitialized];
1028- });
1033+ self.preferenceHelper .shouldWaitForInit = NO ;
1034+ dispatch_async (dispatch_get_main_queue (), ^{
1035+ [self initUserSessionAndCallCallback: !self .isInitialized];
1036+ });
1037+ }
10291038 };
10301039
10311040 // Set a expiration timer in case we don't get a call back (I'm looking at you, iPad):
0 commit comments