Skip to content

Commit cb72835

Browse files
authored
Merge pull request #367 from BranchMetrics/answers
Answers
2 parents eec6a00 + da15393 commit cb72835

File tree

24 files changed

+464
-274
lines changed

24 files changed

+464
-274
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// BNCFabricAnswers.h
3+
// Branch-TestBed
4+
//
5+
// Created by Ahmed Nawar on 6/2/16.
6+
// Copyright © 2016 Branch Metrics. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface BNCFabricAnswers : NSObject
12+
13+
+ (void)sendEventWithName:(NSString*)name andAttributes:(NSDictionary*)attributes;
14+
+ (NSDictionary *)prepareBranchDataForAnswers:(NSDictionary *)dictionary;
15+
16+
@end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// BNCFabricAnswers.m
3+
// Branch-TestBed
4+
//
5+
// Created by Ahmed Nawar on 6/2/16.
6+
// Copyright © 2016 Branch Metrics. All rights reserved.
7+
//
8+
9+
#import "BNCFabricAnswers.h"
10+
#import "BNCPreferenceHelper.h"
11+
#import "../Fabric/Answers.h"
12+
13+
@implementation BNCFabricAnswers
14+
15+
+ (void)sendEventWithName:(NSString *)name andAttributes:(NSDictionary *)attributes {
16+
ANSLogCustomEvent(name, [self prepareBranchDataForAnswers:attributes]);
17+
}
18+
19+
+ (NSDictionary *)prepareBranchDataForAnswers:(NSDictionary *)dictionary {
20+
NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
21+
22+
for (NSString *key in dictionary.allKeys) {
23+
if ([key hasPrefix:@"+"] || ([key hasPrefix:@"$"] && ![key isEqualToString:@"$identity_id"])) {
24+
// ignore because this data is not found on the ShareSheet
25+
continue;
26+
} else if ([dictionary[key] isKindOfClass:[NSArray class]]) {
27+
// flatten arrays, special treatement for ~tags
28+
NSString *aKey;
29+
if ([key hasPrefix:@"~"])
30+
aKey = [key substringFromIndex:1];
31+
else
32+
aKey = key;
33+
NSArray *valuesArray = dictionary[key];
34+
for (NSUInteger i = 0; i < valuesArray.count; ++i) {
35+
temp[[NSString stringWithFormat:@"%@.%lu", aKey, (unsigned long)i]] = valuesArray[i];
36+
}
37+
} else if ([key hasPrefix:@"~"]) {
38+
// strip tildes ~
39+
temp[[key substringFromIndex:1]] = dictionary[key];
40+
} else if ([key isEqualToString:@"$identity_id"]) {
41+
temp[@"referring_branch_identity"] = dictionary[key];
42+
}
43+
}
44+
45+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];
46+
temp[@"branch_identity"] = preferenceHelper.identityID;
47+
48+
return temp;
49+
}
50+
51+
@end

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "BNCEncodingUtils.h"
1111
#import "BNCConfig.h"
1212
#import "Branch.h"
13+
#import "../Fabric/Fabric+FABKits.h"
1314

1415
static const NSTimeInterval DEFAULT_TIMEOUT = 5.5;
1516
static const NSTimeInterval DEFAULT_RETRY_INTERVAL = 0;
@@ -45,6 +46,9 @@
4546

4647
NSString * const BRANCH_PREFS_KEY_BRANCH_VIEW_USAGE_CNT = @"bnc_branch_view_usage_cnt_";
4748

49+
// The name of this key was specified in the account-creation API integration
50+
static NSString * const BNC_BRANCH_FABRIC_APP_KEY_KEY = @"branch_key";
51+
4852
@interface BNCPreferenceHelper ()
4953

