Skip to content

fix(track): Batch decision payloads into the same array in the snapshot. #155

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 1 commit into from
Aug 21, 2018
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
70 changes: 34 additions & 36 deletions packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,47 +115,45 @@ function getImpressionEventParams(configObj, experimentId, variationId) {
* @param {Object} logger Logger object
* @return {Object} Conversion event params
*/
function getConversionEventParams(configObj, eventKey, eventTags, experimentsToVariationMap, logger) {

var conversionEventParams = [];
function getVisitorSnapshot(configObj, eventKey, eventTags, experimentsToVariationMap, logger) {
var snapshot = {
decisions: [],
events: []
};

fns.forOwn(experimentsToVariationMap, function(variationId, experimentId) {

var decision = {
decisions: [{
campaign_id: projectConfig.getLayerId(configObj, experimentId),
experiment_id: experimentId,
variation_id: variationId,
}],
events: [],
campaign_id: projectConfig.getLayerId(configObj, experimentId),
experiment_id: experimentId,
variation_id: variationId,
};

var eventDict = {
entity_id: projectConfig.getEventId(configObj, eventKey),
timestamp: fns.currentTimestamp(),
uuid: fns.uuid(),
key: eventKey,
};
snapshot.decisions.push(decision);
});

if (eventTags) {
var revenue = eventTagUtils.getRevenueValue(eventTags, logger);
if (revenue) {
eventDict[enums.RESERVED_EVENT_KEYWORDS.REVENUE] = revenue;
}
var eventDict = {
entity_id: projectConfig.getEventId(configObj, eventKey),
timestamp: fns.currentTimestamp(),
uuid: fns.uuid(),
key: eventKey,
};

var eventValue = eventTagUtils.getEventValue(eventTags, logger);
if (eventValue) {
eventDict[enums.RESERVED_EVENT_KEYWORDS.VALUE] = eventValue;
}
if (eventTags) {
var revenue = eventTagUtils.getRevenueValue(eventTags, logger);
if (revenue) {
eventDict[enums.RESERVED_EVENT_KEYWORDS.REVENUE] = revenue;
}

eventDict['tags'] = eventTags;
var eventValue = eventTagUtils.getEventValue(eventTags, logger);
if (eventValue) {
eventDict[enums.RESERVED_EVENT_KEYWORDS.VALUE] = eventValue;
}
decision.events = [eventDict];

conversionEventParams.push(decision);
});
eventDict['tags'] = eventTags;
}
snapshot.events.push(eventDict);

return conversionEventParams;
return snapshot;
}

module.exports = {
Expand Down Expand Up @@ -210,13 +208,13 @@ module.exports = {
var commonParams = getCommonEventParams(options);
conversionEvent.url = ENDPOINT;

var conversionEventParams = getConversionEventParams(options.configObj,
options.eventKey,
options.eventTags,
options.experimentsToVariationMap,
options.logger);
var snapshot = getVisitorSnapshot(options.configObj,
options.eventKey,
options.eventTags,
options.experimentsToVariationMap,
options.logger);

commonParams.visitors[0].snapshots = conversionEventParams;
commonParams.visitors[0].snapshots = [snapshot];
conversionEvent.params = commonParams;

return conversionEvent;
Expand Down
53 changes: 53 additions & 0 deletions packages/optimizely-sdk/lib/core/event_builder/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,59 @@ describe('lib/core/event_builder', function() {
assert.deepEqual(actualParams, expectedParams);
});

it('should create the correct snapshot for multiple experiments attached to the event', function() {
var expectedParams = {
url: 'https://logx.optimizely.com/v1/events',
httpVerb: 'POST',
params: {
'account_id': '12001',
'project_id': '111001',
'visitors': [{
'visitor_id': 'testUser',
'attributes': [],
'snapshots': [{
'decisions': [{
'variation_id': '111128',
'experiment_id': '111127',
'campaign_id': '4'
}, {
'variation_id': '122228',
'experiment_id': '122227',
'campaign_id': '5'
}],
'events': [{
'timestamp': Math.round(new Date().getTime()),
'entity_id': '111100',
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'key': 'testEventWithMultipleExperiments'
}]
}]
}],
'revision': '42',
'client_name': 'node-sdk',
'client_version': packageJSON.version,
'anonymize_ip': false,
}
};

var eventOptions = {
clientEngine: 'node-sdk',
clientVersion: packageJSON.version,
configObj: configObj,
eventKey: 'testEventWithMultipleExperiments',
logger: mockLogger,
userId: 'testUser',
experimentsToVariationMap: {
'111127': '111128',
'122227': '122228'
},
};

var actualParams = eventBuilder.getConversionEvent(eventOptions);

assert.deepEqual(actualParams, expectedParams);
});

describe('and event tags are passed it', function() {
it('should create proper params for getConversionEvent with event tags', function() {
var expectedParams = {
Expand Down
11 changes: 1 addition & 10 deletions packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1272,16 +1272,7 @@ describe('lib/optimizely', function() {
'campaign_id': '4',
'experiment_id': '111127',
'variation_id': '111129'
}],
'events': [{
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this no longer in the snapshot?

Copy link
Contributor Author

@mikeproeng37 mikeproeng37 Aug 21, 2018

Choose a reason for hiding this comment

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

It is, it gets added on line 154. It's just that instead of having the event duplicated for every decision, we just send one event per snapshot.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh okay cool

'entity_id': '111100',
'timestamp': Math.round(new Date().getTime()),
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'key': 'testEventWithMultipleExperiments',
}]
},
{
'decisions': [{
}, {
'campaign_id': '5',
'experiment_id': '122227',
'variation_id': '122229'
Expand Down