Skip to content

fix: Send correct impression event data for projects with duplicate rule keys #690

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 4 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions packages/optimizely-sdk/lib/core/decision/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ describe('lib/core/decision', function() {
});
});

describe('getExperimentId method', function() {
it('should return null when experiment is null', function() {
var experimentId = decision.getExperimentId(rolloutDecisionObj);
assert.strictEqual(experimentId, null);
});

it('should return null when experiment is not defined', function() {
var experimentId = decision.getExperimentId({});
assert.strictEqual(experimentId, null);
});

it('should return experiment id when experiment is defined', function() {
var experimentId = decision.getExperimentId(featureTestDecisionObj);
assert.strictEqual(experimentId, '594098');
});
});

describe('getVariationKey method', function() {
it('should return empty string when variation is null', function() {
var variationKey = decision.getVariationKey(rolloutDecisionObj);
Expand Down
9 changes: 9 additions & 0 deletions packages/optimizely-sdk/lib/core/decision/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ export function getVariationKey(decisionObj: DecisionObj): string {
export function getFeatureEnabledFromVariation(decisionObj: DecisionObj): boolean {
return decisionObj.variation?.featureEnabled ?? false;
}

/**
* Get experiment id from the provided decision object
* @param {DecisionObj} decisionObj Object representing decision
* @returns {string} Experiment id or null if experiment is null
*/
export function getExperimentId(decisionObj: DecisionObj): string | null {
return decisionObj.experiment?.id ?? null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { assert } from 'chai';

import fns from '../../utils/fns';
import * as projectConfig from '../project_config';
import * as decision from '../decision';
import { buildImpressionEvent, buildConversionEvent } from './event_helpers';

describe('lib/event_builder/event_helpers', function() {
Expand All @@ -33,19 +34,21 @@ describe('lib/event_builder/event_helpers', function() {
};

sinon.stub(projectConfig, 'getEventId');
sinon.stub(projectConfig, 'getVariationIdFromExperimentAndVariationKey');
sinon.stub(projectConfig, 'getExperimentId');
sinon.stub(projectConfig, 'getVariationIdFromExperimentIdAndVariationKey');
sinon.stub(projectConfig, 'getLayerId');
sinon.stub(projectConfig, 'getAttributeId');

sinon.stub(decision, 'getExperimentId');

sinon.stub(fns, 'uuid').returns('uuid');
sinon.stub(fns, 'currentTimestamp').returns(100);
});

afterEach(function() {
decision.getExperimentId.restore();

projectConfig.getEventId.restore();
projectConfig.getVariationIdFromExperimentAndVariationKey.restore();
projectConfig.getExperimentId.restore();
projectConfig.getVariationIdFromExperimentIdAndVariationKey.restore();
projectConfig.getLayerId.restore();
projectConfig.getAttributeId.restore();

Expand All @@ -56,7 +59,7 @@ 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 = {
var decisionObj = {
experiment: {
key: 'exp1',
status: 'Running',
Expand All @@ -79,17 +82,19 @@ describe('lib/event_builder/event_helpers', function() {
},
decisionSource: 'experiment',
}
projectConfig.getVariationIdFromExperimentAndVariationKey
.withArgs(configObj, 'exp1', 'var1')
decision.getExperimentId.withArgs(decisionObj).returns('exp1-id');

projectConfig.getVariationIdFromExperimentIdAndVariationKey
.withArgs(configObj, 'exp1-id', 'var1')
.returns('var1-id');
projectConfig.getExperimentId.withArgs(configObj, 'exp1').returns('exp1-id');
projectConfig.getLayerId.withArgs(configObj, 'exp1-id').returns('layer-id');

projectConfig.getAttributeId.withArgs(configObj, 'plan_type').returns('plan_type_id');


var result = buildImpressionEvent({
configObj: configObj,
decisionObj: decision,
decisionObj: decisionObj,
enabled: true,
flagKey: 'flagkey1',
userId: 'user1',
Expand Down Expand Up @@ -148,7 +153,7 @@ describe('lib/event_builder/event_helpers', function() {

describe('when botFiltering and anonymizeIP are undefined', function() {
it('should create an ImpressionEvent with the correct attributes', function() {
var decision = {
var decisionObj = {
experiment: {
key: 'exp1',
status: 'Running',
Expand All @@ -171,10 +176,11 @@ describe('lib/event_builder/event_helpers', function() {
},
decisionSource: 'experiment',
}
projectConfig.getVariationIdFromExperimentAndVariationKey
.withArgs(configObj, 'exp1', 'var1')
decision.getExperimentId.withArgs(decisionObj).returns('exp1-id');

projectConfig.getVariationIdFromExperimentIdAndVariationKey
.withArgs(configObj, 'exp1-id', 'var1')
.returns('var1-id');
projectConfig.getExperimentId.withArgs(configObj, 'exp1').returns('exp1-id');
projectConfig.getLayerId.withArgs(configObj, 'exp1-id').returns('layer-id');

projectConfig.getAttributeId.withArgs(configObj, 'plan_type').returns('plan_type_id');
Expand All @@ -184,7 +190,7 @@ describe('lib/event_builder/event_helpers', function() {

var result = buildImpressionEvent({
configObj: configObj,
decisionObj: decision,
decisionObj: decisionObj,
flagKey: 'flagkey1',
enabled: false,
userId: 'user1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import { DecisionObj } from '../decision_service';
import {
getAttributeId,
getEventId,
getExperimentId,
getLayerId,
getVariationIdFromExperimentAndVariationKey,
getVariationIdFromExperimentIdAndVariationKey,
ProjectConfig,
} from '../project_config';

Expand Down Expand Up @@ -134,10 +133,10 @@ export const buildImpressionEvent = function({

const ruleType = decisionObj.decisionSource;
const experimentKey = decision.getExperimentKey(decisionObj);
const experimentId = decision.getExperimentId(decisionObj);
const variationKey = decision.getVariationKey(decisionObj);

const variationId = experimentKey !== '' && variationKey !== '' ? getVariationIdFromExperimentAndVariationKey(configObj, experimentKey, variationKey) : null;
const experimentId = experimentKey !== '' ? getExperimentId(configObj, experimentKey) : null;
const variationId = experimentId !== null && variationKey !== '' ? getVariationIdFromExperimentIdAndVariationKey(configObj, experimentId, variationKey) : null;
const layerId = experimentId !== null ? getLayerId(configObj, experimentId) : null;

return {
Expand Down
13 changes: 13 additions & 0 deletions packages/optimizely-sdk/lib/core/project_config/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ describe('lib/core/project_config', function() {
});
});

describe('#getVariationIdFromExperimentIdAndVariationKey', function() {
it('should return the variation id for the given experiment id and variation key', function() {
assert.strictEqual(
projectConfig.getVariationIdFromExperimentIdAndVariationKey(
configObj,
testData.experiments[0].id,
testData.experiments[0].variations[0].key
),
testData.experiments[0].variations[0].id
);
});
});

describe('#getSendFlagDecisionsValue', function() {
it('should return false when sendFlagDecisions is undefined', function() {
configObj.sendFlagDecisions = undefined;
Expand Down
69 changes: 45 additions & 24 deletions packages/optimizely-sdk/lib/core/project_config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,26 @@ export const getVariationIdFromExperimentAndVariationKey = function(
return null;
};

/**
* Get the variation ID given the experiment id and variation key
* @param {ProjectConfig} projectConfig Object representing project configuration
* @param {string} experimentId Id of the experiment the variation belongs to
* @param {string} variationKey The variation key
* @return {string|null} Variation ID or null
*/
export const getVariationIdFromExperimentIdAndVariationKey = function(
projectConfig: ProjectConfig,
experimentId: string,
variationKey: string
): string | null {
const experiment = projectConfig.experimentIdMap[experimentId];
if (experiment.variationKeyMap.hasOwnProperty(variationKey)) {
return experiment.variationKeyMap[variationKey].id;
}

return null;
};

/**
* Get experiment from provided experiment key
* @param {ProjectConfig} projectConfig Object representing project configuration
Expand Down Expand Up @@ -727,28 +747,29 @@ export const getSendFlagDecisionsValue = function(projectConfig: ProjectConfig):
}

export default {
createProjectConfig: createProjectConfig,
getExperimentId: getExperimentId,
getLayerId: getLayerId,
getAttributeId: getAttributeId,
getEventId: getEventId,
getExperimentStatus: getExperimentStatus,
isActive: isActive,
isRunning: isRunning,
getExperimentAudienceConditions: getExperimentAudienceConditions,
getVariationKeyFromId: getVariationKeyFromId,
getVariationIdFromExperimentAndVariationKey: getVariationIdFromExperimentAndVariationKey,
getExperimentFromKey: getExperimentFromKey,
getTrafficAllocation: getTrafficAllocation,
getExperimentFromId: getExperimentFromId,
getFeatureFromKey: getFeatureFromKey,
getVariableForFeature: getVariableForFeature,
getVariableValueForVariation: getVariableValueForVariation,
getTypeCastValue: getTypeCastValue,
getSendFlagDecisionsValue: getSendFlagDecisionsValue,
getAudiencesById: getAudiencesById,
eventWithKeyExists: eventWithKeyExists,
isFeatureExperiment: isFeatureExperiment,
toDatafile: toDatafile,
tryCreatingProjectConfig: tryCreatingProjectConfig,
createProjectConfig,
getExperimentId,
getLayerId,
getAttributeId,
getEventId,
getExperimentStatus,
isActive,
isRunning,
getExperimentAudienceConditions,
getVariationKeyFromId,
getVariationIdFromExperimentAndVariationKey,
getVariationIdFromExperimentIdAndVariationKey,
getExperimentFromKey,
getTrafficAllocation,
getExperimentFromId,
getFeatureFromKey,
getVariableForFeature,
getVariableValueForVariation,
getTypeCastValue,
getSendFlagDecisionsValue,
getAudiencesById,
eventWithKeyExists,
isFeatureExperiment,
toDatafile,
tryCreatingProjectConfig,
};
7 changes: 3 additions & 4 deletions packages/optimizely-sdk/lib/optimizely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,13 @@ export default class Optimizely {

const ruleType = decisionObj.decisionSource;
const experimentKey = decision.getExperimentKey(decisionObj);
const experimentId = decision.getExperimentId(decisionObj);
const variationKey = decision.getVariationKey(decisionObj);

let experimentId: string | null = null;
let variationId: string | null = null;

if (experimentKey !== '' && variationKey !== '') {
variationId = projectConfig.getVariationIdFromExperimentAndVariationKey(configObj, experimentKey, variationKey);
experimentId = projectConfig.getExperimentId(configObj, experimentKey);
if (experimentId !== null && variationKey !== '') {
variationId = projectConfig.getVariationIdFromExperimentIdAndVariationKey(configObj, experimentId, variationKey);
}

const impressionEventOptions = {
Expand Down