Skip to content

Commit ebec5c9

Browse files
fix: Update Hashed Email Logic with Unassigned (#32)
1 parent 9a96175 commit ebec5c9

5 files changed

Lines changed: 91 additions & 10 deletions

File tree

mParticle-Rokt-Swift/MPRoktLayout.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,30 @@ public class MPRoktLayout {
5656
let hashedEmailIdentity = MPKitRokt.getHashedEmailUserIdentityType()
5757

5858
let userEmailIdentity = user.identities[NSNumber(value: MPIdentity.email.rawValue)]
59-
let userHashedEmailIdentity = user.identities[hashedEmailIdentity]
6059

61-
let emailMismatch = email != nil && email != userEmailIdentity
62-
let hashedEmailMismatch = hashedEmail != nil && hashedEmail != userHashedEmailIdentity
60+
let emailMismatch: Bool = {
61+
guard let email = email,
62+
let userEmail = user.identities[NSNumber(value: MPIdentity.email.rawValue)] else {
63+
return false
64+
}
65+
return email != userEmail
66+
}()
67+
let hashedEmailMismatch: Bool = {
68+
guard let hashedEmail = hashedEmail,
69+
let hashedEmailIdentity = hashedEmailIdentity,
70+
let userHashedEmail = user.identities[hashedEmailIdentity] else {
71+
return false
72+
}
73+
return hashedEmail != userHashedEmail
74+
}()
6375

6476
if emailMismatch || hashedEmailMismatch {
6577
// If there is an existing email or hashed email but it doesn't match what was passed in, warn the customer
6678
if emailMismatch {
6779
print("The existing email on the user (\(userEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(email ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
6880
}
6981
if hashedEmailMismatch {
70-
print("The existing hashed email on the user (\(userHashedEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
82+
print("The existing hashed email on the user (\(user.identities[hashedEmailIdentity ?? NSNumber(value: -1)] ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the hashed email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
7183
}
7284

7385
syncIdentities(user: user, email: email, hashedEmail: hashedEmail, hashedEmailKey: hashedEmailIdentity, completion: completion)
@@ -80,12 +92,14 @@ public class MPRoktLayout {
8092
user: MParticleUser,
8193
email: String?,
8294
hashedEmail: String?,
83-
hashedEmailKey: NSNumber,
95+
hashedEmailKey: NSNumber?,
8496
completion: @escaping () -> Void
8597
) {
8698
let identityRequest = MPIdentityApiRequest(user: user)
8799
identityRequest.setIdentity(email, identityType: .email)
88-
identityRequest.setIdentity(hashedEmail, identityType: MPIdentity(rawValue: hashedEmailKey.uintValue) ?? .other)
100+
if let hashedEmailKey = hashedEmailKey {
101+
identityRequest.setIdentity(hashedEmail, identityType: MPIdentity(rawValue: hashedEmailKey.uintValue) ?? .other)
102+
}
89103

90104
mparticle.identity.identify(identityRequest) {apiResult, error in
91105
if let error = error {

mParticle-Rokt/MPKitRokt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
@property (nonatomic, unsafe_unretained, readonly) BOOL started;
1818

1919
+ (NSDictionary<NSString *, NSString *> * _Nonnull)prepareAttributes:(NSDictionary<NSString *, NSString *> * _Nonnull)attributes filteredUser:(FilteredMParticleUser * _Nullable)filteredUser performMapping:(BOOL)performMapping;
20-
+ (NSNumber * _Nonnull)getRoktHashedEmailUserIdentityType;
20+
+ (NSNumber * _Nullable)getRoktHashedEmailUserIdentityType;
2121

2222
@end

mParticle-Rokt/MPKitRokt.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ + (RoktConfig *)convertMPRoktConfig:(MPRoktConfig *)mpRoktConfig {
366366
+ (NSString *)stringForIdentityType:(MPIdentity)identityType {
367367
NSNumber *hashedEmailIdentity = [MPKitRokt getRoktHashedEmailUserIdentityType];
368368

369-
if (hashedEmailIdentity.unsignedIntValue == identityType) {
369+
if (hashedEmailIdentity && hashedEmailIdentity.unsignedIntValue == identityType) {
370370
return @"emailsha256";
371371
}
372372

@@ -443,7 +443,7 @@ + (NSNumber *)getRoktHashedEmailUserIdentityType {
443443
NSString *hashedIdentityTypeString = roktKitConfig[kMPHashedEmailUserIdentityType];
444444
NSNumber *hashedIdentityTypeNumber = [MPKitRokt identityTypeForString:hashedIdentityTypeString.lowercaseString];
445445

446-
return hashedIdentityTypeNumber != nil ? hashedIdentityTypeNumber : @(MPIdentityOther);
446+
return hashedIdentityTypeNumber;
447447
}
448448

449449
- (MPKitExecStatus *)purchaseFinalized:(NSString *)placementId catalogItemId:(NSString *)catalogItemId success:(NSNumber *)success {

mParticle_RoktTests/mParticle_RoktTests.m

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ - (void)testAddIdentityAttributes {
236236
FilteredMParticleUser *filteredUser = [[FilteredMParticleUser alloc] init];
237237
id mockfilteredUser = OCMPartialMock(filteredUser);
238238
[[[mockfilteredUser stub] andReturn:testIdentities] userIdentities];
239+
id mockMPKitRoktClass = OCMClassMock([MPKitRokt class]);
240+
[[[mockMPKitRoktClass stub] andReturn:@(MPIdentityOther)] getRoktHashedEmailUserIdentityType];
239241

240242
[MPKitRokt addIdentityAttributes:passedAttributes filteredUser:filteredUser];
241243

@@ -267,6 +269,69 @@ - (void)testAddIdentityAttributes {
267269
XCTAssertEqualObjects(passedAttributes[@"device_application_stamp"], @"Test DAS");
268270
}
269271

272+
- (void)testAddIdentityAttributesUnassigned {
273+
NSMutableDictionary<NSString *, NSString *> *passedAttributes = [[NSMutableDictionary alloc] init];
274+
NSDictionary<NSNumber *, NSString *> *testIdentities = @{@(MPIdentityCustomerId): @"testCustomerID",
275+
@(MPIdentityEmail): @"testEmail@gmail.com",
276+
@(MPIdentityFacebook): @"testFacebook",
277+
@(MPIdentityFacebookCustomAudienceId): @"testCustomAudienceID",
278+
@(MPIdentityGoogle): @"testGoogle",
279+
@(MPIdentityMicrosoft): @"testMicrosoft",
280+
@(MPIdentityOther): @"testOther",
281+
@(MPIdentityTwitter): @"testTwitter",
282+
@(MPIdentityYahoo): @"testYahoo",
283+
@(MPIdentityOther2): @"testOther2",
284+
@(MPIdentityOther3): @"testOther3",
285+
@(MPIdentityOther4): @"testOther4",
286+
@(MPIdentityOther5): @"testOther5",
287+
@(MPIdentityOther6): @"testOther6",
288+
@(MPIdentityOther7): @"testOther7",
289+
@(MPIdentityOther8): @"testOther8",
290+
@(MPIdentityOther9): @"testOther9",
291+
@(MPIdentityOther10): @"testOther10",
292+
@(MPIdentityMobileNumber): @"1(234)-567-8910",
293+
@(MPIdentityPhoneNumber2): @"1(234)-567-2222",
294+
@(MPIdentityPhoneNumber3): @"1(234)-567-3333",
295+
@(MPIdentityIOSAdvertiserId): @"testAdvertID",
296+
@(MPIdentityIOSVendorId): @"testVendorID",
297+
@(MPIdentityPushToken): @"testPushToken",
298+
@(MPIdentityDeviceApplicationStamp): @"Test DAS"};
299+
300+
FilteredMParticleUser *filteredUser = [[FilteredMParticleUser alloc] init];
301+
id mockfilteredUser = OCMPartialMock(filteredUser);
302+
[[[mockfilteredUser stub] andReturn:testIdentities] userIdentities];
303+
id mockMPKitRoktClass = OCMClassMock([MPKitRokt class]);
304+
[[[mockMPKitRoktClass stub] andReturn:nil] getRoktHashedEmailUserIdentityType];
305+
306+
[MPKitRokt addIdentityAttributes:passedAttributes filteredUser:filteredUser];
307+
308+
XCTAssertEqualObjects(passedAttributes[@"customerid"], @"testCustomerID");
309+
XCTAssertEqualObjects(passedAttributes[@"email"], @"testEmail@gmail.com");
310+
XCTAssertEqualObjects(passedAttributes[@"facebook"], @"testFacebook");
311+
XCTAssertEqualObjects(passedAttributes[@"facebookcustomaudienceid"], @"testCustomAudienceID");
312+
XCTAssertEqualObjects(passedAttributes[@"google"], @"testGoogle");
313+
XCTAssertEqualObjects(passedAttributes[@"microsoft"], @"testMicrosoft");
314+
XCTAssertEqualObjects(passedAttributes[@"other"], @"testOther");
315+
XCTAssertEqualObjects(passedAttributes[@"twitter"], @"testTwitter");
316+
XCTAssertEqualObjects(passedAttributes[@"yahoo"], @"testYahoo");
317+
XCTAssertEqualObjects(passedAttributes[@"other2"], @"testOther2");
318+
XCTAssertEqualObjects(passedAttributes[@"other3"], @"testOther3");
319+
XCTAssertEqualObjects(passedAttributes[@"other4"], @"testOther4");
320+
XCTAssertEqualObjects(passedAttributes[@"other5"], @"testOther5");
321+
XCTAssertEqualObjects(passedAttributes[@"other6"], @"testOther6");
322+
XCTAssertEqualObjects(passedAttributes[@"other7"], @"testOther7");
323+
XCTAssertEqualObjects(passedAttributes[@"other8"], @"testOther8");
324+
XCTAssertEqualObjects(passedAttributes[@"other9"], @"testOther9");
325+
XCTAssertEqualObjects(passedAttributes[@"other10"], @"testOther10");
326+
XCTAssertEqualObjects(passedAttributes[@"mobile_number"], @"1(234)-567-8910");
327+
XCTAssertEqualObjects(passedAttributes[@"phone_number_2"], @"1(234)-567-2222");
328+
XCTAssertEqualObjects(passedAttributes[@"phone_number_3"], @"1(234)-567-3333");
329+
XCTAssertEqualObjects(passedAttributes[@"ios_idfa"], @"testAdvertID");
330+
XCTAssertEqualObjects(passedAttributes[@"ios_idfv"], @"testVendorID");
331+
XCTAssertEqualObjects(passedAttributes[@"push_token"], @"testPushToken");
332+
XCTAssertEqualObjects(passedAttributes[@"device_application_stamp"], @"Test DAS");
333+
}
334+
270335
- (void)testAddIdentityAttributesWithExistingAttributes {
271336
NSMutableDictionary<NSString *, NSString *> *passedAttributes = [[NSMutableDictionary alloc] init];
272337
[passedAttributes setObject:@"bar" forKey:@"foo"];
@@ -299,6 +364,8 @@ - (void)testAddIdentityAttributesWithExistingAttributes {
299364
FilteredMParticleUser *filteredUser = [[FilteredMParticleUser alloc] init];
300365
id mockfilteredUser = OCMPartialMock(filteredUser);
301366
[[[mockfilteredUser stub] andReturn:testIdentities] userIdentities];
367+
id mockMPKitRoktClass = OCMClassMock([MPKitRokt class]);
368+
[[[mockMPKitRoktClass stub] andReturn:@(MPIdentityOther)] getRoktHashedEmailUserIdentityType];
302369

303370
[MPKitRokt addIdentityAttributes:passedAttributes filteredUser:filteredUser];
304371

mParticle_RoktTests/mParticle_Rokt_SwiftTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ struct mParticle_Rokt_SwiftTests {
273273
)
274274

275275
// Then
276-
#expect(layout.roktLayout == nil, "Layout should attempt to identify user attributes and fail")
276+
#expect(layout.roktLayout != nil, "Layout should attempt to identify user attributes and fail")
277277
}
278278

279279
@MainActor @available(iOS 15, *)

0 commit comments

Comments
 (0)