Skip to content

Commit

Permalink
feat: Add Tracking Support for Custom Domains
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Stalnaker committed Apr 29, 2024
1 parent 5ea7f68 commit 447c621
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
65 changes: 65 additions & 0 deletions UnitTests/MPNetworkCommunicationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,46 @@ - (void)testModifyURLWithOptions {
XCTAssert([modifyURL.accessibilityHint isEqualToString:@"identity"]);
}

- (void)testModifyURLWithOptionsAndTrackingOverride {
[self swizzleInstanceMethodForInstancesOfClass:[NSBundle class] selector:@selector(infoDictionary)];
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
stateMachine.attAuthorizationStatus = @(MPATTAuthorizationStatusAuthorized);

MPNetworkOptions *options = [[MPNetworkOptions alloc] init];
options.identityHost = @"identity.mpproxy.example.com";
options.identityTrackingHost = @"identity-tracking.mpproxy.example.com";
[MParticle sharedInstance].networkOptions = options;

MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init];
NSURL *modifyURL = [networkCommunication modifyURL].url;

[self deswizzle];

XCTAssert([modifyURL.absoluteString rangeOfString:@"https://identity-tracking.mpproxy.example.com/v1/0/modify"].location != NSNotFound);
XCTAssert([modifyURL.accessibilityHint isEqualToString:@"identity"]);
}

- (void)testEventURLWithOptionsAndOverrideAndEventsOnlyAndTrackingHost {
[self swizzleInstanceMethodForInstancesOfClass:[NSBundle class] selector:@selector(infoDictionary)];
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
stateMachine.attAuthorizationStatus = @(MPATTAuthorizationStatusAuthorized);

MPNetworkOptions *options = [[MPNetworkOptions alloc] init];
options.eventsHost = @"events.mpproxy.example.com";
options.eventsTrackingHost = @"events-tracking.mpproxy.example.com";
options.eventsOnly = true;
[MParticle sharedInstance].networkOptions = options;

MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init];
NSURL *eventURL = [networkCommunication eventURL].url;

[self deswizzle];

XCTAssert([eventURL.absoluteString rangeOfString:@"https://events-tracking.mpproxy.example.com/"].location != NSNotFound);
XCTAssert([eventURL.absoluteString rangeOfString:@"v1"].location == NSNotFound);
XCTAssert([eventURL.absoluteString rangeOfString:@"identity"].location == NSNotFound);
}


- (void)testAliasURL {
[self swizzleInstanceMethodForInstancesOfClass:[NSBundle class] selector:@selector(infoDictionary)];
Expand Down Expand Up @@ -226,6 +266,31 @@ - (void)testAliasURLWithOptionsAndOverrideAndEventsOnly {
XCTAssert([aliasURL.accessibilityHint isEqualToString:@"identity"]);
}

- (void)testAliasURLWithOptionsAndOverrideAndEventsOnlyAndTrackingHost {
[self swizzleInstanceMethodForInstancesOfClass:[NSBundle class] selector:@selector(infoDictionary)];
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
stateMachine.attAuthorizationStatus = @(MPATTAuthorizationStatusAuthorized);

MPNetworkOptions *options = [[MPNetworkOptions alloc] init];
options.eventsHost = @"events.mpproxy.example.com";
options.eventsTrackingHost = @"events-tracking.mpproxy.example.com";
options.aliasHost = @"alias.mpproxy.example.com";
options.aliasTrackingHost = @"alias-tracking.mpproxy.example.com";
options.overridesAliasSubdirectory = true;
options.eventsOnly = true;
[MParticle sharedInstance].networkOptions = options;

MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init];
NSURL *aliasURL = [networkCommunication aliasURL].url;

[self deswizzle];

XCTAssert([aliasURL.absoluteString rangeOfString:@"https://alias-tracking.mpproxy.example.com/"].location != NSNotFound);
XCTAssert([aliasURL.absoluteString rangeOfString:@"v1"].location == NSNotFound);
XCTAssert([aliasURL.absoluteString rangeOfString:@"identity"].location == NSNotFound);
XCTAssert([aliasURL.accessibilityHint isEqualToString:@"identity"]);
}

