Skip to content

Commit a59b05c

Browse files
feat: Map Selected Identity to Emailsha256
1 parent eaee66e commit a59b05c

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

UnitTests/MPRoktTests.m

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
@interface MPRokt ()
1212
- (NSArray<NSDictionary<NSString *, NSString *> *> *)getRoktPlacementAttributesMapping;
13+
- (NSNumber *)getRoktHashedEmailUserIdentityType;
1314
- (void)confirmUser:(NSDictionary<NSString *, NSString *> *)attributes user:(MParticleUser * _Nullable)user completion:(void (^)(MParticleUser *_Nullable))completion;
1415
@end
1516

@@ -398,16 +399,56 @@ - (void)testTriggeredIdentifyWithMismatchedEmailIdentity {
398399
}
399400

400401
- (void)testTriggeredIdentifyWithMismatchedOtherIdentity {
402+
[self hashedIdentityTest: MPIdentityOther];
403+
}
404+
405+
- (void)testTriggeredIdentifyWithMismatchedOther2Identity {
406+
[self hashedIdentityTest: MPIdentityOther2];
407+
}
408+
409+
- (void)testTriggeredIdentifyWithMismatchedOther3Identity {
410+
[self hashedIdentityTest: MPIdentityOther3];
411+
}
412+
413+
- (void)testTriggeredIdentifyWithMismatchedOther4Identity {
414+
[self hashedIdentityTest: MPIdentityOther4];
415+
}
416+
417+
- (void)testTriggeredIdentifyWithMismatchedOther5Identity {
418+
[self hashedIdentityTest: MPIdentityOther5];
419+
}
420+
421+
- (void)testTriggeredIdentifyWithMismatchedOther6Identity {
422+
[self hashedIdentityTest: MPIdentityOther6];
423+
}
424+
425+
- (void)testTriggeredIdentifyWithMismatchedOther7Identity {
426+
[self hashedIdentityTest: MPIdentityOther7];
427+
}
428+
- (void)testTriggeredIdentifyWithMismatchedOther8Identity {
429+
[self hashedIdentityTest: MPIdentityOther8];
430+
}
431+
432+
- (void)testTriggeredIdentifyWithMismatchedOther9Identity {
433+
[self hashedIdentityTest: MPIdentityOther9];
434+
}
435+
436+
- (void)testTriggeredIdentifyWithMismatchedOther10Identity {
437+
[self hashedIdentityTest: MPIdentityOther10];
438+
}
439+
440+
- (void)hashedIdentityTest: (MPIdentity)mpIdentity {
401441
MParticleUser *currentUser = [MParticle sharedInstance].identity.currentUser;
402442

403443
MPUserDefaults *userDefaults = [MPUserDefaults standardUserDefaultsWithStateMachine:[MParticle sharedInstance].stateMachine backendController:[MParticle sharedInstance].backendController identity:[MParticle sharedInstance].identity];
404444

405-
NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:MPUserIdentityOther], @"i" : @"[email protected]"}];
445+
NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:mpIdentity], @"i" : @"[email protected]"}];
406446

407447
[userDefaults setMPObject:userIdentityArray forKey:kMPUserIdentityArrayKey userId:currentUser.userId];
408-
XCTAssertEqualObjects(currentUser.identities[@(MPIdentityOther)], @"[email protected]");
448+
XCTAssertEqualObjects(currentUser.identities[@(mpIdentity)], @"[email protected]");
409449

