Skip to content

Commit

Permalink
feat: Update Forwarding Logic (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStalnaker authored Sep 6, 2023
1 parent 5889c33 commit 6637af6
Showing 1 changed file with 68 additions and 85 deletions.
153 changes: 68 additions & 85 deletions mParticle-Kochava/MPKitKochava.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

NSString *const kvAppId = @"appId";
NSString *const kvCurrency = @"currency";
NSString *const kvUseCustomerId = @"useCustomerId";
NSString *const kvIncludeOtherUserIds = @"passAllOtherUserIdentities";
NSString *const kvUserIdentificationType = @"userIdentificationType";
NSString *const kvEmailIdentificationType = @"emailIdentificationType";
NSString *const kvRetrieveAttributionData = @"retrieveAttributionData";
NSString *const kvEnableLogging = @"enableLogging";
NSString *const kvLimitAdTracking = @"limitAdTracking";
Expand Down Expand Up @@ -130,27 +130,30 @@ + (void)addCustomIdentityLinks:(NSDictionary *)identityLink {
}

#pragma mark Accessors and private methods
- (void)identityLinkCustomerId {
FilteredMParticleUser *user = [self currentUser];
if (!user || user.userIdentities.count == 0) {
return;
}

NSMutableDictionary *identityInfo = [[NSMutableDictionary alloc] initWithCapacity:user.userIdentities.count];
NSString *identityKey;

NSString *identityValue = user.userIdentities[@(MPUserIdentityCustomerId)];
if (identityValue) {
identityKey = @"CustomerId";
identityInfo[identityKey] = identityValue;
}

for (NSString *key in identityInfo.allKeys) {
[KVAIdentityLink registerWithName:key identifier:identityInfo[key]];
}
- (NSError *)errorWithMessage:(NSString *)message {
NSError *error = [NSError errorWithDomain:MPKitKochavaErrorDomain code:0 userInfo:@{MPKitKochavaErrorKey:message}];
return error;
}

- (void)retrieveAttributionWithCompletionHandler:(void(^)(NSDictionary *attribution))completionHandler {
[KVATracker.shared.attribution retrieveResultWithCompletionHandler:^(KVAAttributionResult * _Nonnull attributionResult)
{
if (!attributionResult.rawDictionary) {
[self->_kitApi onAttributionCompleteWithResult:nil error:[self errorWithMessage:@"Received nil attributionData from Kochava"]];
} else {
MPAttributionResult *mParticleResult = [[MPAttributionResult alloc] init];
mParticleResult.linkInfo = attributionResult.rawDictionary;

[self->_kitApi onAttributionCompleteWithResult:mParticleResult error:nil];
}

if (completionHandler) {
completionHandler(attributionResult.rawDictionary);
}
}];
}

- (void)identityLinkOtherUserIds {
- (void)synchronizeIdentity {
FilteredMParticleUser *user = [self currentUser];
if (!user.userIdentities || user.userIdentities.count == 0) {
return;
Expand All @@ -163,32 +166,52 @@ - (void)identityLinkOtherUserIds {
userIdentity = [userIdentityType integerValue];

switch (userIdentity) {
case MPUserIdentityEmail:
identityKey = @"Email";
case MPUserIdentityCustomerId:
identityKey = @"CustomerId";
break;

case MPUserIdentityOther:
identityKey = @"OtherId";
identityKey = @"Other";
break;

case MPUserIdentityOther2:
identityKey = @"Other2";
break;

case MPUserIdentityOther3:
identityKey = @"Other3";
break;

case MPUserIdentityOther4:
identityKey = @"Other4";
break;

case MPUserIdentityOther5:
identityKey = @"Other5";
break;

case MPUserIdentityOther6:
identityKey = @"Other6";
break;

case MPUserIdentityFacebook:
identityKey = @"Facebook";
case MPUserIdentityOther7:
identityKey = @"Other7";
break;

case MPUserIdentityTwitter:
identityKey = @"Twitter";
case MPUserIdentityOther8:
identityKey = @"Other8";
break;

case MPUserIdentityGoogle:
identityKey = @"Google";
case MPUserIdentityOther9:
identityKey = @"Other9";
break;

case MPUserIdentityYahoo:
identityKey = @"Yahoo";
case MPUserIdentityOther10:
identityKey = @"Other10";
break;

case MPUserIdentityMicrosoft:
identityKey = @"Microsoft";
case MPUserIdentityEmail:
identityKey = @"Email";
break;

default:
Expand All @@ -198,7 +221,12 @@ - (void)identityLinkOtherUserIds {

NSString *identityValue = user.userIdentities[userIdentityType];
if (identityValue) {
identityInfo[identityKey] = identityValue;
if ([self.configuration[kvUserIdentificationType] isEqualToString:identityKey]) {
identityInfo[identityKey] = identityValue;
}
if ([self.configuration[kvEmailIdentificationType] isEqualToString:identityKey]) {
identityInfo[identityKey] = identityValue;
}
}
}

Expand All @@ -207,39 +235,6 @@ - (void)identityLinkOtherUserIds {
}
}

- (NSError *)errorWithMessage:(NSString *)message {
NSError *error = [NSError errorWithDomain:MPKitKochavaErrorDomain code:0 userInfo:@{MPKitKochavaErrorKey:message}];
return error;
}

- (void)retrieveAttributionWithCompletionHandler:(void(^)(NSDictionary *attribution))completionHandler {
[KVATracker.shared.attribution retrieveResultWithCompletionHandler:^(KVAAttributionResult * _Nonnull attributionResult)
{
if (!attributionResult.rawDictionary) {
[self->_kitApi onAttributionCompleteWithResult:nil error:[self errorWithMessage:@"Received nil attributionData from Kochava"]];
} else {
MPAttributionResult *mParticleResult = [[MPAttributionResult alloc] init];
mParticleResult.linkInfo = attributionResult.rawDictionary;

[self->_kitApi onAttributionCompleteWithResult:mParticleResult error:nil];
}

if (completionHandler) {
completionHandler(attributionResult.rawDictionary);
}
}];
}

- (void)synchronize {
if ([self.configuration[kvUseCustomerId] boolValue]) {
[self identityLinkCustomerId];
}

if ([self.configuration[kvIncludeOtherUserIds] boolValue]) {
[self identityLinkOtherUserIds];
}
}

#pragma mark MPKitInstanceProtocol methods
- (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configuration {
MPKitExecStatus *execStatus = nil;
Expand Down Expand Up @@ -277,8 +272,8 @@ - (void)start {
KVALog.shared.level = [self.configuration[kvEnableLogging] boolValue] ? KVALogLevel.debug : KVALogLevel.never;
}

if ([self.configuration[kvUseCustomerId] boolValue] || [self.configuration[kvIncludeOtherUserIds] boolValue]) {
[self synchronize];
if (self.configuration[kvUserIdentificationType] || self.configuration[kvEmailIdentificationType] ) {
[self synchronizeIdentity];
}

NSDictionary *userActivityDictionary = self.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
Expand Down Expand Up @@ -429,21 +424,9 @@ - (MPKitExecStatus *)setUserIdentity:(NSString *)identityString identityType:(MP
return execStatus;
}

if (user.userIdentities[@(identityType)] && [user.userIdentities[@(identityType)] isEqual:identityString]) {
execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceKochava) returnCode:MPKitReturnCodeRequirementsNotMet];
return execStatus;
}

if (identityType == MPUserIdentityCustomerId) {
if ([self.configuration[kvUseCustomerId] boolValue]) {
[self identityLinkCustomerId];
execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceKochava) returnCode:MPKitReturnCodeSuccess];
}
} else {
if ([self.configuration[kvIncludeOtherUserIds] boolValue]) {
[self identityLinkOtherUserIds];
execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceKochava) returnCode:MPKitReturnCodeSuccess];
}
if (self.configuration[kvUserIdentificationType] || self.configuration[kvEmailIdentificationType] ) {
[self synchronizeIdentity];
execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceKochava) returnCode:MPKitReturnCodeSuccess];
}

if (!execStatus) {
Expand Down

0 comments on commit 6637af6

Please sign in to comment.