Skip to content

Commit cf803ae

Browse files
committed
Catch for fs_user_id aliases
1 parent 34fcdc9 commit cf803ae

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ describe('javascript-sdk (Browser)', function() {
605605
beforeEach(function() {
606606
sandbox.stub(logger, 'log');
607607
sandbox.stub(logger, 'error');
608+
sandbox.stub(logger, 'warn');
608609
clock = sinon.useFakeTimers(new Date());
609610
});
610611

@@ -804,6 +805,77 @@ describe('javascript-sdk (Browser)', function() {
804805
sinon.assert.called(fakeEventManager.sendEvent);
805806
});
806807

808+
it('should convert fs-user-id, FS-USER-ID, and FS_USER_ID to fs_user_id identifier when calling sendOdpEvent', async () => {
809+
const fakeEventManager = {
810+
updateSettings: sinon.spy(),
811+
start: sinon.spy(),
812+
stop: sinon.spy(),
813+
registerVuid: sinon.spy(),
814+
identifyUser: sinon.spy(),
815+
sendEvent: sinon.spy(),
816+
flush: sinon.spy(),
817+
};
818+
819+
const client = optimizelyFactory.createInstance({
820+
datafile: testData.getOdpIntegratedConfigWithSegments(),
821+
errorHandler: fakeErrorHandler,
822+
eventDispatcher: fakeEventDispatcher,
823+
eventBatchSize: null,
824+
logger,
825+
odpOptions: {
826+
eventManager: fakeEventManager,
827+
},
828+
});
829+
830+
const readyData = await client.onReady();
831+
assert.equal(readyData.success, true);
832+
assert.isUndefined(readyData.reason);
833+
834+
// fs-user-id
835+
client.sendOdpEvent(ODP_EVENT_ACTION.INITIALIZED, undefined, new Map([['fs-user-id', 'fsUserA']]));
836+
sinon.assert.notCalled(logger.error);
837+
sinon.assert.neverCalledWith(logger.warn, LOG_MESSAGES.ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED);
838+
839+
const sendEventArgs1 = fakeEventManager.sendEvent.args;
840+
assert.deepEqual(
841+
sendEventArgs1[0].toString(),
842+
new OdpEvent('fullstack', 'client_initialized', new Map([['fs_user_id', 'fsUserA']]), new Map()).toString()
843+
);
844+
845+
// FS-USER-ID
846+
client.sendOdpEvent(ODP_EVENT_ACTION.INITIALIZED, undefined, new Map([['FS-USER-ID', 'fsUserA']]));
847+
sinon.assert.notCalled(logger.error);
848+
sinon.assert.neverCalledWith(logger.warn, LOG_MESSAGES.ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED);
849+
850+
const sendEventArgs2 = fakeEventManager.sendEvent.args;
851+
assert.deepEqual(
852+
sendEventArgs2[0].toString(),
853+
new OdpEvent('fullstack', 'client_initialized', new Map([['fs_user_id', 'fsUserA']]), new Map()).toString()
854+
);
855+
856+
// FS_USER_ID
857+
client.sendOdpEvent(ODP_EVENT_ACTION.INITIALIZED, undefined, new Map([['FS_USER_ID', 'fsUserA']]));
858+
sinon.assert.notCalled(logger.error);
859+
sinon.assert.neverCalledWith(logger.warn, LOG_MESSAGES.ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED);
860+
861+
const sendEventArgs3 = fakeEventManager.sendEvent.args;
862+
assert.deepEqual(
863+
sendEventArgs3[0].toString(),
864+
new OdpEvent('fullstack', 'client_initialized', new Map([['fs_user_id', 'fsUserA']]), new Map()).toString()
865+
);
866+
867+
// fs_user_id
868+
client.sendOdpEvent(ODP_EVENT_ACTION.INITIALIZED, undefined, new Map([['fs_user_id', 'fsUserA']]));
869+
sinon.assert.notCalled(logger.error);
870+
sinon.assert.neverCalledWith(logger.warn, LOG_MESSAGES.ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED);
871+
872+
const sendEventArgs4 = fakeEventManager.sendEvent.args;
873+
assert.deepEqual(
874+
sendEventArgs4[0].toString(),
875+
new OdpEvent('fullstack', 'client_initialized', new Map([['fs_user_id', 'fsUserA']]), new Map()).toString()
876+
);
877+
});
878+
807879
it('should throw an error and not send an odp event when calling sendOdpEvent with an invalid action input', async () => {
808880
const fakeEventManager = {
809881
updateSettings: sinon.spy(),

packages/optimizely-sdk/lib/optimizely/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import {
6565
NODE_CLIENT_ENGINE,
6666
NODE_CLIENT_VERSION,
6767
ODP_DEFAULT_EVENT_TYPE,
68+
FS_USER_ID_ALIAS,
69+
ODP_USER_KEY,
6870
} from '../utils/enums';
6971

7072
const MODULE_NAME = 'OPTIMIZELY';
@@ -1712,8 +1714,27 @@ export default class Optimizely implements Client {
17121714

17131715
const odpEventType = type ?? ODP_DEFAULT_EVENT_TYPE;
17141716

1717+
const odpIdentifiers = new Map(identifiers);
1718+
1719+
if (identifiers && identifiers.size > 0) {
1720+
try {
1721+
identifiers.forEach((identifier_value, identifier_key) => {
1722+
// Catch for fs-user-id, FS-USER-ID, and FS_USER_ID and assign value to fs_user_id identifier.
1723+
if (
1724+
FS_USER_ID_ALIAS === identifier_key.toLowerCase() ||
1725+
ODP_USER_KEY.FS_USER_ID === identifier_key.toLowerCase()
1726+
) {
1727+
odpIdentifiers.delete(identifier_key);
1728+
odpIdentifiers.set(ODP_USER_KEY.FS_USER_ID, identifier_value);
1729+
}
1730+
});
1731+
} catch (e) {
1732+
this.logger.warn(LOG_MESSAGES.ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED);
1733+
}
1734+
}
1735+
17151736
try {
1716-
const odpEvent = new OdpEvent(odpEventType, action, identifiers, data);
1737+
const odpEvent = new OdpEvent(odpEventType, action, odpIdentifiers, data);
17171738
this.odpManager.sendEvent(odpEvent);
17181739
} catch (e) {
17191740
this.logger.error(ERROR_MESSAGES.ODP_EVENT_FAILED, e);

packages/optimizely-sdk/lib/utils/enums/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export const LOG_MESSAGES = {
123123
ODP_DISABLED: 'ODP Disabled.',
124124
ODP_IDENTIFY_FAILED_ODP_DISABLED: '%s: ODP identify event for user %s is not dispatched (ODP disabled).',
125125
ODP_IDENTIFY_FAILED_ODP_NOT_INTEGRATED: '%s: ODP identify event %s is not dispatched (ODP not integrated).',
126+
ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED:
127+
'%s: sendOdpEvent failed to parse through and convert fs_user_id aliases',
126128
PARSED_REVENUE_VALUE: '%s: Parsed revenue value "%s" from event tags.',
127129
PARSED_NUMERIC_VALUE: '%s: Parsed event value "%s" from event tags.',
128130
RETURNING_STORED_VARIATION:
@@ -349,6 +351,11 @@ export enum ODP_USER_KEY {
349351
FS_USER_ID = 'fs_user_id',
350352
}
351353

354+
/**
355+
* Alias for fs_user_id to catch for and automatically convert to fs_user_id
356+
*/
357+
export const FS_USER_ID_ALIAS = 'fs-user-id';
358+
352359
export const ODP_DEFAULT_EVENT_TYPE = 'fullstack';
353360
export const ODP_EVENT_BROWSER_ENDPOINT = 'https://jumbe.zaius.com/v2/zaius.gif';
354361

0 commit comments

Comments
 (0)