Skip to content

[FSSDK-11529] Impression event logic adjustment for holdouts #1076

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 19 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6412e89
decision service holdout implementation
junaed-optimizely Jul 24, 2025
3a90a81
[FSSDK-11528] audience condition improvement
junaed-optimizely Jul 25, 2025
f6c4cda
[FSSDK-11528] decision service tests added
junaed-optimizely Jul 25, 2025
9a77240
[FSSDK-11528] decision service tests improvement
junaed-optimizely Jul 25, 2025
b8eac89
[FSSDK-11528] test improvement + decision service logic adjustment
junaed-optimizely Jul 28, 2025
4c88648
[FSSDK-11528] feature toggle readibility improvement
junaed-optimizely Jul 28, 2025
aa3381e
[FSSDK-11528] type def update
junaed-optimizely Jul 28, 2025
0d034b2
[FSSDK-11528] test improvement
junaed-optimizely Jul 28, 2025
78eaa41
[FSSDK-11528] remove only
junaed-optimizely Jul 28, 2025
de3baaa
[FSSDK-11529] condition adjustment
junaed-optimizely Jul 29, 2025
8e33597
Revert "[FSSDK-11529] condition adjustment"
junaed-optimizely Jul 29, 2025
0618802
Reapply "[FSSDK-11529] condition adjustment"
junaed-optimizely Jul 29, 2025
83b7017
[FSSDK-11529] test addition + layer logic adjustment
junaed-optimizely Jul 29, 2025
fc8181f
Merge branch 'master' into junaed/fssdk-11529-update-impression-event
junaed-optimizely Jul 29, 2025
894af95
[FSSDK-11529] test data update
junaed-optimizely Jul 30, 2025
4fc5339
[FSSDK-11529] test name update
junaed-optimizely Jul 30, 2025
eb3df08
[FSSDK-11529] review improvements
junaed-optimizely Jul 31, 2025
504bb8c
[FSSDK-11529] fsc fix
junaed-optimizely Aug 1, 2025
6bf661a
[FSSDK-11529] project config issue fix
junaed-optimizely Aug 1, 2025
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: 3 additions & 1 deletion lib/event_processor/event_builder/user_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {

import { EventTags, UserAttributes } from '../../shared_types';
import { LoggerFacade } from '../../logging/logger';
import { DECISION_SOURCES } from '../../common_exports';

export type VisitorAttribute = {
entityId: string
Expand Down Expand Up @@ -185,7 +186,8 @@ export const buildImpressionEvent = function({
const variationId = decision.getVariationId(decisionObj);
const cmabUuid = decisionObj.cmabUuid;

const layerId = experimentId !== null ? getLayerId(configObj, experimentId) : null;
const layerId =
experimentId !== null && ruleType !== DECISION_SOURCES.HOLDOUT ? getLayerId(configObj, experimentId) : null;

return {
...buildBaseEvent({
Expand Down
2 changes: 1 addition & 1 deletion lib/feature_toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
* flag and all associated checks can be removed from the codebase.
*/

export const holdout = () => false;
export const holdout = () => true;
202 changes: 202 additions & 0 deletions lib/optimizely/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ import { DECISION_SOURCES } from '../utils/enums';
import OptimizelyUserContext from '../optimizely_user_context';
import { newErrorDecision } from '../optimizely_decision';
import { ImpressionEvent } from '../event_processor/event_builder/user_event';
import { OptimizelyDecideOption } from '../shared_types';


const holdoutData = [
{
id: 'holdout_test_id',
key: 'holdout_test_key',
status: 'Running',
includeFlags: [],
excludeFlags: [],
audienceIds: [],
audienceConditions: [],
variations: [
{
id: 'holdout_variation_id',
key: 'holdout_variation_key',
variables: [],
featureEnabled: false,
},
],
trafficAllocation: [
{
entityId: 'holdout_variation_id',
endOfRange: 10000,
},
],
},
];

describe('Optimizely', () => {
const eventDispatcher = {
Expand Down Expand Up @@ -212,5 +240,179 @@ describe('Optimizely', () => {
const event = processSpy.mock.calls[0][0] as ImpressionEvent;
expect(event.cmabUuid).toBe('uuid-cmab');
});

it('should dispatch impression event for holdout decision', async () => {
const datafile = getDecisionTestDatafile();

datafile.holdouts = holdoutData;
const projectConfig = createProjectConfig(datafile);

const projectConfigManager = getMockProjectConfigManager({
initConfig: projectConfig,
});

const mockEventDispatcher = {
dispatchEvent: vi.fn(() => Promise.resolve({ statusCode: 200 })),
};
const eventProcessor = getForwardingEventProcessor(mockEventDispatcher);
const processSpy = vi.spyOn(eventProcessor, 'process');

const optimizely = new Optimizely({
clientEngine: 'node-sdk',
projectConfigManager,
eventProcessor,
jsonSchemaValidator,
logger,
odpManager,
disposable: true,
cmabService: {} as any
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const decisionService = optimizely.decisionService;
vi.spyOn(decisionService, 'resolveVariationsForFeatureList').mockImplementation(() => {
return Value.of('async', [{
error: false,
result: {
variation: projectConfig.holdouts[0].variations[0],
experiment: projectConfig.holdouts[0],
decisionSource: DECISION_SOURCES.HOLDOUT,
},
reasons: [],
}]);
});

const user = new OptimizelyUserContext({
optimizely,
userId: 'test_user',
attributes: {},
});

const decision = await optimizely.decideAsync(user, 'flag_1', []);

expect(decision.ruleKey).toBe('holdout_test_key');
expect(decision.flagKey).toBe('flag_1');
expect(decision.variationKey).toBe('holdout_variation_key');
expect(decision.enabled).toBe(false);

expect(eventProcessor.process).toHaveBeenCalledOnce();

const event = processSpy.mock.calls[0][0] as ImpressionEvent;

expect(event.type).toBe('impression');
expect(event.ruleKey).toBe('holdout_test_key');
expect(event.ruleType).toBe('holdout');
expect(event.enabled).toBe(false);
});

it('should not dispatch impression event for holdout when DISABLE_DECISION_EVENT is used', async () => {
const datafile = getDecisionTestDatafile();

datafile.holdouts = holdoutData;

const projectConfig = createProjectConfig(datafile);

const projectConfigManager = getMockProjectConfigManager({
initConfig: projectConfig,
});

const mockEventDispatcher = {
dispatchEvent: vi.fn(() => Promise.resolve({ statusCode: 200 })),
};
const eventProcessor = getForwardingEventProcessor(mockEventDispatcher);
vi.spyOn(eventProcessor, 'process');

const optimizely = new Optimizely({
clientEngine: 'node-sdk',
projectConfigManager,
eventProcessor,
jsonSchemaValidator,
logger,
odpManager,
disposable: true,
cmabService: {} as any
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const decisionService = optimizely.decisionService;
vi.spyOn(decisionService, 'resolveVariationsForFeatureList').mockImplementation(() => {
return Value.of('async', [{
error: false,
result: {
variation: projectConfig.holdouts![0].variations[0],
experiment: projectConfig.holdouts![0],
decisionSource: DECISION_SOURCES.HOLDOUT,
},
reasons: [],
}]);
});

const user = new OptimizelyUserContext({
optimizely,
userId: 'test_user',
attributes: {},
});

const decision = await optimizely.decideAsync(user, 'flag_1', [OptimizelyDecideOption.DISABLE_DECISION_EVENT]);

expect(decision.ruleKey).toBe('holdout_test_key');
expect(decision.enabled).toBe(false);
expect(eventProcessor.process).not.toHaveBeenCalled();
});
});
describe('isFeatureEnabled', () => {
it('should dispatch impression event for holdout decision for isFeatureEnabled', async () => {
const datafile = getDecisionTestDatafile();
datafile.holdouts = holdoutData;

const projectConfig = createProjectConfig(datafile);
const projectConfigManager = getMockProjectConfigManager({
initConfig: projectConfig,
});
const mockEventDispatcher = {
dispatchEvent: vi.fn(() => Promise.resolve({ statusCode: 200 })),
};
const eventProcessor = getForwardingEventProcessor(mockEventDispatcher);
vi.spyOn(eventProcessor, 'process');

const optimizely = new Optimizely({
clientEngine: 'node-sdk',
projectConfigManager,
eventProcessor,
jsonSchemaValidator,
logger,
odpManager,
disposable: true,
cmabService: {} as any
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const decisionService = optimizely.decisionService;
vi.spyOn(decisionService, 'getVariationForFeature').mockReturnValue({
error: false,
result: {
variation: projectConfig.holdouts![0].variations[0],
experiment: projectConfig.holdouts![0],
decisionSource: DECISION_SOURCES.HOLDOUT,
},
reasons: [],
});
const result = optimizely.isFeatureEnabled('flag_1', 'test_user', {});

expect(result).toBe(false);

expect(eventProcessor.process).toHaveBeenCalledOnce();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const event = eventProcessor.process.mock.calls[0][0] as ImpressionEvent;

expect(event.type).toBe('impression');
expect(event.ruleKey).toBe('holdout_test_key');
expect(event.ruleType).toBe('holdout');
expect(event.enabled).toBe(false);
});
})
Copy link
Preview

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon after the closing brace. This is inconsistent with the rest of the codebase style.

Suggested change
})
});

Copilot uses AI. Check for mistakes.

});
4 changes: 3 additions & 1 deletion lib/optimizely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export default class Optimizely extends BaseService implements Client {

let featureEnabled = decision.getFeatureEnabledFromVariation(decisionObj);

if (decisionSource === DECISION_SOURCES.FEATURE_TEST) {
if (decisionSource === DECISION_SOURCES.FEATURE_TEST || decisionSource === DECISION_SOURCES.HOLDOUT) {
sourceInfo = {
experimentKey: experimentKey,
variationKey: variationKey,
Expand All @@ -661,6 +661,7 @@ export default class Optimizely extends BaseService implements Client {

if (
decisionSource === DECISION_SOURCES.FEATURE_TEST ||
decisionSource === DECISION_SOURCES.HOLDOUT ||
(decisionSource === DECISION_SOURCES.ROLLOUT && projectConfig.getSendFlagDecisionsValue(configObj))
) {
this.sendImpressionEvent(decisionObj, feature.key, userId, featureEnabled, attributes);
Expand Down Expand Up @@ -1503,6 +1504,7 @@ export default class Optimizely extends BaseService implements Client {
if (
!options[OptimizelyDecideOption.DISABLE_DECISION_EVENT] &&
(decisionSource === DECISION_SOURCES.FEATURE_TEST ||
decisionSource === DECISION_SOURCES.HOLDOUT ||
(decisionSource === DECISION_SOURCES.ROLLOUT && projectConfig.getSendFlagDecisionsValue(configObj)))
) {
this.sendImpressionEvent(decisionObj, key, userId, flagEnabled, attributes);
Expand Down
Loading