Skip to content

feat(flag-decisions): Add support for sending flag decisions #599

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 15 commits into from
Oct 28, 2020
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
1 change: 1 addition & 0 deletions packages/optimizely-sdk/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Added support sending impression events every time a decision is made ([#599](https://github.com/optimizely/javascript-sdk/pull/599))

## [4.3.4] - October 8, 2020

Expand Down
71 changes: 71 additions & 0 deletions packages/optimizely-sdk/lib/core/decision/index.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from 'chai';
import { rolloutDecisionObj, featureTestDecisionObj } from '../../tests/test_data';
import * as decision from './';

describe('lib/core/decision', function() {
describe('getExperimentKey method', function() {
it('should return empty string when experiment is null', function() {
var experimentKey = decision.getExperimentKey(rolloutDecisionObj);
assert.strictEqual(experimentKey, '');
});

it('should return empty string when experiment is not defined', function() {
var experimentKey = decision.getExperimentKey({});
assert.strictEqual(experimentKey, '');
});

it('should return experiment key when experiment is defined', function() {
var experimentKey = decision.getExperimentKey(featureTestDecisionObj);
assert.strictEqual(experimentKey, 'testing_my_feature');
});
});

describe('getVariationKey method', function() {
it('should return empty string when variation is null', function() {
var variationKey = decision.getVariationKey(rolloutDecisionObj);
assert.strictEqual(variationKey, '');
});

it('should return empty string when variation is not defined', function() {
var variationKey = decision.getVariationKey({});
assert.strictEqual(variationKey, '');
});

it('should return variation key when variation is defined', function() {
var variationKey = decision.getVariationKey(featureTestDecisionObj);
assert.strictEqual(variationKey, 'variation');
});
});

describe('getFeatureEnabledFromVariation method', function() {
it('should return false when variation is null', function() {
var featureEnabled = decision.getFeatureEnabledFromVariation(rolloutDecisionObj);
assert.strictEqual(featureEnabled, false);
});

it('should return false when variation is not defined', function() {
var featureEnabled = decision.getFeatureEnabledFromVariation({});
assert.strictEqual(featureEnabled, false);
});

it('should return featureEnabled boolean when variation is defined', function() {
var featureEnabled = decision.getFeatureEnabledFromVariation(featureTestDecisionObj);
assert.strictEqual(featureEnabled, true);
});
});
})
44 changes: 44 additions & 0 deletions packages/optimizely-sdk/lib/core/decision/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add some basic unit tests for this module. I think we should have some coverage for when experiment or variation are null or undefined.

* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DecisionObj } from '../decision_service';

/**
* Get experiment key from the provided decision object
* @param {DecisionObj} decisionObj Object representing decision
* @returns {string} Experiment key or empty string if experiment is null
*/
export function getExperimentKey(decisionObj: DecisionObj): string {
return decisionObj.experiment?.key ?? '';
}

/**
* Get variation key from the provided decision object
* @param {DecisionObj} decisionObj Object representing decision
* @returns {string} Variation key or empty string if variation is null
*/
export function getVariationKey(decisionObj: DecisionObj): string {
return decisionObj.variation?.key ?? '';
}

/**
* Get featureEnabled from variation in the provided decision object
* @param {DecisionObj} decisionObj Object representing decision
* @returns {boolean} featureEnabled boolean or false if variation is null
*/
export function getFeatureEnabledFromVariation(decisionObj: DecisionObj): boolean {
return decisionObj.variation?.featureEnabled ?? false;
}
6 changes: 3 additions & 3 deletions packages/optimizely-sdk/lib/core/decision_service/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface DecisionService {
* @param {FeatureFlag} feature A feature flag object from project configuration
* @param {string} userId A string identifying the user, for bucketing
* @param {unknown} attributes Optional user attributes
* @return {Decision} An object with experiment, variation, and decisionSource
* @return {DecisionObj} An object with experiment, variation, and decisionSource
* properties. If the user was not bucketed into a variation, the variation
* property is null.
*/
Expand All @@ -62,7 +62,7 @@ export interface DecisionService {
feature: FeatureFlag,
userId: string,
attributes: unknown
): Decision;
): DecisionObj;

/**
* Removes forced variation for given userId and experimentKey
Expand Down Expand Up @@ -99,7 +99,7 @@ interface Options {
UNSTABLE_conditionEvaluators: unknown;
}

interface Decision {
export interface DecisionObj {
experiment: Experiment | null;
variation: Variation | null;
decisionSource: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,11 @@ DecisionService.prototype.getForcedVariation = function(configObj, experimentKey

/**
* Sets the forced variation for a user in a given experiment
* @param {Object} configObj Object representing project configuration
* @param {string} experimentKey Key for experiment.
* @param {string} userId The user Id.
* @param {string} variationKey Key for variation. If null, then clear the existing experiment-to-variation mapping
* @return {boolean} A boolean value that indicates if the set completed successfully.
* @param {Object} configObj Object representing project configuration
* @param {string} experimentKey Key for experiment.
* @param {string} userId The user Id.
* @param {string|null} variationKey Key for variation. If null, then clear the existing experiment-to-variation mapping
* @return {boolean} A boolean value that indicates if the set completed successfully.
*/
DecisionService.prototype.setForcedVariation = function(configObj, experimentKey, userId, variationKey) {
if (variationKey != null && !stringValidator.validate(variationKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/
import { ProjectConfig } from '../project_config';
import { DecisionObj } from '../decision_service';
import { EventTags, UserAttributes } from '../../shared_types';

interface ImpressionConfig {
experimentKey: string;
variationKey: string;
decisionObj: DecisionObj;
userId: string;
flagKey: string;
userAttributes?: UserAttributes;
clientEngine: string;
clientVersion: string;
Expand Down Expand Up @@ -60,6 +61,10 @@ interface ImpressionEvent {
id: string;
key: string;
} | null;

ruleKey: string,
flagKey: string,
ruleType: string,
}

interface ConversionConfig {
Expand Down
31 changes: 23 additions & 8 deletions packages/optimizely-sdk/lib/core/event_builder/event_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import fns from '../../utils/fns';
import projectConfig from '../project_config';
import * as eventTagUtils from '../../utils/event_tag_utils';
import * as attributesValidator from'../../utils/attributes_validator';
import * as decision from '../decision';

var logger = getLogger('EVENT_BUILDER');

/**
* Creates an ImpressionEvent object from decision data
* @param {Object} config
* @param {Object} config.configObj
* @param {String} config.experimentKey
* @param {String} config.variationKey
* @param {Object} config.decisionObj
* @param {String} config.userId
* @param {Object} config.userAttributes
* @param {String} config.clientEngine
Expand All @@ -36,17 +35,29 @@ var logger = getLogger('EVENT_BUILDER');
*/
export var buildImpressionEvent = function(config) {
var configObj = config.configObj;
var experimentKey = config.experimentKey;
var variationKey = config.variationKey;
var decisionObj = config.decisionObj;
var userId = config.userId;
var flagKey = config.flagKey;
var userAttributes = config.userAttributes;
var clientEngine = config.clientEngine;
var clientVersion = config.clientVersion;
var ruleType = decisionObj.decisionSource;
var experimentKey = decision.getExperimentKey(decisionObj);
var variationKey = decision.getVariationKey(decisionObj);

var variationId = projectConfig.getVariationIdFromExperimentAndVariationKey(configObj, experimentKey, variationKey);
var experimentId = projectConfig.getExperimentId(configObj, experimentKey);
var layerId = projectConfig.getLayerId(configObj, experimentId);
let experimentId = null;
let variationId = null;

if (experimentKey !== '' && variationKey !== '') {
variationId = projectConfig.getVariationIdFromExperimentAndVariationKey(configObj, experimentKey, variationKey);
}
if (experimentKey !== '') {
experimentId = projectConfig.getExperimentId(configObj, experimentKey);
}
let layerId = null;
if (experimentId !== null) {
layerId = projectConfig.getLayerId(configObj, experimentId);
}
return {
type: 'impression',
timestamp: fns.currentTimestamp(),
Expand Down Expand Up @@ -80,6 +91,10 @@ export var buildImpressionEvent = function(config) {
id: variationId,
key: variationKey,
},

ruleKey: experimentKey,
flagKey: flagKey,
ruleType: ruleType,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ describe('lib/event_builder/event_helpers', function() {
describe('buildImpressionEvent', function() {
describe('when botFiltering and anonymizeIP are true', function() {
it('should build an ImpressionEvent with the correct attributes', function() {
var decision = {
experiment: {
key: 'exp1',
status: 'Running',
forcedVariations: {},
audienceIds: [],
layerId: 'layer-id',
trafficAllocation: [],
variationKeyMap: {
'variation': {
key: 'var1',
id: 'var1-id',
}
},
id: 'exp1-id',
variations: [{ key: 'var1', id: 'var1-id' }],
},
variation: {
key: 'var1',
id: 'var1-id',
},
decisionSource: 'experiment',
}
projectConfig.getVariationIdFromExperimentAndVariationKey
.withArgs(configObj, 'exp1', 'var1')
.returns('var1-id');
Expand All @@ -66,8 +89,8 @@ describe('lib/event_builder/event_helpers', function() {

var result = buildImpressionEvent({
configObj: configObj,
experimentKey: 'exp1',
variationKey: 'var1',
decisionObj: decision,
flagKey: 'flagkey1',
userId: 'user1',
userAttributes: {
plan_type: 'bronze',
Expand Down Expand Up @@ -113,12 +136,39 @@ describe('lib/event_builder/event_helpers', function() {
id: 'var1-id',
key: 'var1',
},

ruleKey: "exp1",
flagKey: 'flagkey1',
ruleType: 'experiment',
});
});
});

describe('when botFiltering and anonymizeIP are undefined', function() {
it('should create an ImpressionEvent with the correct attributes', function() {
var decision = {
experiment: {
key: 'exp1',
status: 'Running',
forcedVariations: {},
audienceIds: [],
layerId: '253442',
trafficAllocation: [],
variationKeyMap: {
'variation': {
key: 'var1',
id: 'var1-id',
}
},
id: '1237847778',
variations: [{ key: 'var1', id: 'var1-id' }],
},
variation: {
key: 'var1',
id: 'var1-id',
},
decisionSource: 'experiment',
}
projectConfig.getVariationIdFromExperimentAndVariationKey
.withArgs(configObj, 'exp1', 'var1')
.returns('var1-id');
Expand All @@ -132,8 +182,8 @@ describe('lib/event_builder/event_helpers', function() {

var result = buildImpressionEvent({
configObj: configObj,
experimentKey: 'exp1',
variationKey: 'var1',
decisionObj: decision,
flagKey: 'flagkey1',
userId: 'user1',
userAttributes: {
plan_type: 'bronze',
Expand Down Expand Up @@ -179,6 +229,10 @@ describe('lib/event_builder/event_helpers', function() {
id: 'var1-id',
key: 'var1',
},

ruleKey: "exp1",
flagKey: 'flagkey1',
ruleType: 'experiment',
});
});
});
Expand Down
7 changes: 5 additions & 2 deletions packages/optimizely-sdk/lib/core/event_builder/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ interface ImpressionOptions {
clientEngine: string;
clientVersion: string;
configObj: ProjectConfig;
experimentId: string;
experimentId: string | null;
ruleKey: string;
flagKey: string;
ruleType: string;
eventKey?: string;
variationId: string;
variationId: string | null;
logger?: LogHandler;
userId: string;
}
Expand Down
Loading