8000 [FSSDK-9607] add pixelUrl to OdpConfig (#888) · chrisvaf/javascript-sdk@f61dc52 · GitHub
[go: up one dir, main page]

Skip to content

Commit f61dc52

Browse files
[FSSDK-9607] add pixelUrl to OdpConfig (optimizely#888)
1 parent 7e03647 commit f61dc52

15 files changed

+93
-38
lines changed

lib/core/odp/odp_config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ export class OdpConfig {
4545
return this._apiKey;
4646
}
4747

48+
/**
49+
* Url for sending events via pixel.
50+
* @private
51+
*/
52+
private _pixelUrl: string;
53+
54+
/**
55+
* Getter to retrieve the ODP pixel URL
56+
* @public
57+
*/
58+
get pixelUrl(): string {
59+
return this._pixelUrl;
60+
}
61+
4862
/**
4963
* All ODP segments used in the current datafile (associated with apiHost/apiKey).
5064
* @private
@@ -59,9 +73,10 @@ export class OdpConfig {
5973
return this._segmentsToCheck;
6074
}
6175

62-
constructor(apiKey?: string, apiHost?: string, segmentsToCheck?: string[]) {
76+
constructor(apiKey?: string, apiHost?: string, pixelUrl?: string, segmentsToCheck?: string[]) {
6377
this._apiKey = apiKey ?? '';
6478
this._apiHost = apiHost ?? '';
79+
this._pixelUrl = pixelUrl ?? '';
6580
this._segmentsToCheck = segmentsToCheck ?? [];
6681
}
6782

@@ -76,6 +91,7 @@ export class OdpConfig {
7691
} else {
7792
if (config.apiKey) this._apiKey = config.apiKey;
7893
if (config.apiHost) this._apiHost = config.apiHost;
94+
if (config.pixelUrl) this._pixelUrl = config.pixelUrl;
7995
if (config.segmentsToCheck) this._segmentsToCheck = config.segmentsToCheck;
8096

8197
return true;
@@ -98,6 +114,7 @@ export class OdpConfig {
98114
return (
99115
this._apiHost === configToCompare._apiHost &&
100116
this._apiKey === configToCompare._apiKey &&
117+
this._pixelUrl === configToCompare._pixelUrl &&
101118
checkArrayEquality(this.segmentsToCheck, configToCompare._segmentsToCheck)
102119
);
103120
}

lib/core/odp/odp_manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface IOdpManager {
4040

4141
eventManager: IOdpEventManager | undefined;
4242

43-
updateSettings({ apiKey, apiHost, segmentsToCheck }: OdpConfig): boolean;
43+
updateSettings({ apiKey, apiHost, pixelUrl, segmentsToCheck }: OdpConfig): boolean;
4444

4545
close(): void;
4646

@@ -97,7 +97,7 @@ export abstract class OdpManager implements IOdpManager {
9797
/**
9898
* Provides a method to update ODP Manager's ODP Config API Key, API Host, and Audience Segments
9999
*/
100-
updateSettings({ apiKey, apiHost, segmentsToCheck }: OdpConfig): boolean {
100+
updateSettings({ apiKey, apiHost, pixelUrl, segmentsToCheck }: OdpConfig): boolean {
101101
if (!this.enabled) {
102102
return false;
103103
}
@@ -114,7 +114,7 @@ export abstract class OdpManager implements IOdpManager {
114114

115115
this.eventManager.flush();
116116

117-
const newConfig = new OdpConfig(apiKey, apiHost, segmentsToCheck);
117+
const newConfig = new OdpConfig(apiKey, apiHost, pixelUrl, segmentsToCheck);
118118
const configDidUpdate = this.odpConfig.update(newConfig);
119119

120120
if (configDidUpdate) {

lib/core/project_config/index.tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,10 @@ describe('lib/core/project_config', function() {
821821
assert.exists(config.hostForOdp);
822822
});
823823

824+
it('should populate the pixelUrl value from the odp integration', () => {
825+
assert.exists(config.pixelUrlForOdp);
826+
});
827+
824828
it('should contain all expected unique odp segments in allSegments', () => {
825829
assert.equal(config.allSegments.length, 3);
826830
assert.deepEqual(config.allSegments, ['odp-segment-1', 'odp-segment-2', 'odp-segment-3']);
@@ -848,6 +852,11 @@ describe('lib/core/project_config', function() {
848852
assert.equal(config.hostForOdp, 'https://api.zaius.com');
849853
});
850854

855+
it('should populate the pixelUrl value from the odp integration', () => {
856+
assert.exists(config.pixelUrlForOdp);
857+
assert.equal(config.pixelUrlForOdp, 'https://jumbe.zaius.com');
858+
});
859+
851860
it('should contain all expected unique odp segments in all segments', () => {
852861
assert.equal(config.allSegments.length, 0);
853862
});

lib/core/project_config/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export interface ProjectConfig {
9292
integrationKeyMap?: { [key: string]: Integration };
9393
publicKeyForOdp?: string;
9494
hostForOdp?: string;
95+
pixelUrlForOdp?: string;
9596
allSegments: string[];
9697
}
9798

@@ -203,6 +204,10 @@ export const createProjectConfig = function(datafileObj?: JSON, datafileStr: str
203204
if (integration.host && !projectConfig.hostForOdp) {
204205
projectConfig.hostForOdp = integration.host;
205206
}
207+
208+
if (integration.pixelUrl && !projectConfig.pixelUrlForOdp) {
209+
projectConfig.pixelUrlForOdp = integration.pixelUrl;
210+
}
206211
}
207212
});
208213
}

lib/core/project_config/project_config_schema.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,13 @@ var schemaDefinition = {
290290
},
291291
publicKey: {
292292
type: 'string'
293-
}
294-
}
295-
}
296-
}
293+
},
294+
pixelUrl: {
295+
type: 'string'
296+
},
297+
},
298+
},
299+
},
297300
},
298301
};
299302

