Skip to content

Commit 5b379df

Browse files
authored
fix(track): Send all decisions for the same event in one snapshot. (#155)
1 parent 7ab1195 commit 5b379df

File tree

3 files changed

+88
-46
lines changed

3 files changed

+88
-46
lines changed

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

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,47 +115,45 @@ function getImpressionEventParams(configObj, experimentId, variationId) {
115115
* @param {Object} logger Logger object
116116
* @return {Object} Conversion event params
117117
*/
118-
function getConversionEventParams(configObj, eventKey, eventTags, experimentsToVariationMap, logger) {
119-
120-
var conversionEventParams = [];
118+
function getVisitorSnapshot(configObj, eventKey, eventTags, experimentsToVariationMap, logger) {
119+
var snapshot = {
120+
decisions: [],
121+
events: []
122+
};
121123

122124
fns.forOwn(experimentsToVariationMap, function(variationId, experimentId) {
123-
124125
var decision = {
125-
decisions: [{
126-
campaign_id: projectConfig.getLayerId(configObj, experimentId),
127-
experiment_id: experimentId,
128-
variation_id: variationId,
129-
}],
130-
events: [],
126+
campaign_id: projectConfig.getLayerId(configObj, experimentId),
127+
experiment_id: experimentId,
128+
variation_id: variationId,
131129
};
132130

133-
var eventDict = {
134-
entity_id: projectConfig.getEventId(configObj, eventKey),
135-
timestamp: fns.currentTimestamp(),
136-
uuid: fns.uuid(),
137-
key: eventKey,
138-
};
131+
snapshot.decisions.push(decision);
132+
});
139133

140-
if (eventTags) {
141-
var revenue = eventTagUtils.getRevenueValue(eventTags, logger);
142-
if (revenue) {
143-
eventDict[enums.RESERVED_EVENT_KEYWORDS.REVENUE] = revenue;
144-
}
134+
var eventDict = {
135+
entity_id: projectConfig.getEventId(configObj, eventKey),
136+
timestamp: fns.currentTimestamp(),
137+
uuid: fns.uuid(),
138+
key: eventKey,
139+
};
145140

146-
var eventValue = eventTagUtils.getEventValue(eventTags, logger);
147-
if (eventValue) {
148-
eventDict[enums.RESERVED_EVENT_KEYWORDS.VALUE] = eventValue;
149-
}
141+
if (eventTags) {
142+
var revenue = eventTagUtils.getRevenueValue(eventTags, logger);
143+
if (revenue) {
144+
eventDict[enums.RESERVED_EVENT_KEYWORDS.REVENUE] = revenue;
145+
}
150146

151-
eventDict['tags'] = eventTags;
147+
var eventValue = eventTagUtils.getEventValue(eventTags, logger);
148+
if (eventValue) {
149+
eventDict[enums.RESERVED_EVENT_KEYWORDS.VALUE] = eventValue;
152150
}
153-
decision.events = [eventDict];
154151

155-
conversionEventParams.push(decision);
156-
});
152+
eventDict['tags'] = eventTags;
153+
}
154+
snapshot.events.push(eventDict);
157155

158-
return conversionEventParams;
156+
return snapshot;
159157
}
160158

161159
module.exports = {
@@ -210,13 +208,13 @@ module.exports = {
210208
var commonParams = getCommonEventParams(options);
211209
conversionEvent.url = ENDPOINT;
212210

213-
var conversionEventParams = getConversionEventParams(options.configObj,
214-
options.eventKey,
215-
options.eventTags,
216-
options.experimentsToVariationMap,
217-
options.logger);
211+
var snapshot = getVisitorSnapshot(options.configObj,
212+
options.eventKey,
213+
options.eventTags,
214+
options.experimentsToVariationMap,
215+
options.logger);
218216

219-
commonParams.visitors[0].snapshots = conversionEventParams;
217+
commonParams.visitors[0].snapshots = [snapshot];
220218
conversionEvent.params = commonParams;
221219

222220
return conversionEvent;

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,59 @@ describe('lib/core/event_builder', function() {
782782
assert.deepEqual(actualParams, expectedParams);
783783
});
784784

785+
it('should create the correct snapshot for multiple experiments attached to the event', function() {
786+
var expectedParams = {
787+
url: 'https://logx.optimizely.com/v1/events',
788+
httpVerb: 'POST',
789+
params: {
790+
'account_id': '12001',
791+
'project_id': '111001',
792+
'visitors': [{
793+
'visitor_id': 'testUser',
794+
'attributes': [],
795+
'snapshots': [{
796+
'decisions': [{
797+
'variation_id': '111128',
798+
'experiment_id': '111127',
799+
'campaign_id': '4'
800+
}, {
801+
'variation_id': '122228',
802+
'experiment_id': '122227',
803+
'campaign_id': '5'
804+
}],
805+
'events': [{
806+
'timestamp': Math.round(new Date().getTime()),
807+
'entity_id': '111100',
808+
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
809+
'key': 'testEventWithMultipleExperiments'
810+
}]
811+
}]
812+
}],
813+
'revision': '42',
814+
'client_name': 'node-sdk',
815+
'client_version': packageJSON.version,
816+
'anonymize_ip': false,
817+
}
818+
};
819+
820+
var eventOptions = {
821+
clientEngine: 'node-sdk',
822+
clientVersion: packageJSON.version,
823+
configObj: configObj,
824+
eventKey: 'testEventWithMultipleExperiments',
825+
logger: mockLogger,
826+
userId: 'testUser',
827+
experimentsToVariationMap: {
828+
'111127': '111128',
829+
'122227': '122228'
830+
},
831+
};
832+
833+
var actualParams = eventBuilder.getConversionEvent(eventOptions);
834+
835+
assert.deepEqual(actualParams, expectedParams);
836+
});
837+
785838
describe('and event tags are passed it', function() {
786839
it('should create proper params for getConversionEvent with event tags', function() {
787840
var expectedParams = {

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,16 +1272,7 @@ describe('lib/optimizely', function() {
12721272
'campaign_id': '4',
12731273
'experiment_id': '111127',
12741274
'variation_id': '111129'
1275-
}],
1276-
'events': [{
1277-
'entity_id': '111100',
1278-
'timestamp': Math.round(new Date().getTime()),
1279-
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
1280-
'key': 'testEventWithMultipleExperiments',
1281-
}]
1282-
},
1283-
{
1284-
'decisions': [{
1275+
}, {
12851276
'campaign_id': '5',
12861277
'experiment_id': '122227',
12871278
'variation_id': '122229'

0 commit comments

Comments
 (0)