Skip to content

[FSSDK-10797] send vuid automatically with odp events #951

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

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions lib/core/odp/odp_event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface IOdpEventManager {

stop(): Promise<void>;

registerVuid(vuid: string): void;
sendInitializedEvent(vuid: string): void;

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

Expand Down Expand Up @@ -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<string, string>();
identifiers.set(ODP_USER_KEY.VUID, vuid);

Expand Down
28 changes: 26 additions & 2 deletions lib/core/odp/odp_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -94,6 +94,8 @@ export abstract class OdpManager implements IOdpManager {
*/
protected odpIntegrationConfig?: OdpIntegrationConfig;

protected vuid?: string;

constructor({
odpIntegrationConfig,
segmentManager,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion lib/optimizely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
Expand Down
18 changes: 0 additions & 18 deletions lib/plugins/odp_manager/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
4 changes: 0 additions & 4 deletions lib/plugins/odp_manager/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
}
2 changes: 1 addition & 1 deletion tests/odpEventManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ describe('OdpEventManager', () => {
const fsUserId = 'test-fs-user-id';

eventManager.start();
eventManager.registerVuid(vuid);
eventManager.sendInitializedEvent(vuid);

jest.advanceTimersByTime(250);

Expand Down
37 changes: 34 additions & 3 deletions tests/odpManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ const testOdpManager = ({
protected initializeVuid(): Promise<void> {
return vuidInitializer?.() ?? Promise.resolve();
}
registerVuid(vuid: string | undefined): void {
throw new Error('Method not implemented.' + vuid || '');
}
}
return new TestOdpManager();
}
Expand Down Expand Up @@ -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', '[email protected]']]);
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 = {
Expand Down
Loading