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 @@ -28,6 +28,7 @@ const mockBrazeSDK = () => {
throw new Error('Braze SDK Error: changeUser requires a non-empty userId. (v4.2.1)');
}
}),
addAlias: jest.fn(),
openSession: jest.fn(),
getUser: jest.fn().mockReturnThis(),
setCountry: jest.fn(),
Expand Down Expand Up @@ -151,6 +152,30 @@ describe('isLoaded', () => {
const isLoaded = braze.isReady();
expect(isLoaded).toBe(false);
});

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

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

// mock true for isLoaded
jest.spyOn(braze, 'isLoaded').mockReturnValue(true);

jest.spyOn(window.braze, 'addAlias');
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved

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

describe('identify', () => {
Expand Down Expand Up @@ -561,11 +586,9 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
rating: 3,
review_body: 'Good product.',
Expand Down Expand Up @@ -622,11 +645,9 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

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

Expand Down Expand Up @@ -680,11 +701,9 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logPurchase).toHaveBeenCalledWith('123454387', 15.99, 'USD', 1, {
rating: 5,
});
Expand Down Expand Up @@ -734,11 +753,14 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
rating: 3,
review_body: 'Good product.',
review_id: '12345',
});
});

it('should call the necessary Braze methods for order completed event wit hreserved properties', () => {
Expand Down Expand Up @@ -791,11 +813,9 @@ describe('track', () => {
},
};

jest.spyOn(window.braze, 'changeUser');
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Product Reviewed', {
products: [{ name: 'Game', price: 15.99, product_id: '123454387', quantity: 1 }],
});
Expand Down Expand Up @@ -827,19 +847,17 @@ describe('page', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Home', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -866,19 +884,17 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toHaveBeenCalledWith('user123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -905,19 +921,17 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -944,20 +958,17 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
});
});

Expand All @@ -984,23 +995,20 @@ describe('page', () => {
type: 'page',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
event_name: 'ABC',
referer: 'index',
currency: 'usd',
},
},
};

jest.spyOn(window.braze, 'changeUser');
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledWith('anon123');
expect(window.braze.logCustomEvent).toHaveBeenCalledWith('Page View', {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
referer: 'index',
});
});
Expand Down Expand Up @@ -1033,7 +1041,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1042,7 +1050,7 @@ describe('hybrid mode', () => {
braze.page(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.changeUser).toHaveBeenCalledTimes(0);
});

it('should not call the necessary Braze methods for track call', () => {
Expand Down Expand Up @@ -1071,7 +1079,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1080,7 +1088,7 @@ describe('hybrid mode', () => {
braze.track(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
expect(window.braze.changeUser).toHaveBeenCalledTimes(0);
});

it('should call the necessary Braze methods for identify call', () => {
Expand Down Expand Up @@ -1109,7 +1117,7 @@ describe('hybrid mode', () => {
name: 'Home',
properties: {
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
url: 'https://www.rudderstack.com',
},
},
};
Expand All @@ -1118,6 +1126,6 @@ describe('hybrid mode', () => {
braze.identify(rudderElement);

// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(1);
expect(window.braze.changeUser).toHaveBeenCalledTimes(1);
});
});
utsabc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@
}

isReady() {
return this.isLoaded();
if (this.isLoaded()) {
try {
const anonymousId = this.analytics.getAnonymousId();
window.braze.getUser().addAlias(anonymousId, 'rudder_id');
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
return true;
} catch (error) {
logger.error(`Error in isReady - ${stringifyWithoutCircularV1(error, true)}`);
return false;

Check warning on line 85 in packages/analytics-js-integrations/src/integrations/Braze/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/Braze/browser.js#L84-L85

Added lines #L84 - L85 were not covered by tests
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false;
}

/**
Expand Down Expand Up @@ -255,21 +265,11 @@
if (this.isHybridModeEnabled) {
return;
}
const { userId } = rudderElement.message;
const eventName = rudderElement.message.event;
let { properties } = rudderElement.message;
const { anonymousId } = rudderElement.message;
let canSendCustomEvent = false;
if (userId) {
window.braze.changeUser(userId);
canSendCustomEvent = true;
} else if (this.trackAnonymousUser) {
window.braze.changeUser(anonymousId);
canSendCustomEvent = true;
}
if (eventName && canSendCustomEvent) {
if (eventName) {
if (eventName.toLowerCase() === 'order completed') {
handlePurchase(properties, userId);
handlePurchase(properties);
} else {
properties = handleReservedProperties(properties);
window.braze.logCustomEvent(eventName, properties);
Expand All @@ -281,15 +281,8 @@
if (this.isHybridModeEnabled) {
return;
}
const { userId } = rudderElement.message;
const eventName = rudderElement.message.name;
let { properties } = rudderElement.message;
const { anonymousId } = rudderElement.message;
if (userId) {
window.braze.changeUser(userId);
} else if (this.trackAnonymousUser) {
window.braze.changeUser(anonymousId);
}
properties = handleReservedProperties(properties);
if (eventName) {
window.braze.logCustomEvent(eventName, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ const handleReservedProperties = props => {
return props;
};

const handlePurchase = (properties, userId) => {
const handlePurchase = (properties) => {
const { products, currency } = properties;
const currencyCode = currency;

window.braze.changeUser(userId);

// del used properties
del(properties, 'products');
del(properties, 'currency');
Expand Down
Loading