|
| 1 | +// var querystring = require('querystring'); |
| 2 | + |
| 3 | +/* |
| 4 | + * Wrapper for a user properties JSON object that supports operations |
| 5 | + */ |
| 6 | + |
| 7 | +var AMP_OP_ADD = '$add'; |
| 8 | +var AMP_OP_SET = '$set'; |
| 9 | +var AMP_OP_SET_ONCE = '$setOnce'; |
| 10 | +var AMP_OP_UNSET = '$unset'; |
| 11 | + |
| 12 | + |
| 13 | +var Identify = function() { |
| 14 | + this.userPropertiesOperations = {}; |
| 15 | + this.properties = []; // keep track of keys that have been added |
| 16 | +}; |
| 17 | + |
| 18 | +Identify.prototype.add = function(property, value) { |
| 19 | + // validate value is number?? |
| 20 | + this._addOperation(AMP_OP_ADD, property, value); |
| 21 | + return this; |
| 22 | +}; |
| 23 | + |
| 24 | +Identify.prototype.set = function(property, value) { |
| 25 | + this._addOperation(AMP_OP_SET, property, value); |
| 26 | + return this; |
| 27 | +}; |
| 28 | + |
| 29 | +Identify.prototype.setOnce = function(property, value) { |
| 30 | + this._addOperation(AMP_OP_SET_ONCE, value); |
| 31 | + return this; |
| 32 | +}; |
| 33 | + |
| 34 | +Identify.prototype.unset = function(property) { |
| 35 | + this._addOperation(AMP_OP_UNSET, property, '-'); |
| 36 | + return this; |
| 37 | +}; |
| 38 | + |
| 39 | +Identify.prototype._addOperation = function(operation, property, value) { |
| 40 | + // check that property wasn't already used in this Identify |
| 41 | + if (this.properties.indexOf(property) !== -1) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (!(operation in this.userPropertiesOperations)){ |
| 46 | + this.userPropertiesOperations[operation] = {}; |
| 47 | + } |
| 48 | + this.userPropertiesOperations[operation][property] = value; |
| 49 | + this.properties.push(property); |
| 50 | +}; |
| 51 | + |
| 52 | + |
| 53 | +module.exports = { |
| 54 | + Identify: Identify, |
| 55 | + AMP_OP_ADD: AMP_OP_ADD, |
| 56 | + AMP_OP_SET: AMP_OP_SET, |
| 57 | + AMP_OP_SET_ONCE: AMP_OP_SET_ONCE, |
| 58 | + AMP_OP_UNSET: AMP_OP_UNSET |
| 59 | +}; |
0 commit comments