Skip to content
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

[FSSDK-9562] set pixel api endpoint dynamically #852

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
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';
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved

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;
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
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!);
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
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