Skip to content

Commit 3fb8cb1

Browse files
authored
feat: SDKE-221 Support hashedEmailUserIdentityType for "other" identity type (#1052)
1 parent c95519d commit 3fb8cb1

File tree

2 files changed

+361
-6
lines changed

2 files changed

+361
-6
lines changed

src/roktManager.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import {
88
generateUniqueId,
99
isFunction,
1010
AttributeValue,
11+
isEmpty,
1112
} from "./utils";
1213
import { SDKIdentityApi } from "./identity.interfaces";
1314
import { SDKLoggerApi } from "./sdkRuntimeModels";
1415
import { IStore, LocalSessionAttributes } from "./store";
16+
import { UserIdentities } from "@mparticle/web-sdk";
17+
import { IdentityType } from "./types";
1518

1619
// https://docs.rokt.com/developers/integration-guides/web/library/attributes
1720
export interface IRoktPartnerAttributes {
@@ -96,6 +99,7 @@ export default class RoktManager {
9699
private launcherOptions?: IRoktLauncherOptions;
97100
private logger: SDKLoggerApi;
98101
private domain?: string;
102+
private mappedEmailShaIdentityType?: string | null;
99103
/**
100104
* Initializes the RoktManager with configuration settings and user data.
101105
*
@@ -116,7 +120,8 @@ export default class RoktManager {
116120
options?: IRoktOptions
117121
): void {
118122
const { userAttributeFilters, settings } = roktConfig || {};
119-
const { placementAttributesMapping } = settings || {};
123+
const { placementAttributesMapping, hashedEmailUserIdentityType } = settings || {};
124+
this.mappedEmailShaIdentityType = hashedEmailUserIdentityType?.toLowerCase() ?? null;
120125

121126
this.identityService = identityService;
122127
this.store = store;
@@ -182,23 +187,43 @@ export default class RoktManager {
182187
// Get current user identities
183188
this.currentUser = this.identityService.getCurrentUser();
184189
const currentUserIdentities = this.currentUser?.getUserIdentities()?.userIdentities || {};
190+
185191
const currentEmail = currentUserIdentities.email;
186192
const newEmail = mappedAttributes.email as string;
187193

188-
// https://go.mparticle.com/work/SQDSDKS-7338
189-
// Check if email exists and differs
190-
if (newEmail && (!currentEmail || currentEmail !== newEmail)) {
191-
if (currentEmail && currentEmail !== newEmail) {
194+
let currentHashedEmail: string | undefined;
195+
let newHashedEmail: string | undefined;
196+
197+
// Hashed email identity is valid if it is set to Other-Other10
198+
if(this.mappedEmailShaIdentityType && IdentityType.getIdentityType(this.mappedEmailShaIdentityType) !== false) {
199+
currentHashedEmail = currentUserIdentities[this.mappedEmailShaIdentityType];
200+
newHashedEmail = mappedAttributes['emailsha256'] as string || mappedAttributes[this.mappedEmailShaIdentityType] as string || undefined;
201+
}
202+
203+
const emailChanged = this.hasIdentityChanged(currentEmail, newEmail);
204+
const hashedEmailChanged = this.hasIdentityChanged(currentHashedEmail, newHashedEmail);
205+
206+
const newIdentities: UserIdentities = {};
207+
if (emailChanged) {
208+
newIdentities.email = newEmail;
209+
if (newEmail) {
192210
this.logger.warning(`Email mismatch detected. Current email, ${currentEmail} differs from email passed to selectPlacements call, ${newEmail}. Proceeding to call identify with ${newEmail}. Please verify your implementation.`);
193211
}
212+
}
213+
214+
if (hashedEmailChanged) {
215+
newIdentities[this.mappedEmailShaIdentityType] = newHashedEmail;
216+
this.logger.warning(`emailsha256 mismatch detected. Current mParticle ${this.mappedEmailShaIdentityType} identity, ${currentHashedEmail}, differs from 'emailsha256' passed to selectPlacements call, ${newHashedEmail}. Proceeding to call identify with ${this.mappedEmailShaIdentityType} set to ${newHashedEmail}. Please verify your implementation`);
217+
}
194218

219+
if (!isEmpty(newIdentities)) {
195220
// Call identify with the new user identities
196221
try {
197222
await new Promise<void>((resolve, reject) => {
198223
this.identityService.identify({
199224
userIdentities: {
200225
...currentUserIdentities,
201-
email: newEmail
226+
...newIdentities
202227
}
203228
}, () => {
204229
resolve();
@@ -321,6 +346,7 @@ export default class RoktManager {
321346
this.messageQueue.forEach((message) => {
322347
if(!(message.methodName in this) || !isFunction(this[message.methodName])) {
323348
this.logger?.error(`RoktManager: Method ${message.methodName} not found`);
349+
324350
return;
325351
}
326352

@@ -373,4 +399,27 @@ export default class RoktManager {
373399

374400
this.messageQueue.delete(messageId);
375401
}
402+
403+
/**
404+
* Checks if an identity value has changed by comparing current and new values
405+
*
406+
* @param {string | undefined} currentValue - The current identity value
407+
* @param {string | undefined} newValue - The new identity value to compare against
408+
* @returns {boolean} True if the identity has changed (new value exists and differs from current), false otherwise
409+
*/
410+
private hasIdentityChanged(currentValue: string | undefined, newValue: string | undefined): boolean {
411+
if (!newValue) {
412+
return false;
413+
}
414+
415+
if (!currentValue) {
416+
return true; // New value exists but no current value
417+
}
418+
419+
if (currentValue !== newValue) {
420+
return true; // Values are different
421+
}
422+
423+
return false; // Values are the same
424+
}
376425
}

0 commit comments

Comments
 (0)