- (void)testEmptyUploadsArray {
MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init];
NSArray *uploads = @[];
Expand Down
12 changes: 12 additions & 0 deletions mParticle-Apple-SDK/Include/mParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Allows you to override the default event host.
*/
@property (nonatomic) NSString *eventsHost;
/**
Allows you to override the tracking event host. Set this to automatically use an alternate custom domain when ATTStatus has been authorized.
*/
@property (nonatomic) NSString *eventsTrackingHost;
/**
Defaults to false. If set true the eventsHost above with overwrite the subdirectory of the URL in addition to the host.
*/
@property (nonatomic) BOOL overridesEventsSubdirectory;
Expand All @@ -109,6 +113,10 @@ Allows you to override the default identity host.
*/
@property (nonatomic) NSString *identityHost;
/**
Allows you to override the tracking identity host. Set this to automatically use an alternate custom domain when ATTStatus has been authorized.
*/
@property (nonatomic) NSString *identityTrackingHost;
/**
Defaults to false. If set true the identityHost above with overwrite the subdirectory of the URL in addition to the host.
*/
@property (nonatomic) BOOL overridesIdentitySubdirectory;
Expand All @@ -118,6 +126,10 @@ Allows you to override the default alias host.
*/
@property (nonatomic) NSString *aliasHost;
/**
Allows you to override the tracking alias host. Set this to automatically use an alternate custom domain when ATTStatus has been authorized.
*/
@property (nonatomic) NSString *aliasTrackingHost;
/**
Defaults to false. If set true the aliasHost above with overwrite the subdirectory of the URL in addition to the host.
*/
@property (nonatomic) BOOL overridesAliasSubdirectory;
Expand Down
30 changes: 26 additions & 4 deletions mParticle-Apple-SDK/Network/MPNetworkCommunication.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ - (MPURL *)eventURL {
}

MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
NSString *eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost;
NSString *eventHost;
if ([MParticle sharedInstance].networkOptions.eventsTrackingHost && stateMachine.attAuthorizationStatus.integerValue == MPATTAuthorizationStatusAuthorized) {
eventHost = [MParticle sharedInstance].networkOptions.eventsTrackingHost;
} else {
eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost;
}
NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, self.defaultEventHost, kMPEventsVersion, stateMachine.apiKey, kMPEventsURL];
NSURL *defaultURL = [NSURL URLWithString:urlString];

Expand Down Expand Up @@ -251,7 +256,13 @@ - (MPURL *)logoutURL {
}

- (MPURL *)identityURL:(NSString *)pathComponent {
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
NSString *identityHost;
if ([MParticle sharedInstance].networkOptions.identityTrackingHost && stateMachine.attAuthorizationStatus.integerValue == MPATTAuthorizationStatusAuthorized) {
identityHost = [MParticle sharedInstance].networkOptions.identityTrackingHost;
} else {
identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
}
NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

Expand All @@ -275,7 +286,13 @@ - (MPURL *)identityURL:(NSString *)pathComponent {

- (MPURL *)modifyURL {
NSString *pathComponent = @"modify";
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
NSString *identityHost;
if ([MParticle sharedInstance].networkOptions.identityTrackingHost && stateMachine.attAuthorizationStatus.integerValue == MPATTAuthorizationStatusAuthorized) {
identityHost = [MParticle sharedInstance].networkOptions.identityTrackingHost;
} else {
identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
}
NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, [MPPersistenceController mpId], pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

Expand Down Expand Up @@ -305,7 +322,12 @@ - (MPURL *)aliasURL {
NSString *pathComponent = @"alias";
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;

NSString *eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: self.defaultEventHost;
NSString *eventHost;
if ([MParticle sharedInstance].networkOptions.aliasTrackingHost && stateMachine.attAuthorizationStatus.integerValue == MPATTAuthorizationStatusAuthorized) {
eventHost = [MParticle sharedInstance].networkOptions.aliasTrackingHost;
} else {
eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: self.defaultEventHost;
}
NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, self.defaultEventHost, kMPIdentityVersion, kMPIdentityKey, stateMachine.apiKey, pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

Expand Down
3 changes: 3 additions & 0 deletions mParticle-Apple-SDK/mParticle.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ - (NSString *)description {
[description appendFormat:@" configHost: %@\n", _configHost];
[description appendFormat:@" overridesConfigSubdirectory: %s\n", _overridesConfigSubdirectory ? "true" : "false"];
[description appendFormat:@" eventsHost: %@\n", _eventsHost];
[description appendFormat:@" eventsTrackingHost: %@\n", _eventsTrackingHost];
[description appendFormat:@" overridesEventSubdirectory: %s\n", _overridesEventsSubdirectory ? "true" : "false"];
[description appendFormat:@" identityHost: %@\n", _identityHost];
[description appendFormat:@" identityTrackingHost: %@\n", _identityTrackingHost];
[description appendFormat:@" overridesIdentitySubdirectory: %s\n", _overridesIdentitySubdirectory ? "true" : "false"];
[description appendFormat:@" aliasHost: %@\n", _aliasHost];
[description appendFormat:@" aliasTrackingHost: %@\n", _aliasTrackingHost];
[description appendFormat:@" overridesAliasSubdirectory: %s\n", _overridesAliasSubdirectory ? "true" : "false"];
[description appendFormat:@" certificates: %@\n", _certificates];
[description appendFormat:@" pinningDisabledInDevelopment: %s\n", _pinningDisabledInDevelopment ? "true" : "false"];
Expand Down

0 comments on commit 447c621

Please sign in to comment.