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: braze anonymousId tracking with alias details #1994

Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -142,39 +142,99 @@ describe('isLoaded', () => {
});
});

describe('isLoaded', () => {
it('should get false value with isReady', () => {
const config = {};
const analytics = {};
const destinationInfo = {};
describe('setUserAlias', () => {
let braze;
let config;
let analytics;

const braze = new Braze(config, analytics, destinationInfo);
const isLoaded = braze.isReady();
expect(isLoaded).toBe(false);
});

it('should call the add alias method', () => {
const config = {
beforeEach(() => {
config = {
appKey: 'APP_KEY',
};
const analytics = {
getAnonymousId: jest.fn().mockReturnValue('anon123'),
analytics = {
getAnonymousId: jest.fn(),
};
const destinationInfo = {};

const braze = new Braze(config, analytics, destinationInfo);
braze = new Braze(config, analytics, {});
braze.init();
// mock the window.braze
mockBrazeSDK();

// mock true for isLoaded
});

it('should successfully set user alias', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser().addAlias.mockReturnValue(true);

const result = braze.setUserAlias();
expect(result).toBe(true);
expect(window.braze.getUser().addAlias).toHaveBeenCalledWith('anon123', 'rudder_id');
});

it('should fail when anonymous ID is missing', () => {
analytics.getAnonymousId.mockReturnValue(null);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should fail when user object is not available', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser = jest.fn().mockReturnValue(null);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should fail when addAlias returns false', () => {
analytics.getAnonymousId.mockReturnValue('anon123');
window.braze.getUser().addAlias.mockReturnValue(false);

const result = braze.setUserAlias();
expect(result).toBe(false);
});

it('should handle errors gracefully', () => {
analytics.getAnonymousId.mockImplementation(() => {
throw new Error('Test error');
});

const result = braze.setUserAlias();
expect(result).toBe(false);
});
});


describe('isReady', () => {
let braze;
let config;
let analytics;

beforeEach(() => {
config = { appKey: 'APP_KEY' };
analytics = { getAnonymousId: jest.fn() };
braze = new Braze(config, analytics, {});
});

it('should return false when not loaded', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(false);

const result = braze.isReady();
expect(result).toBe(false);
expect(braze.isLoaded).toHaveBeenCalled();
});

it('should return true when loaded and alias set successfully', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(true);
jest.spyOn(braze, 'setUserAlias').mockReturnValue(true);

jest.spyOn(window.braze, 'addAlias');
const result = braze.isReady();
expect(result).toBe(true);
});

it('should return false when loaded but alias setting fails', () => {
jest.spyOn(braze, 'isLoaded').mockReturnValue(true);
jest.spyOn(braze, 'setUserAlias').mockReturnValue(false);

const isReady = braze.isReady();
expect(window.braze.addAlias).toHaveBeenCalledWith('anon123', 'rudder_id');
expect(isReady).toBe(true);
const result = braze.isReady();
expect(result).toBe(false);
});
});

Expand Down Expand Up @@ -589,6 +649,7 @@ describe('track', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
rating: 3,
review_body: 'Good product.',
Expand Down Expand Up @@ -648,6 +709,7 @@ describe('track', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logPurchase).toHaveBeenCalledTimes(1);
expect(window.braze.logPurchase).toHaveBeenCalledWith('123454387', 15.99, 'USD', 1, {});
});

Expand Down Expand Up @@ -704,6 +766,7 @@ describe('track', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logPurchase).toHaveBeenCalledTimes(1);
expect(window.braze.logPurchase).toHaveBeenCalledWith('123454387', 15.99, 'USD', 1, {
rating: 5,
});
Expand Down Expand Up @@ -756,6 +819,7 @@ describe('track', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
rating: 3,
review_body: 'Good product.',
Expand Down Expand Up @@ -816,6 +880,7 @@ describe('track', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
products: [{ name: 'Game', price: 15.99, product_id: '123454387', quantity: 1 }],
});
Expand Down Expand Up @@ -855,6 +920,7 @@ describe('page', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Home', {
title: 'Home | RudderStack',
url: 'https://www.rudderstack.com',
Expand Down Expand Up @@ -892,6 +958,7 @@ describe('page', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'https://www.rudderstack.com',
Expand Down Expand Up @@ -929,6 +996,7 @@ describe('page', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'https://www.rudderstack.com',
Expand Down Expand Up @@ -966,6 +1034,7 @@ describe('page', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'https://www.rudderstack.com',
Expand Down Expand Up @@ -1006,6 +1075,7 @@ describe('page', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.logCustomEvent).toHaveBeenCalledTimes(1);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'https://www.rudderstack.com',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class Braze {
if (!config.appKey) this.appKey = '';
this.endPoint = '';
this.isHybridModeEnabled = config.connectionMode === 'hybrid';
this.isReadyStatus = {
hasLoggedErrorForAlias: false,
};

if (config.dataCenter) {
// ref: https://www.braze.com/docs/user_guide/administrative/access_braze/braze_instances
const dataCenterArr = config.dataCenter.trim().split('-');
Expand All @@ -50,6 +54,13 @@ class Braze {
} = destinationInfo ?? {});
}

logAliasError(message) {
if (!this.isReadyStatus.hasLoggedErrorForAlias) {
logger.error(message);
this.isReadyStatus.hasLoggedErrorForAlias = true;
}
}

init() {
loadNativeSdk();
window.braze.initialize(this.appKey, {
Expand All @@ -74,18 +85,38 @@ class Braze {
return window.brazeQueue === null;
}

isReady() {
if (this.isLoaded()) {
try {
const anonymousId = this.analytics.getAnonymousId();
window.braze.getUser().addAlias(anonymousId, 'rudder_id');
return true;
} catch (error) {
logger.error(`Error in isReady - ${stringifyWithoutCircularV1(error, true)}`);
setUserAlias() {
try {
const anonymousId = this.analytics.getAnonymousId();
if (!anonymousId) {
this.logAliasError('Anonymous ID is not available');
return false;
}

const user = window.braze.getUser();
if (!user) {
this.logAliasError('Braze user object is not available');
return false;
}

const aliasSet = user.addAlias(anonymousId, 'rudder_id');
if (!aliasSet) {
this.logAliasError('Failed to set alias for braze');
return false;
}

return true;
} catch (error) {
this.logAliasError(`Error setting alias: ${stringifyWithoutCircularV1(error, true)}`);
return false;
}
}

isReady() {
if (!this.isLoaded()) {
return false;
}
return false;
return this.setUserAlias();
}

/**
Expand Down Expand Up @@ -267,7 +298,13 @@ class Braze {
}
const eventName = rudderElement.message.event;
let { properties } = rudderElement.message;
if (eventName) {

const { userId } = rudderElement.message;
let canSendCustomEvent = false;
if (userId || this.trackAnonymousUser) {
canSendCustomEvent = true;
}
if (eventName && canSendCustomEvent) {
if (eventName.toLowerCase() === 'order completed') {
handlePurchase(properties);
} else {
Expand Down