Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 748eaca

Browse files
authored
feat(log event notification): Update event processor version & pass notificationCenter to event processor (optimizely#345)
Summary: This PR puts together the final pieces to enable the log event notification. - The event processor dependency is updated to the latest version, which has support for receiving a notification center and using it to trigger log event notifications - Initialization code in Optimizely is updated to pass the notification center to the event processor - js-sdk-utils is also updated to the latest version in order to start using that package as the single source of truth for notification types Test plan: Updated unit tests Issues: https://optimizely.atlassian.net/browse/OASIS-4976
1 parent ff47244 commit 748eaca

File tree

5 files changed

+94
-54
lines changed

5 files changed

+94
-54
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ function Optimizely(config) {
112112
dispatcher: this.eventDispatcher,
113113
flushInterval: config.eventFlushInterval !== undefined ? config.eventFlushInterval : DEFAULT_EVENT_FLUSH_INTERVAL,
114114
maxQueueSize: config.eventBatchSize !== undefined ? config.eventBatchSize : DEFAULT_EVENT_MAX_QUEUE_SIZE,
115+
notificationCenter: this.notificationCenter,
115116
});
116117
this.eventProcessor.start();
117118

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

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5607,11 +5607,11 @@ describe('lib/optimizely', function() {
56075607
isValidInstance: true,
56085608
});
56095609

5610-
sinon.assert.calledWithExactly(eventProcessor.LogTierV1EventProcessor, {
5610+
sinon.assert.calledWithExactly(eventProcessor.LogTierV1EventProcessor, sinon.match({
56115611
dispatcher: eventDispatcher,
56125612
flushInterval: 5000,
56135613
maxQueueSize: 1,
5614-
});
5614+
}));
56155615
});
56165616
});
56175617

@@ -5629,11 +5629,11 @@ describe('lib/optimizely', function() {
56295629
eventBatchSize: 100,
56305630
});
56315631

5632-
sinon.assert.calledWithExactly(eventProcessor.LogTierV1EventProcessor, {
5632+
sinon.assert.calledWithExactly(eventProcessor.LogTierV1EventProcessor, sinon.match({
56335633
dispatcher: eventDispatcher,
56345634
flushInterval: 50,
56355635
maxQueueSize: 100,
5636-
});
5636+
}));
56375637
});
56385638
});
56395639
});
@@ -5939,4 +5939,61 @@ describe('lib/optimizely', function() {
59395939
});
59405940
});
59415941
});
5942+
5943+
describe('log event notification', function() {
5944+
var optlyInstance;
5945+
var bucketStub;
5946+
var eventDispatcherSpy;
5947+
beforeEach(function() {
5948+
bucketStub = sinon.stub(bucketer, 'bucket');
5949+
eventDispatcherSpy = sinon.spy();
5950+
optlyInstance = new Optimizely({
5951+
clientEngine: 'node-sdk',
5952+
datafile: testData.getTestProjectConfig(),
5953+
eventBuilder: eventBuilder,
5954+
errorHandler: {
5955+
handleError: function() {},
5956+
},
5957+
eventDispatcher: {
5958+
dispatchEvent: eventDispatcherSpy,
5959+
},
5960+
logger: {
5961+
log: function() {},
5962+
},
5963+
isValidInstance: true,
5964+
eventBatchSize: 1,
5965+
});
5966+
});
5967+
5968+
afterEach(function() {
5969+
bucketer.bucket.restore();
5970+
optlyInstance.close();
5971+
});
5972+
5973+
it('should trigger a log event notification when an impression event is dispatched', function() {
5974+
var notificationListener = sinon.spy();
5975+
optlyInstance.notificationCenter.addNotificationListener(
5976+
enums.NOTIFICATION_TYPES.LOG_EVENT,
5977+
notificationListener
5978+
);
5979+
bucketStub.returns('111129');
5980+
var activate = optlyInstance.activate('testExperiment', 'testUser');
5981+
assert.strictEqual(activate, 'variation');
5982+
sinon.assert.calledOnce(eventDispatcherSpy);
5983+
sinon.assert.calledOnce(notificationListener);
5984+
sinon.assert.calledWithExactly(notificationListener, eventDispatcherSpy.getCall(0).args[0]);
5985+
});
5986+
5987+
it('should trigger a log event notification when a conversion event is dispatched', function() {
5988+
var notificationListener = sinon.spy();
5989+
optlyInstance.notificationCenter.addNotificationListener(
5990+
enums.NOTIFICATION_TYPES.LOG_EVENT,
5991+
notificationListener
5992+
);
5993+
optlyInstance.track('testEvent', 'testUser');
5994+
sinon.assert.calledOnce(eventDispatcherSpy);
5995+
sinon.assert.calledOnce(notificationListener);
5996+
sinon.assert.calledWithExactly(notificationListener, eventDispatcherSpy.getCall(0).args[0]);
5997+
});
5998+
});
59425999
});

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

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License. *
1515
***************************************************************************/
1616

