diff --git a/lib/core/odp/odp_event_manager.ts b/lib/core/odp/odp_event_manager.ts index 3b91d7712..627cf0a27 100644 --- a/lib/core/odp/odp_event_manager.ts +++ b/lib/core/odp/odp_event_manager.ts @@ -46,7 +46,7 @@ export interface IOdpEventManager { stop(): Promise; - registerVuid(vuid: string): void; + sendInitializedEvent(vuid: string): void; identifyUser(userId?: string, vuid?: string): void; @@ -251,7 +251,7 @@ export abstract class OdpEventManager implements IOdpEventManager { * Register a new visitor user id (VUID) in ODP * @param vuid Visitor User ID to send */ - registerVuid(vuid: string): void { + sendInitializedEvent(vuid: string): void { const identifiers = new Map(); identifiers.set(ODP_USER_KEY.VUID, vuid); diff --git a/lib/core/odp/odp_manager.ts b/lib/core/odp/odp_manager.ts index 2c22acd94..e57909d31 100644 --- a/lib/core/odp/odp_manager.ts +++ b/lib/core/odp/odp_manager.ts @@ -46,7 +46,7 @@ export interface IOdpManager { sendEvent({ type, action, identifiers, data }: OdpEvent): void; - registerVuid(vuid: string): void; + setVuid(vuid: string): void; } export enum Status { @@ -94,6 +94,8 @@ export abstract class OdpManager implements IOdpManager { */ protected odpIntegrationConfig?: OdpIntegrationConfig; + protected vuid?: string; + constructor({ odpIntegrationConfig, segmentManager, @@ -123,7 +125,24 @@ export abstract class OdpManager implements IOdpManager { } } - abstract registerVuid(vuid: string): void; + setVuid(vuid: string): void { + if (!this.odpIntegrationConfig) { + this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE); + return; + } + + if (!this.odpIntegrationConfig.integrated) { + this.logger.log(LogLevel.INFO, ERROR_MESSAGES.ODP_NOT_INTEGRATED); + return; + } + + try { + this.vuid = vuid; + this.eventManager.sendInitializedEvent(vuid); + } catch (e) { + this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED); + } + } getStatus(): Status { return this.status; @@ -271,6 +290,11 @@ export abstract class OdpManager implements IOdpManager { throw new Error('ODP action is not valid (cannot be empty).'); } + if (this.vuid) { + identifiers = new Map(identifiers); + identifiers.set(ODP_USER_KEY.VUID, this.vuid); + } + this.eventManager.sendEvent(new OdpEvent(mType, action, identifiers, data)); } } diff --git a/lib/optimizely/index.ts b/lib/optimizely/index.ts index 3271a23f4..5cd78be63 100644 --- a/lib/optimizely/index.ts +++ b/lib/optimizely/index.ts @@ -194,7 +194,7 @@ export default class Optimizely implements Client { if (success) { const vuid = this.vuidManager?.vuid; if (vuid) { - this.odpManager?.registerVuid(vuid); + this.odpManager?.setVuid(vuid); } } }); diff --git a/lib/plugins/odp_manager/index.browser.ts b/lib/plugins/odp_manager/index.browser.ts index 909feb933..c04f27c4b 100644 --- a/lib/plugins/odp_manager/index.browser.ts +++ b/lib/plugins/odp_manager/index.browser.ts @@ -152,22 +152,4 @@ export class BrowserOdpManager extends OdpManager { super.identifyUser(fsUserId, vuid); } - - registerVuid(vuid: string): void { - if (!this.odpIntegrationConfig) { - this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_CONFIG_NOT_AVAILABLE); - return; - } - - if (!this.odpIntegrationConfig.integrated) { - this.logger.log(LogLevel.INFO, ERROR_MESSAGES.ODP_NOT_INTEGRATED); - return; - } - - try { - this.eventManager.registerVuid(vuid); - } catch (e) { - this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED); - } - } } diff --git a/lib/plugins/odp_manager/index.node.ts b/lib/plugins/odp_manager/index.node.ts index cefa22d89..181b5d546 100644 --- a/lib/plugins/odp_manager/index.node.ts +++ b/lib/plugins/odp_manager/index.node.ts @@ -133,8 +133,4 @@ export class NodeOdpManager extends OdpManager { logger, }); } - - registerVuid(vuid: string): void { - this.logger.log(LogLevel.ERROR, `Unable to registerVuid ${vuid} in a node server context`); - } } diff --git a/tests/odpEventManager.spec.ts b/tests/odpEventManager.spec.ts index 31bc7a753..395d5650f 100644 --- a/tests/odpEventManager.spec.ts +++ b/tests/odpEventManager.spec.ts @@ -634,7 +634,7 @@ describe('OdpEventManager', () => { const fsUserId = 'test-fs-user-id'; eventManager.start(); - eventManager.registerVuid(vuid); + eventManager.sendInitializedEvent(vuid); jest.advanceTimersByTime(250); diff --git a/tests/odpManager.spec.ts b/tests/odpManager.spec.ts index c4c64133f..67c1f0ddc 100644 --- a/tests/odpManager.spec.ts +++ b/tests/odpManager.spec.ts @@ -73,9 +73,6 @@ const testOdpManager = ({ protected initializeVuid(): Promise { return vuidInitializer?.() ?? Promise.resolve(); } - registerVuid(vuid: string | undefined): void { - throw new Error('Method not implemented.' + vuid || ''); - } } return new TestOdpManager(); } @@ -493,6 +490,40 @@ describe('OdpManager', () => { expect(event2.identifiers).toEqual(identifiers); }); + it('should add the available vuid to sendEvent identifies', async () => { + const odpIntegrationConfig: OdpIntegratedConfig = { + integrated: true, + odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA) + }; + + const odpManager = testOdpManager({ + odpIntegrationConfig, + segmentManager, + eventManager, + logger, + vuidEnabled: true, + }); + + await odpManager.onReady(); + odpManager.setVuid('vuid_test'); + + const identifiers = new Map([['email', 'a@b.com']]); + const data = new Map([['key1', 'value1'], ['key2', 'value2']]); + + odpManager.sendEvent({ + action: 'action', + type: 'type', + identifiers, + data, + }); + + const [event] = capture(mockEventManager.sendEvent).byCallIndex(0); + expect(event.action).toEqual('action'); + expect(event.type).toEqual('type'); + expect(event.identifiers.get(ODP_USER_KEY.VUID)).toEqual('vuid_test'); + expect(event.data).toEqual(data); + }); + it('should throw an error if event action is empty string and not call eventManager', async () => { const odpIntegrationConfig: OdpIntegratedConfig = {