Skip to content

Commit

Permalink
[FSSDK-9562] set pixel api endpoint dynamically (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
raju-opti committed Aug 17, 2023
1 parent 91826b0 commit 36ff5d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import configValidator from './utils/config_validator';
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
import OptimizelyUserContext from './optimizely_user_context';

import { LOG_MESSAGES, ODP_EVENT_ACTION, ODP_EVENT_BROWSER_ENDPOINT } from './utils/enums';
import { LOG_MESSAGES, ODP_EVENT_ACTION } from './utils/enums';
import { BrowserOdpManager } from './plugins/odp_manager/index.browser';
import { OdpConfig } from './core/odp/odp_config';
import { BrowserOdpEventManager } from './plugins/odp/event_manager/index.browser';
Expand Down Expand Up @@ -1031,6 +1031,7 @@ describe('javascript-sdk (Browser)', function() {

it('should send an odp event to the browser endpoint', async () => {
const odpConfig = new OdpConfig();

const apiManager = new BrowserOdpEventApiManager(mockRequestHandler, logger);
const eventManager = new BrowserOdpEventManager({
odpConfig,
Expand Down Expand Up @@ -1065,8 +1066,9 @@ describe('javascript-sdk (Browser)', function() {

let publicKey = datafile.integrations[0].publicKey;

const pixelApiEndpoint = 'https://jumbe.zaius.com/v2/zaius.gif';
let requestEndpoint = new URL(requestParams.get('endpoint'));
assert.equal(requestEndpoint.origin + requestEndpoint.pathname, ODP_EVENT_BROWSER_ENDPOINT);
assert.equal(requestEndpoint.origin + requestEndpoint.pathname, pixelApiEndpoint);
assert.equal(requestParams.get('method'), 'GET');

let searchParams = requestEndpoint.searchParams;
Expand Down
20 changes: 18 additions & 2 deletions lib/plugins/odp/event_api_manager/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { OdpEvent } from '../../../core/odp/odp_event';
import { OdpEventApiManager } from '../../../core/odp/odp_event_api_manager';
import { LogLevel } from '../../../modules/logging';
import { ODP_EVENT_BROWSER_ENDPOINT } from '../../../utils/enums';
import { ODP_CONFIG_NOT_READY_MESSAGE } from '../../../core/odp/odp_event_api_manager';

const EVENT_SENDING_FAILURE_MESSAGE = 'ODP event send failed';

const pixelApiPath = 'v2/zaius.gif';

export class BrowserOdpEventApiManager extends OdpEventApiManager {
protected shouldSendEvents(events: OdpEvent[]): boolean {
if (events.length <= 1) {
Expand All @@ -15,6 +16,15 @@ export class BrowserOdpEventApiManager extends OdpEventApiManager {
return false;
}

private getPixelApiEndpoint(): string {
if (!this.odpConfig?.isReady()) {
throw new Error(ODP_CONFIG_NOT_READY_MESSAGE);
}
const apiHost = this.odpConfig.apiHost;
const pixelApiEndpoint = new URL(pixelApiPath, apiHost.replace('api', 'jumbe')).href;
return pixelApiEndpoint;
}

protected generateRequestData(
events: OdpEvent[]
): { method: string; endpoint: string; headers: { [key: string]: string }; data: string } {
Expand All @@ -23,11 +33,17 @@ export class BrowserOdpEventApiManager extends OdpEventApiManager {
this.getLogger().log(LogLevel.ERROR, ODP_CONFIG_NOT_READY_MESSAGE);
throw new Error(ODP_CONFIG_NOT_READY_MESSAGE);
}

// this cannot be cached cause OdpConfig is mutable
// and can be updated in place and it is done so in odp
// manager. We should make OdpConfig immutable and
// refacator later
const pixelApiEndpoint = this.getPixelApiEndpoint();

const apiKey = this.odpConfig.apiKey;
const method = 'GET';
const event = events[0];
const url = new URL(ODP_EVENT_BROWSER_ENDPOINT);
const url = new URL(pixelApiEndpoint);
event.identifiers.forEach((v, k) => {
url.searchParams.append(k, v);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/odp_manager/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class NodeOdpManager extends OdpManager {
});
}

this.eventManager!.start();
this.eventManager.start();

this.initPromise = Promise.resolve();
}
Expand Down
1 change: 0 additions & 1 deletion lib/utils/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ export enum ODP_USER_KEY {
export const FS_USER_ID_ALIAS = 'fs-user-id';

export const ODP_DEFAULT_EVENT_TYPE = 'fullstack';
export const ODP_EVENT_BROWSER_ENDPOINT = 'https://jumbe.zaius.com/v2/zaius.gif';

/**
* ODP Event Action Options
Expand Down

0 comments on commit 36ff5d7

Please sign in to comment.