Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 97e1d2e

Browse files
Merge pull request #15 from optimizely/new-optimizely
Introduce New Optimizely endpoint and bump to v0.1.4
2 parents 22ce4a4 + 310a14c commit 97e1d2e

File tree

17 files changed

+1744
-522
lines changed

17 files changed

+1744
-522
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
-------------------------------------------------------------------------------
2+
0.1.4
3+
-------------------------------------------------------------------------------
4+
* Add functionality for New Optimizely endpoint.
5+
-------------------------------------------------------------------------------
6+
17
-------------------------------------------------------------------------------
28
0.1.3
39
-------------------------------------------------------------------------------
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Classic Optimizely is the older version of Optimizely's endpoint. We are keeping this to support
3+
* existing running experiments, and this will eventually be deprecated.
4+
*/
5+
6+
var _ = require('lodash/core');
7+
var projectConfig = require('../../project_config');
8+
var sprintf = require('sprintf');
9+
10+
module.exports = {
11+
CLASSIC_OPTIMIZELY_ENDPOINT: 'https://%s.log.optimizely.com/event',
12+
13+
GET_METHOD: 'GET',
14+
15+
urlParams: {
16+
accountId: 'd',
17+
projectId: 'a',
18+
experimentPrefix: 'x',
19+
goalId: 'g',
20+
goalName: 'n',
21+
segmentPrefix: 's',
22+
endUserId: 'u',
23+
eventValue: 'v',
24+
source: 'src',
25+
time: 'time'
26+
},
27+
28+
/**
29+
* Get attribute(s) information to the event
30+
* @param {Object} configObj Object representing project configuration
31+
* @param {Object} attributes Object representing user attributes and values which need to be recorded
32+
*/
33+
getAttributeParams: function(configObj, attributes) {
34+
var attributeParams = {};
35+
_.forEach(attributes, function(attributeValue, attributeKey) {
36+
if (attributeValue || attributeValue === false || attributeValue === 0) {
37+
var segmentId = configObj.attributeKeyMap[attributeKey].segmentId;
38+
var segmentParam = sprintf('%s%s', module.exports.urlParams.segmentPrefix, segmentId);
39+
attributeParams[segmentParam] = attributeValue;
40+
}
41+
});
42+
return attributeParams;
43+
},
44+
45+
/**
46+
* Get impression goal information to the event
47+
* @param {Object} configObj Object representing project configuration
48+
* @param {string} experimentKey Experiment which is being activated
49+
* @return {Object} Impression goal params with impression goal information
50+
*/
51+
getImpressionGoalParams: function(configObj, experimentKey) {
52+
var impressionGoalParams = {};
53+
var experimentId = projectConfig.getExperimentId(configObj, experimentKey);
54+
// For tracking impressions, goal ID is set equal to experiment ID of experiment being activated
55+
impressionGoalParams[module.exports.urlParams.goalId] = experimentId;
56+
impressionGoalParams[module.exports.urlParams.goalName] = 'visitor-event';
57+
return impressionGoalParams;
58+
},
59+
60+
/**
61+
* Get conversion goal information to the event
62+
* @param {Object} configObj Object representing project configuration
63+
* @param {string} eventKey Goal key representing the event which needs to be recorded
64+
* @param {number} eventValue Value associated with the event. Can be used to represent revenue in cents.
65+
* @return {Object} Conversion goal params with conversion goal information
66+
*/
67+
getConversionGoalParams: function(configObj, eventKey, eventValue) {
68+
var conversionGoalParams = {};
69+
var goalId = configObj.eventKeyMap[eventKey].id;
70+
var eventIds = goalId;
71+
if (eventValue) {
72+
eventIds = sprintf('%s,%s', goalId, projectConfig.getRevenueGoalId(configObj));
73+
conversionGoalParams[module.exports.urlParams.eventValue] = eventValue;
74+
}
75+
conversionGoalParams[module.exports.urlParams.goalId] = eventIds;
76+
conversionGoalParams[module.exports.urlParams.goalName] = eventKey;
77+
return conversionGoalParams;
78+
},
79+
80+
/**
81+
* Get experiment to variation mapping to the impression event
82+
* @param {Object} configObj Object representing project configuration
83+
* @param {string} experimentKey Experiment which is being activated
84+
* @param {string} variationId Experiment which is being activated
85+
* @return {Object} Experiment params with experiment to variation mapping for impression events
86+
*/
87+
getExperimentParams: function(configObj, experimentKey, variationId) {
88+
var experimentParams = {};
89+
var experimentId = projectConfig.getExperimentId(configObj, experimentKey);
90+
var experimentParam = sprintf('%s%s', module.exports.urlParams.experimentPrefix, experimentId);
91+
experimentParams[experimentParam] = variationId;
92+
return experimentParams;
93+
},
94+
95+
/**
96+
* Maps experiment and corresponding variation as parameters to be used in the event tracking call
97+
* @param {Object} configObj Object representing project configuration
98+
* @param {Array} variationIds Experiment(s) which are being tracked
99+
* @param {Array} validExperimentKeysForEvent Array of valid experiment keys that are associated with the event being tracked
100+
* @return {Object} Experiment variation params with experiment to variation mapping for conversion events
101+
*/
102+
getExperimentVariationParams: function(configObj, variationIds, validExperimentKeysForEvent) {
103+
var experimentVariationParams = {};
104+
105+
_.forEach(validExperimentKeysForEvent, function(experimentKey) {
106+
var experimentId = projectConfig.getExperimentId(configObj, experimentKey);
107+
var experimentParam = sprintf('%s%s', module.exports.urlParams.experimentPrefix, experimentId);
108+
var variationId = projectConfig.getEventVariationIdFromExperimentKey(configObj, experimentKey, variationIds);
109+
experimentVariationParams[experimentParam] = variationId;
110+
});
111+
112+
return experimentVariationParams;
113+
},
114+
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var _ = require('lodash/core');
2+
var projectConfig = require('../../project_config');
3+
4+
var REVENUE_EVENT_METRIC_NAME = 'revenue';
5+
6+
module.exports = {
7+
8+
NEW_OPTIMIZELY_IMPRESSION_ENDPOINT: 'https://p13nlog.dz.optimizely.com/log/decision',
9+
10+
NEW_OPTIMIZELY_CONVERSION_ENDPOINT: 'https://p13nlog.dz.optimizely.com/log/event',
11+
12+
POST_METHOD: 'POST',
13+
14+
CUSTOM_ATTRIBUTE_FEATURE_TYPE: 'custom',
15+
16+
/**
17+
* Creates object of params specific to impression events
18+
* @param {Object} configObj Object representing project configuration
19+
* @param {string} experimentKey Experiment for which impression needs to be recorded
20+
* @param {string} variationId ID for variation which would be presented to user
21+
* @return {Object} Impression event specific params for New Optimizely endpoint
22+
*/
23+
getImpressionEventParams: function(configObj, experimentKey, variationId) {
24+
var impressionEventParams = {
25+
layerId: projectConfig.getLayerId(configObj, experimentKey),
26+
decision: {
27+
isLayerHoldback: false,
28+
experimentId: projectConfig.getExperimentId(configObj, experimentKey),
29+
variationId: variationId,
30+
},
31+
};
32+
return impressionEventParams;
33+
},
34+
35+
/**
36+
* Creates object of params specific to conversion events
37+
* @param {Object} configObj Object representing project configuration
38+
* @param {string} eventKey Event key representing the event which needs to be recorded
39+
* @param {number} eventValue Value associated with the event. Can be used to represent revenue in cents
40+
* @param {Array} variationIds Experiment variation ID(s) which are being tracked
41+
* @param {Array} validExperimentKeysForEvent Array of valid experiment keys that are associated with the event key
42+
* @return {Object} Conversion event specific params for New Optimizely endpoint
43+
*/
44+
getConversionEventParams: function(configObj, eventKey, eventValue, variationIds, validExperimentKeysForEvent) {
45+
var conversionEventParams = {
46+
eventEntityId: projectConfig.getEventId(configObj, eventKey),
47+
eventName: eventKey,
48+
eventFeatures: [],
49+
layerStates: [],
50+
};
51+
52+
_.forEach(validExperimentKeysForEvent, function(experimentKey) {
53+
var experimentId = projectConfig.getExperimentId(configObj, experimentKey);
54+
var variationId = projectConfig.getEventVariationIdFromExperimentKey(configObj, experimentKey, variationIds);
55+
56+
var layerState = {
57+
layerId: projectConfig.getLayerId(configObj, experimentKey),
58+
decision: {
59+
isLayerHoldback: false,
60+
experimentId: experimentId,
61+
variationId: variationId,
62+
},
63+
actionTriggered: true,
64+
};
65+
66+
conversionEventParams.layerStates.push(layerState);
67+
});
68+
69+
conversionEventParams.eventMetrics = [];
70+
if (eventValue) {
71+
var revenueMetric = {
72+
name: REVENUE_EVENT_METRIC_NAME,
73+
value: eventValue,
74+
};
75+
conversionEventParams.eventMetrics.push(revenueMetric);
76+
}
77+
return conversionEventParams;
78+
},
79+
};

0 commit comments

Comments
 (0)