Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: userId in braze hyrbrid #1833

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ describe('hybrid mode', () => {
expect(window.braze.changeUser).toBeCalledTimes(0);
});

it('should not call the necessary Braze methods for identify call', () => {
it('should call the necessary Braze methods for identify call', () => {
const config = {
appKey: 'APP_KEY',
trackAnonymousUser: true,
Expand Down Expand Up @@ -1118,6 +1118,6 @@ describe('hybrid mode', () => {
braze.identify(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.changeUser).toBeCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ class Braze {
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
identify(rudderElement) {
const { message } = rudderElement;
const { userId } = message;

if (this.isHybridModeEnabled) {
window.braze.changeUser(userId);
Comment on lines +113 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure userId is defined before calling window.braze.changeUser.

In hybrid mode, the code calls window.braze.changeUser(userId) without checking if userId is defined. If userId is undefined or empty, this could cause an error or unexpected behavior. Consider adding a check to ensure userId is a non-empty string before calling changeUser.

Apply this diff to add the check:

        const { message } = rudderElement;
        const { userId } = message;

        if (this.isHybridModeEnabled) {
+          if (userId) {
            window.braze.changeUser(userId);
+          } else {
+            logger.error('User ID is undefined or empty in hybrid mode');
+          }
          return;
        }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { message } = rudderElement;
const { userId } = message;
if (this.isHybridModeEnabled) {
window.braze.changeUser(userId);
const { message } = rudderElement;
const { userId } = message;
if (this.isHybridModeEnabled) {
if (userId) {
window.braze.changeUser(userId);
} else {
logger.error('User ID is undefined or empty in hybrid mode');
}
return;
}

return;
}

const { message } = rudderElement;
const { userId } = message;
const { context } = message;
const email = context?.traits?.email;
const firstName = context?.traits?.firstName || context?.traits?.firstname;
Expand Down
Loading