5054
@property (strong, nonatomic) NSMutableDictionary *persistenceDict;
@@ -193,6 +197,17 @@ - (NSString *)getBranchKey:(BOOL)isLive {
193197
else if ([ret isKindOfClass:[NSDictionary class]]) {
194198
self.branchKey = isLive ? ret[@"live"] : ret[@"test"];
195199
}
200+
} else {
201+
Class fabric = NSClassFromString(@"Fabric");
202+
203+
if (fabric) {
204+
NSDictionary *configDictionary = [fabric configurationDictionaryForKitClass:[Branch class]];
205+
ret = [configDictionary objectForKey:BNC_BRANCH_FABRIC_APP_KEY_KEY];
206+
207+
if ([ret isKindOfClass:[NSString class]]) {
208+
self.branchKey = ret;
209+
}
210+
}
196211
}
197212

198213
return _branchKey;

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#import "../Fabric/FABKitProtocol.h"
4343
#import "../Fabric/Fabric+FABKits.h"
4444

45-
4645
NSString * const BRANCH_FEATURE_TAG_SHARE = @"share";
4746
NSString * const BRANCH_FEATURE_TAG_REFERRAL = @"referral";
4847
NSString * const BRANCH_FEATURE_TAG_INVITE = @"invite";
@@ -326,7 +325,6 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
326325

