Skip to content

Commit

Permalink
Merge pull request #1452 from cmgustavo/bug/braze-duplicate-user-05
Browse files Browse the repository at this point in the history
Fix: duplicated users after merge/delete Braze profile
  • Loading branch information
JohnathanWhite authored Nov 8, 2024
2 parents c19f900 + 1439fd1 commit 7ccefe9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import {
setAccountEVMCreationMigrationComplete,
successAddWallet,
} from './store/wallet/wallet.actions';
import {BrazeWrapper} from './lib/Braze';

const {Timer, SilentPushEvent, InAppMessageModule} = NativeModules;

Expand Down Expand Up @@ -276,7 +277,6 @@ export default () => {
const lockAuthorizedUntil = useAppSelector(
({APP}) => APP.lockAuthorizedUntil,
);
const inAppMessageData = useAppSelector(({APP}) => APP.inAppMessageData);
const keys = useAppSelector(({WALLET}) => WALLET.keys);
const accountEvmCreationMigrationComplete = useAppSelector(
({WALLET}) => WALLET.accountEvmCreationMigrationComplete,
Expand Down Expand Up @@ -471,6 +471,24 @@ export default () => {
return () => subscriptionAppStateChange.remove();
}, [pinLockActive, biometricLockActive]);

useEffect(() => {
const eventBrazeListener = DeviceEventEmitter.addListener(
DeviceEmitterEvents.SHOULD_DELETE_BRAZE_USER,
async eid => {
// Wait for a few seconds to ensure the user is deleted
await sleep(20000);
LogActions.info('Deleting old user EID: ', eid);
await BrazeWrapper.delete(eid);
await sleep(3000);
BrazeWrapper.endMergingUser();
},
);

return () => {
eventBrazeListener.remove();
};
}, []);

// THEME
useEffect(() => {
function onAppStateChange(status: AppStateStatus) {
Expand Down
1 change: 1 addition & 0 deletions src/constants/device-emitter-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export enum DeviceEmitterEvents {
PUSH_NOTIFICATIONS = 'PUSH_NOTIFICATIONS',
FIX_WALLET_ADDRESS = 'FIX_WALLET_ADDRESS',
APP_TOKENS_DATA_LOADED = 'APP_TOKENS_DATA_LOADED',
SHOULD_DELETE_BRAZE_USER = 'SHOULD_DELETE_BRAZE_USER',
}
20 changes: 18 additions & 2 deletions src/lib/Braze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const BrazeWrapper = (() => {
userId?: string;
attributes?: BrazeUserAttributes;
} = {};
let mergingUser = false;

return {
init() {
Expand All @@ -148,6 +149,9 @@ export const BrazeWrapper = (() => {
userId: string | undefined,
attributes?: BrazeUserAttributes | undefined,
) {
if (mergingUser) {
return;
}
if (!lastSeenIdentity) {
lastSeenIdentity = {};
}
Expand Down Expand Up @@ -175,6 +179,14 @@ export const BrazeWrapper = (() => {
};
},

startMergingUser() {
mergingUser = true;
},

endMergingUser() {
mergingUser = false;
},

merge(userToMerge: string, userToKeep: string) {
return mergeUsers(userToMerge, userToKeep);
},
Expand All @@ -186,11 +198,15 @@ export const BrazeWrapper = (() => {
screen(name: string, properties: Record<string, any> = {}) {
const screenName = `Viewed ${name} Screen`;

Braze.logCustomEvent(screenName, properties);
if (!mergingUser) {
Braze.logCustomEvent(screenName, properties);
}
},

track(eventName: string, properties: Record<string, any> = {}) {
Braze.logCustomEvent(eventName, properties);
if (!mergingUser) {
Braze.logCustomEvent(eventName, properties);
}
},
};
})();
Expand Down
13 changes: 10 additions & 3 deletions src/store/bitpay-id/bitpay-id.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import axios from 'axios';
import {BASE_BITPAY_URLS} from '../../constants/config';
import Braze from '@braze/react-native-sdk';
import {dismissOnGoingProcessModal, setBrazeEid} from '../app/app.actions';
import {DeviceEmitterEvents} from '../../constants/device-emitter-events';
import {DeviceEventEmitter} from 'react-native';

interface StartLoginParams {
email: string;
Expand Down Expand Up @@ -59,10 +61,15 @@ export const startBitPayIdAnalyticsInit =

// Check if Braze EID exists and different
if (APP.brazeEid && APP.brazeEid !== eid) {
BrazeWrapper.startMergingUser();
// Should migrate the user to the new EID
LogActions.info('Merging current user to new EID: ', eid);
await BrazeWrapper.merge(APP.brazeEid, eid);
// Delete old user
await BrazeWrapper.delete(APP.brazeEid);
// Emit an event that delete the user
DeviceEventEmitter.emit(
DeviceEmitterEvents.SHOULD_DELETE_BRAZE_USER,
APP.brazeEid,
);
}

dispatch(setBrazeEid(eid));
Expand Down Expand Up @@ -456,7 +463,7 @@ const startPairAndLoadUser =
);
}

dispatch(startBitPayIdStoreInit(data.user));
await dispatch(startBitPayIdStoreInit(data.user));
dispatch(CardEffects.startCardStoreInit(data.user));
dispatch(ShopEffects.startFetchCatalog());
dispatch(ShopEffects.startSyncGiftCards()).then(() =>
Expand Down

0 comments on commit 7ccefe9

Please sign in to comment.