Skip to content

Commit 194f76c

Browse files
committed
[AI-FSSDK] [FSSDK-12721] Skip ODP identify event when fewer than 2 valid identifiers
1 parent a2b378c commit 194f76c

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎lib/odp/odp_manager.spec.ts‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ describe('DefaultOdpManager', () => {
618618
expect(identifiers).toEqual(new Map([['fs_user_id', 'user'], ['vuid', 'vuid_a']]));
619619
});
620620

621-
it('sends identified event when called with just fs_user_id in first parameter', async () => {
621+
it('does not send identified event when called with just fs_user_id and no vuid set', async () => {
622622
const eventManager = getMockOdpEventManager();
623623
eventManager.onRunning.mockReturnValue(Promise.resolve());
624624

@@ -634,12 +634,10 @@ describe('DefaultOdpManager', () => {
634634
await odpManager.onRunning();
635635

636636
odpManager.identifyUser('user');
637-
expect(mockSendEvents).toHaveBeenCalledOnce();
638-
const { identifiers } = mockSendEvents.mock.calls[0][0];
639-
expect(identifiers).toEqual(new Map([['fs_user_id', 'user']]));
637+
expect(mockSendEvents).not.toHaveBeenCalled();
640638
});
641639

642-
it('sends identified event when called with just vuid in first parameter', async () => {
640+
it('does not send identified event when called with just vuid in first parameter and no stored vuid', async () => {
643641
const eventManager = getMockOdpEventManager();
644642
eventManager.onRunning.mockReturnValue(Promise.resolve());
645643

@@ -655,9 +653,58 @@ describe('DefaultOdpManager', () => {
655653
await odpManager.onRunning();
656654

657655
odpManager.identifyUser('vuid_a');
656+
expect(mockSendEvents).not.toHaveBeenCalled();
657+
});
658+
659+
it('sends identified event when called with fs_user_id and vuid is set via setVuid', async () => {
660+
const eventManager = getMockOdpEventManager();
661+
eventManager.onRunning.mockReturnValue(Promise.resolve());
662+
663+
const mockSendEvents = vi.mocked(eventManager.sendEvent as OdpEventManager['sendEvent']);
664+
665+
const odpManager = new DefaultOdpManager({
666+
segmentManager: getMockOdpSegmentManager(),
667+
eventManager,
668+
});
669+
670+
odpManager.start();
671+
odpManager.updateConfig({ integrated: true, odpConfig: config });
672+
await odpManager.onRunning();
673+
674+
// Set vuid first, then identify with fs_user_id
675+
odpManager.setVuid('vuid_123');
676+
677+
// Clear the client_initialized event call
678+
mockSendEvents.mockClear();
679+
680+
odpManager.identifyUser('user');
681+
expect(mockSendEvents).toHaveBeenCalledOnce();
682+
const { type, action, identifiers } = mockSendEvents.mock.calls[0][0];
683+
expect(type).toEqual('fullstack');
684+
expect(action).toEqual('identified');
685+
// identifyUser passes {fs_user_id: 'user'}, and sendEvent augments with stored vuid
686+
expect(identifiers).toEqual(new Map([['fs_user_id', 'user'], ['vuid', 'vuid_123']]));
687+
});
688+
689+
it('sends identified event with three identifiers (fs_user_id, explicit vuid, and stored vuid)', async () => {
690+
const eventManager = getMockOdpEventManager();
691+
eventManager.onRunning.mockReturnValue(Promise.resolve());
692+
693+
const mockSendEvents = vi.mocked(eventManager.sendEvent as OdpEventManager['sendEvent']);
694+
695+
const odpManager = new DefaultOdpManager({
696+
segmentManager: getMockOdpSegmentManager(),
697+
eventManager,
698+
});
699+
700+
odpManager.start();
701+
odpManager.updateConfig({ integrated: true, odpConfig: config });
702+
await odpManager.onRunning();
703+
704+
odpManager.identifyUser('user', 'vuid_a');
658705
expect(mockSendEvents).toHaveBeenCalledOnce();
659706
const { identifiers } = mockSendEvents.mock.calls[0][0];
660-
expect(identifiers).toEqual(new Map([['vuid', 'vuid_a']]));
707+
expect(identifiers).toEqual(new Map([['fs_user_id', 'user'], ['vuid', 'vuid_a']]));
661708
});
662709

663710
it('should reject onRunning() if stopped in new state', async () => {

‎lib/odp/odp_manager.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class DefaultOdpManager extends BaseService implements OdpManager {
212212

213213
identifyUser(userId: string, vuid?: string): void {
214214
const identifiers = new Map<string, string>();
215-
215+
216216
let finalUserId: Maybe<string> = userId;
217217
let finalVuid: Maybe<string> = vuid;
218218

@@ -229,6 +229,21 @@ export class DefaultOdpManager extends BaseService implements OdpManager {
229229
identifiers.set(ODP_USER_KEY.FS_USER_ID, finalUserId);
230230
}
231231

232+
// Include the stored vuid when counting identifiers to determine if there
233+
// are enough to warrant sending an identify event. The vuid would be added
234+
// later in sendEvent(), but we need to account for it here for the count.
235+
const allIdentifiers = new Map(identifiers);
236+
if (!allIdentifiers.has(ODP_USER_KEY.VUID) && this.vuid) {
237+
allIdentifiers.set(ODP_USER_KEY.VUID, this.vuid);
238+
}
239+
240+
// An identify event requires at least 2 identifiers to link (e.g., vuid + fs_user_id).
241+
// A single identifier has no cross-reference value and would generate unnecessary traffic.
242+
if (allIdentifiers.size < 2) {
243+
this.logger?.debug('ODP identify event is not dispatched (only one identifier provided).');
244+
return;
245+
}
246+
232247
const event = new OdpEvent(ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION.IDENTIFIED, identifiers);
233248
this.sendEvent(event);
234249
}

0 commit comments

Comments
 (0)