Skip to content

Commit 5a413d3

Browse files
authored
[FSSDK-10797] send vuid automatically with odp events (#951)
1 parent 40d1ff4 commit 5a413d3

File tree

7 files changed

+64
-31
lines changed

7 files changed

+64
-31
lines changed

lib/core/odp/odp_event_manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface IOdpEventManager {
4646

4747
stop(): Promise<void>;
4848

49-
registerVuid(vuid: string): void;
49+
sendInitializedEvent(vuid: string): void;
5050

5151
identifyUser(userId?: string, vuid?: string): void;
5252

@@ -251,7 +251,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
251251
* Register a new visitor user id (VUID) in ODP
252252
* @param vuid Visitor User ID to send
253253
*/
254-
registerVuid(vuid: string): void {
254+
sendInitializedEvent(vuid: string): void {
255255
const identifiers = new Map<string, string>();
256256
identifiers.set(ODP_USER_KEY.VUID, vuid);
257257

lib/core/odp/odp_manager.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface IOdpManager {
4646

4747
sendEvent({ type, action, identifiers, data }: OdpEvent): void;
4848

49-
registerVuid(vuid: string): void;
49+
setVuid(vuid: string): void;
5050
}
5151

5252
export enum Status {
@@ -94,6 +94,8 @@ export abstract class OdpManager implements IOdpManager {
9494
*/
9595
protected odpIntegrationConfig?: OdpIntegrationConfig;
9696

97+
protected vuid?: string;
98+
9799
constructor({
98100
odpIntegrationConfig,
99101
segmentManager,
@@ -123,7 +125,24 @@ export abstract class OdpManager implements IOdpManager {
123125
}
124126
}
125127

126-
abstract registerVuid(vuid: string): void;
128+
setVuid(vuid: string): void {
129+
if (!this.odpIntegrationConfig) {
130+
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE);
131+
return;
132+
}
133+
134+
if (!this.odpIntegrationConfig.integrated) {
135+
this.logger.log(LogLevel.INFO, ERROR_MESSAGES.ODP_NOT_INTEGRATED);
136+
return;
137+
}
138+
139+
try {
140+
this.vuid = vuid;
141+
this.eventManager.sendInitializedEvent(vuid);
142+
} catch (e) {
143+
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED);
144+
}
145+
}
127146

128147
getStatus(): Status {
129148
return this.status;
@@ -271,6 +290,11 @@ export abstract class OdpManager implements IOdpManager {
271290
throw new Error('ODP action is not valid (cannot be empty).');
272291
}
273292

293+
if (this.vuid) {
294+
identifiers = new Map(identifiers);
295+
identifiers.set(ODP_USER_KEY.VUID, this.vuid);
296+
}
297+
274298
this.eventManager.sendEvent(new OdpEvent(mType, action, identifiers, data));
275299
}
276300
}

lib/optimizely/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export default class Optimizely implements Client {
194194
if (success) {
195195
const vuid = this.vuidManager?.vuid;
196196
if (vuid) {
197-
this.odpManager?.registerVuid(vuid);
197+
this.odpManager?.setVuid(vuid);
198198
}
199199
}
200200
});

lib/plugins/odp_manager/index.browser.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,4 @@ export class BrowserOdpManager extends OdpManager {
152152

153153
super.identifyUser(fsUserId, vuid);
154154
}
155-
156-
registerVuid(vuid: string): void {
157-
if (!this.odpIntegrationConfig) {
158-
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE);
159-
return;
160-
}
161-
162-
if (!this.odpIntegrationConfig.integrated) {
163-
this.logger.log(LogLevel.INFO, ERROR_MESSAGES.ODP_NOT_INTEGRATED);
164-
return;
165-
}
166-
167-
try {
168-
this.eventManager.registerVuid(vuid);
169-
} catch (e) {
170-
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED);
171-
}
172-
}
173155
}

lib/plugins/odp_manager/index.node.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,4 @@ export class NodeOdpManager extends OdpManager {
133133
logger,
134134
});
135135
}
136-
137-
registerVuid(vuid: string): void {
138-
this.logger.log(LogLevel.ERROR, `Unable to registerVuid ${vuid} in a node server context`);
139-
}
140136
}

tests/odpEventManager.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ describe('OdpEventManager', () => {
634634
const fsUserId = 'test-fs-user-id';
635635

636636
eventManager.start();
637-
eventManager.registerVuid(vuid);
637+
eventManager.sendInitializedEvent(vuid);
638638

639639
jest.advanceTimersByTime(250);
640640

tests/odpManager.spec.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ const testOdpManager = ({
7373
protected initializeVuid(): Promise<void> {
7474
return vuidInitializer?.() ?? Promise.resolve();
7575
}
76-
registerVuid(vuid: string | undefined): void {
77-
throw new Error('Method not implemented.' + vuid || '');
78-
}
7976
}
8077
return new TestOdpManager();
8178
}
@@ -493,6 +490,40 @@ describe('OdpManager', () => {
493490
expect(event2.identifiers).toEqual(identifiers);
494491
});
495492

493+
it('should add the available vuid to sendEvent identifies', async () => {
494+
const odpIntegrationConfig: OdpIntegratedConfig = {
495+
integrated: true,
496+
odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
497+
};
498+
499+
const odpManager = testOdpManager({
500+
odpIntegrationConfig,
501+
segmentManager,
502+
eventManager,
503+
logger,
504+
vuidEnabled: true,
505+
});
506+
507+
await odpManager.onReady();
508+
odpManager.setVuid('vuid_test');
509+
510+
const identifiers = new Map([['email', 'a@b.com']]);
511+
const data = new Map([['key1', 'value1'], ['key2', 'value2']]);
512+
513+
odpManager.sendEvent({
514+
action: 'action',
515+
type: 'type',
516+
identifiers,
517+
data,
518+
});
519+
520+
const [event] = capture(mockEventManager.sendEvent).byCallIndex(0);
521+
expect(event.action).toEqual('action');
522+
expect(event.type).toEqual('type');
523+
expect(event.identifiers.get(ODP_USER_KEY.VUID)).toEqual('vuid_test');
524+
expect(event.data).toEqual(data);
525+
});
526+
496527

497528
it('should throw an error if event action is empty string and not call eventManager', async () => {
498529
const odpIntegrationConfig: OdpIntegratedConfig = {

0 commit comments

Comments
 (0)