|
| 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 | +}; |
0 commit comments