Skip to content

Commit c50b101

Browse files
fix: Update Hashed Email Logic With Unassigned (#401)
* fix: Update Hashed Email Logic with Unassigned
1 parent 9032458 commit c50b101

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

UnitTests/MPRoktTests.m

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,70 @@ - (void)hashedIdentityTest: (MPIdentity)mpIdentity {
469469
[self.identityMock verifyWithDelay:0.2];
470470
}
471471

472+
- (void)testDontTriggerIdentifyWithNoRoktHashedEmailUserIdentityType {
473+
MParticleUser *currentUser = [MParticle sharedInstance].identity.currentUser;
474+
475+
MPUserDefaults *userDefaults = [MPUserDefaults standardUserDefaultsWithStateMachine:[MParticle sharedInstance].stateMachine backendController:[MParticle sharedInstance].backendController identity:[MParticle sharedInstance].identity];
476+
477+
NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:MPIdentityOther], @"i" : @"[email protected]"}];
478+
479+
[userDefaults setMPObject:userIdentityArray forKey:kMPUserIdentityArrayKey userId:currentUser.userId];
480+
XCTAssertEqualObjects(currentUser.identities[@(MPIdentityOther)], @"[email protected]");
481+
482+
//Mock Identity as needed
483+
[[[self.mockRokt stub] andReturn:nil] getRoktHashedEmailUserIdentityType];
484+
MParticle *instance = [MParticle sharedInstance];
485+
self.mockInstance = OCMPartialMock(instance);
486+
self.identityMock = OCMClassMock([MPIdentityApi class]);
487+
OCMStub([(MParticle *)self.mockInstance identity]).andReturn(self.identityMock);
488+
[[[self.mockInstance stub] andReturn:self.mockInstance] sharedInstance];
489+
[[[self.identityMock stub] andReturn:currentUser] currentUser];
490+
491+
[[self.identityMock reject] identify:[OCMArg checkWithBlock:^BOOL(MPIdentityApiRequest *request) {
492+
XCTAssertEqualObjects([request.identities objectForKey:@(MPIdentityOther)], @"[email protected]");
493+
return true;
494+
}] completion:OCMOCK_ANY];
495+
496+
NSString *identifier = @"testView";
497+
NSDictionary *attributes = @{@"emailsha256": @"[email protected]", @"sandbox": @"false"};
498+
499+
[self.rokt selectPlacements:identifier attributes:attributes];
500+
501+
[self.identityMock verifyWithDelay:0.2];
502+
}
503+
504+
- (void)testTriggeredIdentifyWithNoRoktHashedEmailUserIdentityType {
505+
MParticleUser *currentUser = [MParticle sharedInstance].identity.currentUser;
506+
507+
MPUserDefaults *userDefaults = [MPUserDefaults standardUserDefaultsWithStateMachine:[MParticle sharedInstance].stateMachine backendController:[MParticle sharedInstance].backendController identity:[MParticle sharedInstance].identity];
508+
509+
NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:MPIdentityOther], @"i" : @"[email protected]"}];
510+
511+
[userDefaults setMPObject:userIdentityArray forKey:kMPUserIdentityArrayKey userId:currentUser.userId];
512+
XCTAssertEqualObjects(currentUser.identities[@(MPIdentityOther)], @"[email protected]");
513+
514+
//Mock Identity as needed
515+
[[[self.mockRokt stub] andReturn:nil] getRoktHashedEmailUserIdentityType];
516+
MParticle *instance = [MParticle sharedInstance];
517+
self.mockInstance = OCMPartialMock(instance);
518+
self.identityMock = OCMClassMock([MPIdentityApi class]);
519+
OCMStub([(MParticle *)self.mockInstance identity]).andReturn(self.identityMock);
520+
[[[self.mockInstance stub] andReturn:self.mockInstance] sharedInstance];
521+
[[[self.identityMock stub] andReturn:currentUser] currentUser];
522+
523+
[[self.identityMock expect] identify:[OCMArg checkWithBlock:^BOOL(MPIdentityApiRequest *request) {
524+
XCTAssertEqualObjects([request.identities objectForKey:@(MPIdentityEmail)], @"[email protected]");
525+
return true;
526+
}] completion:OCMOCK_ANY];
527+
528+
NSString *identifier = @"testView";
529+
NSDictionary *attributes = @{@"email": @"[email protected]", @"emailsha256": @"[email protected]", @"sandbox": @"false"};
530+
531+
[self.rokt selectPlacements:identifier attributes:attributes];
532+
533+
[self.identityMock verifyWithDelay:0.2];
534+
}
535+
472536
- (void)testPurchaseFinalized {
473537
MParticle *instance = [MParticle sharedInstance];
474538
self.mockInstance = OCMPartialMock(instance);

mParticle-Apple-SDK/MPRokt.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ - (NSNumber *)getRoktHashedEmailUserIdentityType {
193193
NSString *hashedIdentityTypeString = roktKitConfig[kMPHashedEmailUserIdentityType];
194194
NSNumber *hashedIdentityTypeNumber = [MPIdentityHTTPIdentities identityTypeForString:hashedIdentityTypeString.lowercaseString];
195195

196-
return hashedIdentityTypeNumber != nil ? hashedIdentityTypeNumber : @(MPIdentityOther);
196+
return hashedIdentityTypeNumber;
197197
}
198198

199199
- (NSDictionary<NSString *, NSString *> *)confirmSandboxAttribute:(NSDictionary<NSString *, NSString *> * _Nullable)attributes {
@@ -220,17 +220,16 @@ - (void)confirmUser:(NSDictionary<NSString *, NSString *> * _Nullable)attributes
220220
NSString *hashedEmail = attributes[@"emailsha256"];
221221
NSNumber *hashedEmailIdentity = [self getRoktHashedEmailUserIdentityType];
222222

223-
if ((email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]) || (hashedEmail && ![hashedEmail isEqualToString: user.identities[hashedEmailIdentity]])) {
224-
// If there is an existing email or hashed email but it doesn't match the what was passed in, warn the customer
225-
if (email && user.identities[@(MPIdentityEmail)]) {
226-
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);
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);
229-
}
230-
223+
BOOL shouldIdentifyFromEmail = (email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]);
224+
BOOL shouldIdentifyFromHash = (hashedEmail && hashedEmailIdentity && ![hashedEmail isEqualToString: user.identities[hashedEmailIdentity]]);
225+
226+
if (shouldIdentifyFromEmail || shouldIdentifyFromHash) {
227+
// Identify the user with the new identity information
231228
MPIdentityApiRequest *identityRequest = [MPIdentityApiRequest requestWithUser:user];
232229
[identityRequest setIdentity:email identityType:MPIdentityEmail];
233-
[identityRequest setIdentity:hashedEmail identityType:hashedEmailIdentity.unsignedIntegerValue];
230+
if (hashedEmailIdentity != nil) {
231+
[identityRequest setIdentity:hashedEmail identityType:hashedEmailIdentity.unsignedIntegerValue];
232+
}
234233

235234
[[[MParticle sharedInstance] identity] identify:identityRequest completion:^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
236235
if (error) {
@@ -241,6 +240,13 @@ - (void)confirmUser:(NSDictionary<NSString *, NSString *> * _Nullable)attributes
241240
completion(apiResult.user);
242241
}
243242
}];
243+
244+
// Warn the customer if we had to identify and therefore delay their Rokt placement.
245+
if (shouldIdentifyFromEmail) {
246+
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 continuing to `selectPlacements:`", user.identities[@(MPIdentityEmail)], email);
247+
} else if (shouldIdentifyFromHash) {
248+
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 continuing to `selectPlacements:`", user.identities[hashedEmailIdentity], hashedEmail);
249+
}
244250
} else {
245251
completion(user);
246252
}

0 commit comments

Comments
 (0)