Skip to content

Commit

Permalink
Added ScCid support and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nsingh-branch committed Oct 4, 2023
1 parent da55b9e commit 8e074f0
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
112 changes: 112 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BNCReferringURLUtilityTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,116 @@ - (void)verifyOldGbraidDataIsCleared {
XCTAssertNil([BNCPreferenceHelper sharedInstance].referrerGBRAIDInitDate);
}

- (void)testReferringURLWithSccid {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=12345"];
NSDictionary *expected = @{
@"sccid": @"12345"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidMixedCase {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?ScCiD=12345"];
NSDictionary *expected = @{
@"sccid": @"12345"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidNoValue {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid="];
NSDictionary *expected = @{
@"sccid": @""
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidValueCasePreserved {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=aAbBcC"];
NSDictionary *expected = @{
@"sccid": @"aAbBcC"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidIgnoredParam {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=12345&other=abcde"];
NSDictionary *expected = @{
@"sccid": @"12345"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidFragment{
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=12345#header"];
NSDictionary *expected = @{
@"sccid": @"12345"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidAsFragment{
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?other=abcde#sccid=12345"];
NSDictionary *expected = @{ };

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected isEqualToDictionary:params]);
}

- (void)testReferringURLWithSccidOverwritesValue {
NSURL *url = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=12345"];
NSDictionary *expected = @{
@"sccid": @"12345"
};

NSURL *url2 = [NSURL URLWithString:@"https://bnctestbed.app.link?sccid=abcde"];
NSDictionary *expected2 = @{
@"sccid": @"abcde"
};

BNCReferringURLUtility *utility = [self referringUtilityForTests];
[utility parseReferringURL:url];
NSDictionary *params = [utility referringURLQueryParamsForEndpoint:openEndpoint];
XCTAssert([expected isEqualToDictionary:params]);

[utility parseReferringURL:url2];
NSDictionary *params2 = [utility referringURLQueryParamsForEndpoint:openEndpoint];

XCTAssert([expected2 isEqualToDictionary:params2]);
}


@end
32 changes: 31 additions & 1 deletion BranchSDK/BNCReferringURLUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "BranchConstants.h"
#import "BNCUrlQueryParameter.h"
#import "BNCLog.h"
#import <UIKit/UIKit.h>

@interface BNCReferringURLUtility()
@property (strong, readwrite, nonatomic) NSMutableDictionary<NSString *, BNCUrlQueryParameter *> *urlQueryParameters;
Expand All @@ -25,10 +26,25 @@ - (instancetype)init {
self.preferenceHelper = [BNCPreferenceHelper sharedInstance];
self.urlQueryParameters = [self deserializeFromJson:self.preferenceHelper.referringURLQueryParameters];
[self checkForAndMigrateOldGbraid];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(clearSccid)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(clearSccid)
name:UIApplicationWillTerminateNotification
object:nil];
}
return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)parseReferringURL:(NSURL *)url {
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
for (NSURLQueryItem *item in components.queryItems) {
Expand Down Expand Up @@ -107,6 +123,7 @@ - (NSDictionary *)referringURLQueryParamsForEndpoint:(NSString *)endpoint {
params[BRANCH_REQUEST_KEY_META_CAMPAIGN_IDS] = [self metaCampaignIDsForEndpoint:endpoint];
params[BRANCH_REQUEST_KEY_GCLID] = [self gclidValueForEndpoint:endpoint];
[params addEntriesFromDictionary:[self gbraidValuesForEndpoint:endpoint]];
params[BRANCH_REQUEST_KEY_SCCID] = [self sccidValueForEndpoint:endpoint];

return params;
}
Expand Down Expand Up @@ -153,8 +170,15 @@ - (NSDictionary *)gbraidValuesForEndpoint:(NSString *)endpoint {
return returnedParams;
}

- (NSString *)sccidValueForEndpoint:(NSString *)endpoint {
if (([endpoint containsString:@"/v2/event"]) || ([endpoint containsString:@"/v1/open"]) || ([endpoint containsString:@"/v1/install"]) ) {
return self.urlQueryParameters[BRANCH_REQUEST_KEY_SCCID].value;
}
return nil;
}

- (BOOL)isSupportedQueryParameter:(NSString *)param {
NSArray *validURLQueryParameters = @[BRANCH_REQUEST_KEY_REFERRER_GBRAID, BRANCH_REQUEST_KEY_GCLID];
NSArray *validURLQueryParameters = @[BRANCH_REQUEST_KEY_REFERRER_GBRAID, BRANCH_REQUEST_KEY_GCLID, BRANCH_REQUEST_KEY_SCCID];
return [self isSupportedQueryParameter:param validParams:validURLQueryParameters];
}

Expand Down Expand Up @@ -266,4 +290,10 @@ - (void)checkForAndMigrateOldGbraid {
}
}

- (void)clearSccid {
[self.urlQueryParameters removeObjectForKey:BRANCH_REQUEST_KEY_SCCID];
self.preferenceHelper.referringURLQueryParameters = [self serializeToJson:self.urlQueryParameters];
}


@end
1 change: 1 addition & 0 deletions BranchSDK/BranchConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ extern NSString * const BRANCH_URL_QUERY_PARAMETERS_VALUE_KEY;
extern NSString * const BRANCH_URL_QUERY_PARAMETERS_TIMESTAMP_KEY;
extern NSString * const BRANCH_URL_QUERY_PARAMETERS_IS_DEEPLINK_KEY;
extern NSString * const BRANCH_URL_QUERY_PARAMETERS_VALIDITY_WINDOW_KEY;
extern NSString * const BRANCH_REQUEST_KEY_SCCID;

extern NSString * const BRANCH_REQUEST_ENDPOINT_SET_IDENTITY;
extern NSString * const BRANCH_REQUEST_ENDPOINT_APP_LINK_SETTINGS;
Expand Down
1 change: 1 addition & 0 deletions BranchSDK/BranchConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
NSString * const BRANCH_URL_QUERY_PARAMETERS_TIMESTAMP_KEY = @"timestamp";
NSString * const BRANCH_URL_QUERY_PARAMETERS_IS_DEEPLINK_KEY = @"isDeepLink";
NSString * const BRANCH_URL_QUERY_PARAMETERS_VALIDITY_WINDOW_KEY = @"validityWindow";
NSString * const BRANCH_REQUEST_KEY_SCCID = @"sccid";

NSString * const BRANCH_REQUEST_ENDPOINT_SET_IDENTITY = @"profile";
NSString * const BRANCH_REQUEST_ENDPOINT_APP_LINK_SETTINGS = @"app-link-settings";
Expand Down

0 comments on commit 8e074f0

Please sign in to comment.