410450
//Mock Identity as needed
451+
[[[self.mockRokt stub] andReturn:@(mpIdentity)] getRoktHashedEmailUserIdentityType];
411452
MParticle *instance = [MParticle sharedInstance];
412453
self.mockInstance = OCMPartialMock(instance);
413454
self.identityMock = OCMClassMock([MPIdentityApi class]);
@@ -416,7 +457,7 @@ - (void)testTriggeredIdentifyWithMismatchedOtherIdentity {
416457
[[[self.identityMock stub] andReturn:currentUser] currentUser];
417458

418459
[[self.identityMock expect] identify:[OCMArg checkWithBlock:^BOOL(MPIdentityApiRequest *request) {
419-
XCTAssertEqualObjects([request.identities objectForKey:@(MPIdentityOther)], @"[email protected]");
460+
XCTAssertEqualObjects([request.identities objectForKey:@(mpIdentity)], @"[email protected]");
420461
return true;
421462
}] completion:OCMOCK_ANY];
422463

mParticle-Apple-SDK/MPIConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,5 @@ extern NSString * _Nonnull const kMPDeviceInvalidVendorId;
519519

520520
// MPRokt Constants
521521
extern NSString * _Nonnull const kMPPlacementAttributesMapping;
522+
extern NSString * _Nonnull const kMPHashedEmailUserIdentityType;
522523
#endif

mParticle-Apple-SDK/MPIConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418

419419
// MPRokt Constants
420420
NSString * const kMPPlacementAttributesMapping = @"placementAttributesMapping";
421+
NSString * const kMPHashedEmailUserIdentityType = @"hashedEmailUserIdentityType";
421422

422423
//
423424
// Primitive data type constants

mParticle-Apple-SDK/MPRokt.m

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "MPForwardQueueParameters.h"
1111
#import "MPILogger.h"
1212
#import "MPIConstants.h"
13+
#import "MPIdentityDTO.h"
1314

1415
@interface MParticle ()
1516

@@ -178,6 +179,23 @@ - (void)close {
178179
return attributeMap;
179180
}
180181

182+
- (NSNumber *)getRoktHashedEmailUserIdentityType {
183+
// Get the kit configuration
184+
NSArray<NSDictionary *> *kitConfigs = [MParticle sharedInstance].kitContainer_PRIVATE.originalConfig.copy;
185+
NSDictionary *roktKitConfig;
186+
for (NSDictionary *kitConfig in kitConfigs) {
187+
if (kitConfig[@"id"] != nil && [kitConfig[@"id"] integerValue] == 181) {
188+
roktKitConfig = kitConfig;
189+
}
190+
}
191+
192+
// Get the string representing which identity to use and convert it to the key (NSNumber)
193+
NSString *hashedIdentityTypeString = roktKitConfig[kMPHashedEmailUserIdentityType];
194+
NSNumber *hashedIdentityTypeNumber = [MPIdentityHTTPIdentities identityTypeForString:hashedIdentityTypeString.lowercaseString];
195+
196+
return hashedIdentityTypeNumber ? hashedIdentityTypeNumber : @(MPIdentityOther);
197+
}
198+
181199
- (NSDictionary<NSString *, NSString *> *)confirmSandboxAttribute:(NSDictionary<NSString *, NSString *> * _Nullable)attributes {
182200
NSMutableDictionary<NSString *, NSString *> *finalAttributes = attributes.mutableCopy;
183201
NSString *sandboxKey = @"sandbox";
@@ -200,18 +218,19 @@ - (void)close {
200218
- (void)confirmUser:(NSDictionary<NSString *, NSString *> * _Nullable)attributes user:(MParticleUser * _Nullable)user completion:(void (^)(MParticleUser *_Nullable))completion {
201219
NSString *email = attributes[@"email"];
202220
NSString *hashedEmail = attributes[@"emailsha256"];
221+
NSNumber *hashedEmailIdentity = [self getRoktHashedEmailUserIdentityType];
203222

204-
if ((email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]) || (hashedEmail && ![hashedEmail isEqualToString: user.identities[@(MPIdentityOther)]])) {
223+
if ((email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]) || (hashedEmail && ![hashedEmail isEqualToString: user.identities[hashedEmailIdentity]])) {
205224
// If there is an existing email or hashed email but it doesn't match the what was passed in, warn the customer
206225
if (email && user.identities[@(MPIdentityEmail)]) {
207226
NSLog(@"The existing email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[@(MPIdentityEmail)], email);
208-
} else if (hashedEmail && user.identities[@(MPIdentityOther)]) {
209-
NSLog(@"The existing hashed email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[@(MPIdentityOther)], hashedEmail);
227+
} else if (hashedEmail && user.identities[hashedEmailIdentity]) {
228+
NSLog(@"The existing hashed email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[hashedEmailIdentity], hashedEmail);
210229
}
211230

212231
MPIdentityApiRequest *identityRequest = [MPIdentityApiRequest requestWithUser:user];
213232
[identityRequest setIdentity:email identityType:MPIdentityEmail];
214-
[identityRequest setIdentity:hashedEmail identityType:MPIdentityOther];
233+
[identityRequest setIdentity:hashedEmail identityType:hashedEmailIdentity.unsignedIntegerValue];
215234

216235
[[[MParticle sharedInstance] identity] identify:identityRequest completion:^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
217236
if (error) {

0 commit comments

Comments
 (0)