Skip to content

[FSSDK-11397] replace odp pixel api usage with event api for browser #1026

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 3 commits into from
Apr 15, 2025
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
36 changes: 25 additions & 11 deletions lib/odp/odp_manager_factory.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,9 +28,9 @@ vi.mock('./odp_manager_factory', () => {

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { getOpaqueOdpManager, OdpManagerOptions } from './odp_manager_factory';
import { BROWSER_DEFAULT_API_TIMEOUT, createOdpManager } from './odp_manager_factory.browser';
import { BROWSER_DEFAULT_API_TIMEOUT, BROWSER_DEFAULT_BATCH_SIZE, BROWSER_DEFAULT_FLUSH_INTERVAL, createOdpManager } from './odp_manager_factory.browser';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { pixelApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { eventApiRequestGenerator, pixelApiRequestGenerator } from './event_manager/odp_event_api_manager';

describe('createOdpManager', () => {
const MockBrowserRequestHandler = vi.mocked(BrowserRequestHandler);
Expand Down Expand Up @@ -77,25 +77,39 @@ describe('createOdpManager', () => {
expect(requestHandlerOptions?.timeout).toBe(BROWSER_DEFAULT_API_TIMEOUT);
});

it('should use batchSize 1 if batchSize is not provided', () => {
const odpManager = createOdpManager({});
it('should use the provided eventBatchSize', () => {
const odpManager = createOdpManager({ eventBatchSize: 99 });
expect(odpManager).toBe(mockGetOpaqueOdpManager.mock.results[0].value);
const { eventBatchSize } = mockGetOpaqueOdpManager.mock.calls[0][0];
expect(eventBatchSize).toBe(1);
expect(eventBatchSize).toBe(99);
});

it('should use batchSize 1 event if some other batchSize value is provided', () => {
const odpManager = createOdpManager({ eventBatchSize: 99 });
it('should use the browser default eventBatchSize if none provided', () => {
const odpManager = createOdpManager({});
expect(odpManager).toBe(mockGetOpaqueOdpManager.mock.results[0].value);
const { eventBatchSize } = mockGetOpaqueOdpManager.mock.calls[0][0];
expect(eventBatchSize).toBe(1);
expect(eventBatchSize).toBe(BROWSER_DEFAULT_BATCH_SIZE);
});

it('should use the provided eventFlushInterval', () => {
const odpManager = createOdpManager({ eventFlushInterval: 9999 });
expect(odpManager).toBe(mockGetOpaqueOdpManager.mock.results[0].value);
const { eventFlushInterval } = mockGetOpaqueOdpManager.mock.calls[0][0];
expect(eventFlushInterval).toBe(9999);
});

it('should use the browser default eventFlushInterval if none provided', () => {
const odpManager = createOdpManager({});
expect(odpManager).toBe(mockGetOpaqueOdpManager.mock.results[0].value);
const { eventFlushInterval } = mockGetOpaqueOdpManager.mock.calls[0][0];
expect(eventFlushInterval).toBe(BROWSER_DEFAULT_FLUSH_INTERVAL);
});

it('uses the pixel api request generator', () => {
it('uses the event api request generator', () => {
const odpManager = createOdpManager({ });
expect(odpManager).toBe(mockGetOpaqueOdpManager.mock.results[0].value);
const { eventRequestGenerator } = mockGetOpaqueOdpManager.mock.calls[0][0];
expect(eventRequestGenerator).toBe(pixelApiRequestGenerator);
expect(eventRequestGenerator).toBe(eventApiRequestGenerator);
});

it('uses the passed options for relevant fields', () => {
Expand Down
10 changes: 6 additions & 4 deletions lib/odp/odp_manager_factory.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/

import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { pixelApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { OdpManager } from './odp_manager';
import { eventApiRequestGenerator } from './event_manager/odp_event_api_manager';
import { getOpaqueOdpManager, OdpManagerOptions, OpaqueOdpManager } from './odp_manager_factory';

export const BROWSER_DEFAULT_API_TIMEOUT = 10_000;
export const BROWSER_DEFAULT_BATCH_SIZE = 10;
export const BROWSER_DEFAULT_FLUSH_INTERVAL = 1000;

export const createOdpManager = (options: OdpManagerOptions = {}): OpaqueOdpManager => {
const segmentRequestHandler = new BrowserRequestHandler({
Expand All @@ -32,9 +33,10 @@ export const createOdpManager = (options: OdpManagerOptions = {}): OpaqueOdpMana

return getOpaqueOdpManager({
...options,
eventBatchSize: 1,
eventBatchSize: options.eventBatchSize || BROWSER_DEFAULT_BATCH_SIZE,
eventFlushInterval: options.eventFlushInterval || BROWSER_DEFAULT_FLUSH_INTERVAL,
segmentRequestHandler,
eventRequestHandler,
eventRequestGenerator: pixelApiRequestGenerator,
eventRequestGenerator: eventApiRequestGenerator,
});
};
Loading