17+
var jsSdkUtils = require('@optimizely/js-sdk-utils');
18+
1719
/**
1820
* Contains global enums used throughout the library
1921
*/
@@ -164,47 +166,7 @@ exports.VALID_CLIENT_ENGINES = [
164166
exports.JAVASCRIPT_CLIENT_ENGINE,
165167
];
166168

167-
/*
168-
* Notification types for use with NotificationCenter
169-
* Format is EVENT: <list of parameters to callback>
170-
*
171-
* SDK consumers can use these to register callbacks with the notification center.
172-
*
173-
* @deprecated since 3.1.0
174-
* ACTIVATE: An impression event will be sent to Optimizely
175-
* Callbacks will receive an object argument with the following properties:
176-
* - experiment {Object}
177-
* - userId {string}
178-
* - attributes {Object|undefined}
179-
* - variation {Object}
180-
* - logEvent {Object}
181-
*
182-
* DECISION: A decision is made in the system. i.e. user activation,
183-
* feature access or feature-variable value retrieval
184-
* Callbacks will receive an object argument with the following properties:
185-
* - type {string}
186-
* - userId {string}
187-
* - attributes {Object|undefined}
188-
* - decisionInfo {Object|undefined}
189-
*
190-
* OPTIMIZELY_CONFIG_UPDATE: This Optimizely instance has been updated with a new
191-
* config
192-
*
193-
* TRACK: A conversion event will be sent to Optimizely
194-
* Callbacks will receive the an object argument with the following properties:
195-
* - eventKey {string}
196-
* - userId {string}
197-
* - attributes {Object|undefined}
198-
* - eventTags {Object|undefined}
199-
* - logEvent {Object}
200-
*
201-
*/
202-
exports.NOTIFICATION_TYPES = {
203-
ACTIVATE: 'ACTIVATE:experiment, user_id,attributes, variation, event',
204-
DECISION: 'DECISION:type, userId, attributes, decisionInfo',
205-
OPTIMIZELY_CONFIG_UPDATE: 'OPTIMIZELY_CONFIG_UPDATE',
206-
TRACK: 'TRACK:event_key, user_id, attributes, event_tags, event',
207-
};
169+
exports.NOTIFICATION_TYPES = jsSdkUtils.NOTIFICATION_TYPES;
208170

209171
exports.DECISION_NOTIFICATION_TYPES = {
210172
AB_TEST: 'ab-test',

packages/optimizely-sdk/package-lock.json

Lines changed: 27 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/optimizely-sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"homepage": "https://github.com/optimizely/javascript-sdk/tree/master/packages/optimizely-sdk",
3434
"dependencies": {
3535
"@optimizely/js-sdk-datafile-manager": "^0.4.0",
36-
"@optimizely/js-sdk-event-processor": "^0.2.1",
36+
"@optimizely/js-sdk-event-processor": "^0.3.0",
3737
"@optimizely/js-sdk-logging": "^0.1.0",
38-
"@optimizely/js-sdk-utils": "^0.1.0",
38+
"@optimizely/js-sdk-utils": "^0.2.0",
3939
"json-schema": "^0.2.3",
4040
"lodash": "^4.17.11",
4141
"murmurhash": "0.0.2",

0 commit comments

Comments
 (0)