Skip to content

Commit 21e1131

Browse files
authored
Send Apple Search Ad Timing DEVEX-594 (#845)
* Turn logging on in example. * Send instrumentation times for Apple Search Ads.
1 parent 32a0c18 commit 21e1131

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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.3fs.", - [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.3fs.", 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):

Examples/UITestBed/UITestBed.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
4DC00B3820A0A1C2002E68CD /* UITestEvents.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DC00B3720A0A1C2002E68CD /* UITestEvents.m */; };
7171
4DC00B4020A0A214002E68CD /* UITestSafari.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DC00B3F20A0A214002E68CD /* UITestSafari.m */; };
7272
4DC00B4320A0C5F4002E68CD /* TBSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DC00B4220A0C5F4002E68CD /* TBSettings.m */; };
73+
4DED488D209BDEB9008DE877 /* OCMock.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4DED488B209BDEB9008DE877 /* OCMock.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
74+
4DED488E209BDEB9008DE877 /* OHHTTPStubs.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4DED488C209BDEB9008DE877 /* OHHTTPStubs.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
7375
4DFF2B6E1EEF3B150043F840 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFF2B6D1EEF3B150043F840 /* main.m */; };
7476
4DFF2B711EEF3B150043F840 /* TBAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFF2B701EEF3B150043F840 /* TBAppDelegate.m */; };
7577
4DFF2B741EEF3B150043F840 /* TBBranchViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFF2B731EEF3B150043F840 /* TBBranchViewController.m */; };

Examples/UITestBed/UITestBed/TBAppDelegate.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ - (BOOL)application:(UIApplication *)application
5050
//[branch setDebug];
5151

5252
// Optionally check for Apple Search Ads attribution:
53-
// [branch delayInitToCheckForSearchAds];
53+
[branch delayInitToCheckForSearchAds];
5454

5555
// Turn this on to debug Apple Search Ads. Should not be included for production.
5656
// [branch setAppleSearchAdsDebugMode];
5757

5858
// For testing app updates:
5959
next_previous_update_time = [BNCPreferenceHelper preferenceHelper].previousAppBuildDate;
60-
60+
61+
BNCLogSetDisplayLevel(BNCLogLevelAll);
6162
[branch setWhiteListedSchemes:@[@"branchuitest"]];
6263
[branch initSessionWithLaunchOptions:launchOptions
6364
andRegisterDeepLinkHandler:^(NSDictionary * _Nullable params, NSError * _Nullable error) {

0 commit comments

Comments
 (0)