Skip to content

Commit ceb6de0

Browse files
committed
Addressing feedback
1 parent c09b87d commit ceb6de0

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

packages/optimizely-sdk/lib/core/event_builder/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function getCommonEventParams(options) {
5858
};
5959

6060
fns.forOwn(attributes, function(attributeValue, attributeKey){
61-
var attributeId = projectConfig.getAttributeId(options.configObj, attributeKey);
61+
var attributeId = projectConfig.getAttributeId(options.configObj, attributeKey, options.logger);
6262
if (attributeId) {
6363
commonParams.visitors[0].attributes.push({
6464
entity_id: attributeId,

packages/optimizely-sdk/lib/core/event_builder/index.tests.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ var uuid = require('uuid');
2727
describe('lib/core/event_builder', function() {
2828
describe('APIs', function() {
2929

30+
var mockLogger;
3031
var configObj;
3132
var clock;
3233

3334
beforeEach(function() {
3435
configObj = projectConfig.createProjectConfig(testData.getTestProjectConfig());
3536
clock = sinon.useFakeTimers(new Date().getTime());
3637
sinon.stub(uuid, 'v4').returns('a68cf1ad-0393-4e18-af87-efe8f01a7c9c');
38+
mockLogger = {
39+
log: sinon.stub(),
40+
};
3741
});
3842

3943
afterEach(function() {
@@ -279,6 +283,7 @@ describe('lib/core/event_builder', function() {
279283
experimentId: '111127',
280284
variationId: '111128',
281285
userId: 'testUser',
286+
logger: mockLogger,
282287
};
283288

284289
var actualParams = eventBuilder.getImpressionEvent(eventOptions);
@@ -345,14 +350,6 @@ describe('lib/core/event_builder', function() {
345350
});
346351

347352
describe('getConversionEvent', function() {
348-
var mockLogger;
349-
350-
beforeEach(function() {
351-
mockLogger = {
352-
log: sinon.stub(),
353-
};
354-
});
355-
356353
it('should create proper params for getConversionEvent without attributes or event value', function() {
357354
var expectedParams = {
358355
url: 'https://logx.optimizely.com/v1/events',
@@ -606,7 +603,7 @@ describe('lib/core/event_builder', function() {
606603
};
607604

608605
var actualParams = eventBuilder.getConversionEvent(eventOptions);
609-
606+
sinon.assert.calledOnce(mockLogger.log);
610607
assert.deepEqual(actualParams, expectedParams);
611608
});
612609

@@ -949,14 +946,6 @@ describe('lib/core/event_builder', function() {
949946
});
950947

951948
describe('createEventWithBucketingId', function () {
952-
var mockLogger;
953-
954-
beforeEach(function () {
955-
mockLogger = {
956-
log: sinon.stub(),
957-
};
958-
});
959-
960949
it('should send proper bucketingID with user attributes', function () {
961950
var expectedParams = {
962951
url: 'https://logx.optimizely.com/v1/events',

packages/optimizely-sdk/lib/core/project_config/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var sprintf = require('sprintf');
1919

2020
var EXPERIMENT_LAUNCHED_STATUS = 'Launched';
2121
var EXPERIMENT_RUNNING_STATUS = 'Running';
22+
var RESERVED_ATTRIBUTE_PREFIX = '$opt_';
2223
var MODULE_NAME = 'PROJECT_CONFIG';
2324

2425
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
@@ -132,15 +133,18 @@ module.exports = {
132133
* Get attribute ID for the provided attribute key
133134
* @param {Object} projectConfig Object representing project configuration
134135
* @param {string} attributeKey Attribute key for which ID is to be determined
135-
* @return {string|null} Attribute ID corresponding to the provided attribute key
136+
* @param {Object} logger
137+
* @return {string|null} Attribute ID corresponding to the provided attribute key. Attribute key if it is a reserved attribute.
136138
*/
137-
getAttributeId: function(projectConfig, attributeKey) {
139+
getAttributeId: function(projectConfig, attributeKey, logger) {
138140
var attribute = projectConfig.attributeKeyMap[attributeKey];
139141
if (attribute) {
140142
return attribute.id;
141-
} else if (enums.RESERVED_USER_ATTRIBUTES.indexOf(attributeKey) !== -1) {
143+
} else if (attributeKey.startsWith(RESERVED_ATTRIBUTE_PREFIX)) {
142144
return attributeKey;
143145
}
146+
147+
logger.log(LOG_LEVEL.DEBUG, sprintf(LOG_MESSAGES.UNRECOGNIZED_ATTRIBUTE, MODULE_NAME, attributeKey));
144148
return null;
145149
},
146150

packages/optimizely-sdk/lib/core/project_config/index.tests.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,15 @@ describe('lib/core/project_config', function() {
216216
describe('projectConfig helper methods', function() {
217217
var testData = testDatafile.getTestProjectConfig();
218218
var configObj;
219-
219+
var createdLogger = logger.createLogger({logLevel: LOG_LEVEL.INFO});
220+
220221
beforeEach(function() {
221222
configObj = projectConfig.createProjectConfig(testData);
223+
sinon.stub(createdLogger, 'log');
224+
});
225+
226+
afterEach(function() {
227+
createdLogger.log.restore();
222228
});
223229

224230
it('should retrieve experiment ID for valid experiment key in getExperimentId', function() {
@@ -246,8 +252,15 @@ describe('lib/core/project_config', function() {
246252
assert.strictEqual(projectConfig.getAttributeId(configObj, 'browser_type'), '111094');
247253
});
248254

255+
it('should retrieve attribute ID for reserved attribute key in getAttributeId', function() {
256+
assert.strictEqual(projectConfig.getAttributeId(configObj, '$opt_user_agent'), '$opt_user_agent');
257+
});
258+
249259
it('should return null for invalid attribute key in getAttributeId', function() {
250-
assert.isNull(projectConfig.getAttributeId(configObj, 'invalidAttributeKey'));
260+
assert.isNull(projectConfig.getAttributeId(configObj, 'invalidAttributeKey', createdLogger));
261+
sinon.assert.calledWithExactly(createdLogger.log,
262+
LOG_LEVEL.DEBUG,
263+
'PROJECT_CONFIG: Unrecognized attribute invalidAttributeKey provided. Pruning before sending event to Optimizely.')
251264
});
252265

253266
it('should retrieve event ID for valid event key in getEventId', function() {

packages/optimizely-sdk/lib/optimizely/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Optimizely.prototype._sendImpressionEvent = function(experimentKey, variationKey
181181
experimentId: experimentId,
182182
userId: userId,
183183
variationId: variationId,
184+
logger: this.logger,
184185
};
185186
var impressionEvent = eventBuilder.getImpressionEvent(impressionEventOptions);
186187
var dispatchedImpressionEventLogMessage = sprintf(LOG_MESSAGES.DISPATCH_IMPRESSION_EVENT,

packages/optimizely-sdk/lib/utils/enums/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ exports.ERROR_MESSAGES = {
5050
NO_JSON_PROVIDED: '%s: No JSON object to validate against schema.',
5151
NO_VARIATION_FOR_EXPERIMENT_KEY: '%s: No variation key %s defined in datafile for experiment %s.',
5252
UNDEFINED_ATTRIBUTE: '%s: Provided attribute: %s has an undefined value.',
53+
UNRECOGNIZED_ATTRIBUTE: '%s: Unrecognized attribute %s provided. Pruning before sending event to Optimizely.',
5354
UNABLE_TO_CAST_VALUE: '%s: Unable to cast value %s to type %s, returning null.',
5455
USER_NOT_IN_FORCED_VARIATION: '%s: User %s is not in the forced variation map. Cannot remove their forced variation.',
5556
USER_PROFILE_LOOKUP_ERROR: '%s: Error while looking up user profile for user ID "%s": %s.',
@@ -134,11 +135,6 @@ exports.RESERVED_ATTRIBUTES = {
134135
USER_AGENT: '$opt_user_agent',
135136
};
136137

137-
exports.RESERVED_USER_ATTRIBUTES = [
138-
exports.RESERVED_ATTRIBUTES.BUCKETING_ID,
139-
exports.RESERVED_ATTRIBUTES.USER_AGENT
140-
];
141-
142138
exports.JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk';
143139
exports.NODE_CLIENT_ENGINE = 'node-sdk';
144140
exports.NODE_CLIENT_VERSION = '2.0.1';

0 commit comments

Comments
 (0)