lib/index.browser.tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ describe('javascript-sdk (Browser)', function() {
879879

880880
client.sendOdpEvent('test', '', new Map([['eamil', 'test@test.test']]), new Map([['key', 'value']]));
881881
clock.tick(10000);
882-
882+
883883
const eventRequestUrl = new URL(fakeRequestHandler.makeRequest.lastCall.args[0]);
884884
const searchParams = eventRequestUrl.searchParams;
885885

@@ -1116,8 +1116,9 @@ describe('javascript-sdk (Browser)', function() {
11161116
clock.tick(100);
11171117

11181118
let publicKey = datafile.integrations[0].publicKey;
1119+
let pixelUrl = datafile.integrations[0].pixelUrl;
11191120

1120-
const pixelApiEndpoint = 'https://jumbe.zaius.com/v2/zaius.gif';
1121+
const pixelApiEndpoint = `${pixelUrl}/v2/zaius.gif`;
11211122
let requestEndpoint = new URL(requestParams.get('endpoint'));
11221123
assert.equal(requestEndpoint.origin + requestEndpoint.pathname, pixelApiEndpoint);
11231124
assert.equal(requestParams.get('method'), 'GET');

lib/optimizely/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class Optimizely implements Client {
148148
NotificationRegistry.getNotificationCenter(config.sdkKey)?.sendNotifications(
149149
NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE
150150
);
151-
151+
152152
this.updateOdpSettings();
153153
});
154154

@@ -186,7 +186,7 @@ export default class Optimizely implements Client {
186186
if (config.odpManager?.initPromise) {
187187
dependentPromises.push(config.odpManager.initPromise);
188188
}
189-
189+
190190
this.readyPromise = Promise.all(dependentPromises).then(promiseResults => {
191191
// If no odpManager exists yet, creates a new one
192192
if (config.odpManager != null) {
@@ -1678,13 +1678,13 @@ export default class Optimizely implements Client {
16781678
}
16791679

16801680
/**
1681-
* Updates ODP Config with most recent ODP key, host, and segments from the project config
1681+
* Updates ODP Config with most recent ODP key, host, pixelUrl, and segments from the project config
16821682
*/
16831683
private updateOdpSettings(): void {
16841684
const projectConfig = this.projectConfigManager.getConfig();
16851685
if (this.odpManager != null && projectConfig != null) {
16861686
this.odpManager.updateSettings(
1687-
new OdpConfig(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.allSegments)
1687+
new OdpConfig(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.pixelUrlForOdp, projectConfig.allSegments)
16881688
);
16891689
}
16901690
}

lib/plugins/odp/event_api_manager/index.browser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class BrowserOdpEventApiManager extends OdpEventApiManager {
2020
if (!this.odpConfig?.isReady()) {
2121
throw new Error(ODP_CONFIG_NOT_READY_MESSAGE);
2222
}
23-
const apiHost = this.odpConfig.apiHost;
24-
const pixelApiEndpoint = new URL(pixelApiPath, apiHost.replace('api', 'jumbe')).href;
23+
const pixelUrl = this.odpConfig.pixelUrl;
24+
const pixelApiEndpoint = new URL(pixelApiPath, pixelUrl).href;
2525
return pixelApiEndpoint;
2626
}
2727

@@ -33,7 +33,7 @@ export class BrowserOdpEventApiManager extends OdpEventApiManager {
3333
this.getLogger().log(LogLevel.ERROR, ODP_CONFIG_NOT_READY_MESSAGE);
3434
throw new Error(ODP_CONFIG_NOT_READY_MESSAGE);
3535
}
36-
36+
3737
// this cannot be cached cause OdpConfig is mutable
3838
// and can be updated in place and it is done so in odp
3939
// manager. We should make OdpConfig immutable and

lib/shared_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export interface Integration {
221221
key: string;
222222
host?: string;
223223
publicKey?: string;
224+
pixelUrl?: string;
224225
}
225226

226227
export interface TrafficAllocation {

lib/tests/test_data.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,11 +3225,13 @@ var odpIntegratedConfigWithSegments = {
32253225
key: 'odp',
32263226
host: 'https://api.zaius.com',
32273227
publicKey: 'W4WzcEs-ABgXorzY7h1LCQ',
3228+
pixelUrl: 'https://jumbe.zaius.com',
32283229
},
32293230
{
32303231
key: 'odp',
32313232
host: 'https://api.zzzzaius.com',
32323233
publicKey: 'W4WzcEs-ABgXorzssssY7h1LCQ',
3234+
pixelUrl: 'https://jumbe.zzzzaius.com',
32333235
},
32343236
{
32353237
key: 'odp',
@@ -3359,6 +3361,7 @@ var odpIntegratedConfigWithoutSegments = {
33593361
key: 'odp',
33603362
host: 'https://api.zaius.com',
33613363
publicKey: 'W4WzcEs-ABgXorzY7h1LCQ',
3364+
pixelUrl: 'https://jumbe.zaius.com',
33623365
},
33633366
{
33643367
key: 'odp',
@@ -3394,6 +3397,7 @@ var odpIntegratedConfigWithoutKey = {
33943397
{
33953398
host: 'https://api.zaius.com',
33963399
publicKey: 'W4WzcEs-ABgXorzY7h1LCQ',
3400+
pixelUrl: 'https://jumbe.zaius.com',
33973401
},
33983402
],
33993403
revision: '100',

tests/odpEventApiManager.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ const ODP_EVENTS = [
3737

3838
const API_KEY = 'test-api-key';
3939
const API_HOST = 'https://odp.example.com';
40+
const PIXEL_URL = 'https://odp.pixel.com';
4041

41-
const odpConfig = new OdpConfig(API_KEY, API_HOST, []);
42+
const odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
4243

4344
describe('NodeOdpEventApiManager', () => {
4445
let mockLogger: LogHandler;
@@ -133,9 +134,10 @@ describe('NodeOdpEventApiManager', () => {
133134
const updatedOdpConfig = new OdpConfig(
134135
'updated-key',
135136
'https://updatedhost.test',
137+
'https://updatedpixel.test',
136138
['updated-seg'],
137139
)
138-
140+
139141
manager.updateSettings(updatedOdpConfig);
140142
await manager.sendEvents(ODP_EVENTS);
141143

tests/odpEventManager.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { UserAgentInfo } from '../lib/core/odp/user_agent_info';
2828

2929
const API_KEY = 'test-api-key';
3030
const API_HOST = 'https://odp.example.com';
31+
const PIXEL_URL = 'https://odp.pixel.com';
3132
const MOCK_IDEMPOTENCE_ID = 'c1dc758e-f095-4f09-9b49-172d74c53880';
3233
const EVENTS: OdpEvent[] = [
3334
new OdpEvent(
@@ -145,7 +146,7 @@ describe('OdpEventManager', () => {
145146
beforeAll(() => {
146147
mockLogger = mock<LogHandler>();
147148
mockApiManager = mock<IOdpEventApiManager>();
148-
odpConfig = new OdpConfig(API_KEY, API_HOST, []);
149+
odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
149150
logger = instance(mockLogger);
150151
apiManager = instance(mockApiManager);
151152
});
@@ -159,7 +160,7 @@ describe('OdpEventManager', () => {
159160
when(mockApiManager.sendEvents(anything())).thenResolve(false);
160161
when(mockApiManager.updateSettings(anything())).thenReturn(undefined);
161162

162-
const apiManager = instance(mockApiManager);
163+
const apiManager = instance(mockApiManager);
163164

164165
const eventManager = new OdpEventManager({
165166
odpConfig,
@@ -172,12 +173,12 @@ describe('OdpEventManager', () => {
172173
const [passedConfig] = capture(mockApiManager.updateSettings).last();
173174
expect(passedConfig).toEqual(odpConfig);
174175
});
175-
176+
176177
it('should update api manager setting with updatetd odp config on updateSettings', () => {
177178
when(mockApiManager.sendEvents(anything())).thenResolve(false);
178179
when(mockApiManager.updateSettings(anything())).thenReturn(undefined);
179180

180-
const apiManager = instance(mockApiManager);
181+
const apiManager = instance(mockApiManager);
181182

182183
const eventManager = new OdpEventManager({
183184
odpConfig,
@@ -190,9 +191,10 @@ describe('OdpEventManager', () => {
190191
const updatedOdpConfig = new OdpConfig(
191192
'updated-key',
192193
'https://updatedhost.test',
194+
'https://pixel.test',
193195
['updated-seg'],
194196
)
195-
197+
196198
eventManager.updateSettings(updatedOdpConfig);
197199

198200
verify(mockApiManager.updateSettings(anything())).twice();
@@ -541,13 +543,15 @@ describe('OdpEventManager', () => {
541543
});
542544
const apiKey = 'testing-api-key';
543545
const apiHost = 'https://some.other.example.com';
546+
const pixelUrl = 'https://some.other.pixel.com';
544547
const segmentsToCheck = ['empty-cart', '1-item-cart'];
545-
const differentOdpConfig = new OdpConfig(apiKey, apiHost, segmentsToCheck);
548+
const differentOdpConfig = new OdpConfig(apiKey, apiHost, pixelUrl, segmentsToCheck);
546549

547550
eventManager.updateSettings(differentOdpConfig);
548551

549552
expect(eventManager['odpConfig'].apiKey).toEqual(apiKey);
550553
expect(eventManager['odpConfig'].apiHost).toEqual(apiHost);
554+
expect(eventManager['odpConfig'].pixelUrl).toEqual(pixelUrl);
551555
expect(eventManager['odpConfig'].segmentsToCheck).toContain(Array.from(segmentsToCheck)[0]);
552556
expect(eventManager['odpConfig'].segmentsToCheck).toContain(Array.from(segmentsToCheck)[1]);
553557
});

tests/odpManager.browser.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ import { OdpEvent } from '../lib/core/odp/odp_event';
3838

3939
const keyA = 'key-a';
4040
const hostA = 'host-a';
41+
const pixelA = 'pixel-a';
4142
const segmentsA = ['a'];
4243
const userA = 'fs-user-a';
4344
const vuidA = 'vuid_a';
44-
const odpConfigA = new OdpConfig(keyA, hostA, segmentsA);
45+
const odpConfigA = new OdpConfig(keyA, hostA, pixelA, segmentsA);
4546

4647
const keyB = 'key-b';
4748
const hostB = 'host-b';
49+
const pixelB = 'pixel-b';
4850
const segmentsB = ['b'];
4951
const userB = 'fs-user-b';
5052
const vuidB = 'vuid_b';
51-
const odpConfigB = new OdpConfig(keyB, hostB, segmentsB);
53+
const odpConfigB = new OdpConfig(keyB, hostB, pixelB, segmentsB);
5254

5355
describe('OdpManager', () => {
5456
let odpConfig: OdpConfig;
@@ -122,7 +124,7 @@ describe('OdpManager', () => {
122124

123125
verify(mockLogger.log(LogLevel.INFO, LOG_MESSAGES.ODP_DISABLED)).once();
124126

125-
browserOdpManager.updateSettings(new OdpConfig('valid', 'host', []));
127+
browserOdpManager.updateSettings(new OdpConfig('valid', 'host', 'pixel-url', []));
126128
expect(browserOdpManager.odpConfig).toBeUndefined;
127129

128130
await browserOdpManager.fetchQualifiedSegments('vuid_user1', []);
@@ -201,7 +203,7 @@ describe('OdpManager', () => {
201203
},
202204
});
203205

204-
const didUpdateA = browserOdpManager.updateSettings(new OdpConfig(keyA, hostA, segmentsA));
206+
const didUpdateA = browserOdpManager.updateSettings(new OdpConfig(keyA, hostA, pixelA, segmentsA));
205207
expect(didUpdateA).toBe(true);
206208

207209
browserOdpManager.fetchQualifiedSegments(vuidA);
@@ -213,7 +215,7 @@ describe('OdpManager', () => {
213215
const fetchQualifiedSegmentsArgsA = capture(mockSegmentApiManager.fetchSegments).last();
214216
expect(fetchQualifiedSegmentsArgsA).toStrictEqual([keyA, hostA, ODP_USER_KEY.VUID, vuidA, segmentsA]);
215217

216-
const didUpdateB = browserOdpManager.updateSettings(new OdpConfig(keyB, hostB, segmentsB));
218+
const didUpdateB = browserOdpManager.updateSettings(new OdpConfig(keyB, hostB, pixelB, segmentsB));
217219
expect(didUpdateB).toBe(true);
218220

219221
browserOdpManager.fetchQualifiedSegments(vuidB);

0 commit comments

Comments
 (0)
0