327326
- (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)isReferrable explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable automaticallyDisplayController:(BOOL)automaticallyDisplayController {
328327
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;
329-
330328
self.preferenceHelper.isReferrable = isReferrable;
331329
self.preferenceHelper.explicitlyRequestedReferrable = explicitlyRequestedReferrable;
332330

Branch-SDK/Branch-SDK/BranchActivityItemProvider.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ - (id)item {
7373
if ([channel isEqualToString:@"facebook"] || [channel isEqualToString:@"twitter"] || [channel isEqualToString:@"com.tinyspeck.chatlyio.share"]) {
7474
return [NSURL URLWithString:[[Branch getInstance] getShortURLWithParams:params andTags:tags andChannel:channel andFeature:feature andStage:stage andAlias:alias ignoreUAString:self.userAgentString forceLinkCreation:YES]];
7575
}
76-
7776
return [NSURL URLWithString:[[Branch getInstance] getShortURLWithParams:params andTags:tags andChannel:channel andFeature:feature andStage:stage andAlias:alias ignoreUAString:nil forceLinkCreation:YES]];
7877
}
7978

Branch-SDK/Branch-SDK/BranchUniversalObject.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "BNCError.h"
1111
#import "BranchConstants.h"
1212
#import "BNCPreferenceHelper.h"
13+
#import "BNCFabricAnswers.h"
1314

1415
@implementation BranchUniversalObject {
1516
BNCPreferenceHelper *_preferenceHelper;
@@ -164,6 +165,7 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
164165
shareViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
165166
if (completion) {
166167
completion(activityType, completed);
168+
[BNCFabricAnswers sendEventWithName:@"Branch Share" andAttributes:[self getDictionaryWithCompleteLinkProperties:linkProperties]];
167169
}
168170
};
169171
} else {
@@ -354,6 +356,18 @@ - (NSDictionary *)getParamsForServerRequestWithAddedLinkProperties:(BranchLinkPr
354356
return [temp copy];
355357
}
356358

359+
- (NSDictionary *)getDictionaryWithCompleteLinkProperties:(BranchLinkProperties *)linkProperties {
360+
NSMutableDictionary *temp = [[self getParamsForServerRequestWithAddedLinkProperties:linkProperties] mutableCopy];
361+
362+
[self safeSetValue:linkProperties.tags forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_TAGS] onDict:temp];
363+
[self safeSetValue:linkProperties.feature forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_FEATURE] onDict:temp];
364+
[self safeSetValue:linkProperties.alias forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_ALIAS] onDict:temp];
365+
[self safeSetValue:linkProperties.channel forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_CHANNEL] onDict:temp];
366+
[self safeSetValue:linkProperties.stage forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_STAGE] onDict:temp];
367+
[self safeSetValue:@(linkProperties.matchDuration) forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_DURATION] onDict:temp];
368+
369+
return [temp copy];
370+
}
357371
- (void)safeSetValue:(NSObject *)value forKey:(NSString *)key onDict:(NSMutableDictionary *)dict {
358372
if (value) {
359373
dict[key] = value;

Branch-SDK/Branch-SDK/Requests/BranchOpenRequest.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "BranchConstants.h"
1313
#import "BNCEncodingUtils.h"
1414
#import "BranchViewHandler.h"
15+
#import "BNCFabricAnswers.h"
1516

1617
@interface BranchOpenRequest ()
1718

@@ -80,32 +81,35 @@ - (void)processResponse:(BNCServerResponse *)response error:(NSError *)error {
8081
preferenceHelper.userUrl = data[BRANCH_RESPONSE_KEY_USER_URL];
8182
preferenceHelper.userIdentity = userIdentity;
8283
preferenceHelper.sessionID = data[BRANCH_RESPONSE_KEY_SESSION_ID];
83-
8484
[BNCSystemObserver setUpdateState];
8585

8686
NSString *sessionData = data[BRANCH_RESPONSE_KEY_SESSION_DATA];
8787

8888
// Update session params
8989
preferenceHelper.sessionParams = sessionData;
90-
90+
9191
// Scenarios:
9292
// If no data, data isn't from a link click, or isReferrable is false, don't set, period.
9393
// Otherwise,
9494
// * On Install: set.
9595
// * On Open and installParams set: don't set.
9696
// * On Open and stored installParams are empty: set.
97-
if (sessionData.length && preferenceHelper.isReferrable) {
97+
if (sessionData.length) {
9898
NSDictionary *sessionDataDict = [BNCEncodingUtils decodeJsonStringToDictionary:sessionData];
9999
BOOL dataIsFromALinkClick = [sessionDataDict[BRANCH_RESPONSE_KEY_CLICKED_BRANCH_LINK] isEqual:@1];
100-
101100
BOOL storedParamsAreEmpty = YES;
101+
102102
if ([preferenceHelper.installParams isKindOfClass:[NSString class]]) {
103103
storedParamsAreEmpty = !preferenceHelper.installParams.length;
104104
}
105105

106106
if (dataIsFromALinkClick && (self.isInstall || storedParamsAreEmpty)) {
107107
preferenceHelper.installParams = sessionData;
108108
}
109+
110+
if (dataIsFromALinkClick) {
111+
[BNCFabricAnswers sendEventWithName:[@"Branch " stringByAppendingString:[[self getActionName] capitalizedString]] andAttributes:sessionDataDict];
112+
}
109113
}
110114

111115
// Clear link identifiers so they don't get reused on the next open
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// ANSCompatibility.h
3+
// AnswersKit
4+
//
5+
// Copyright (c) 2015 Crashlytics, Inc. All rights reserved.
6+
//
7+
8+
#pragma once
9+
10+
#if !__has_feature(nullability)
11+
#define nonnull
12+
#define nullable
13+
#define _Nullable
14+
#define _Nonnull
15+
#endif
16+
17+
#ifndef NS_ASSUME_NONNULL_BEGIN
18+
#define NS_ASSUME_NONNULL_BEGIN
19+
#endif
20+
21+
#ifndef NS_ASSUME_NONNULL_END
22+
#define NS_ASSUME_NONNULL_END
23+
#endif
24+
25+
#if __has_feature(objc_generics)
26+
#define ANS_GENERIC_NSARRAY(type) NSArray<type>
27+
#define ANS_GENERIC_NSDICTIONARY(key_type,object_key) NSDictionary<key_type, object_key>
28+
#else
29+
#define ANS_GENERIC_NSARRAY(type) NSArray
30+
#define ANS_GENERIC_NSDICTIONARY(key_type,object_key) NSDictionary
31+
#endif

0 commit comments

Comments
 (0)