Skip to content

Commit 3c170cf

Browse files
committed
generalize identify operations with single operations dict
1 parent 6a8bb10 commit 3c170cf

File tree

4 files changed

+60
-54
lines changed

4 files changed

+60
-54
lines changed

amplitude.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,16 +3295,19 @@ var type = require('./type');
32953295
* only the first operation will be saved, and the rest will be ignored.
32963296
*/
32973297

3298-
var AMP_OP_ADD = '$add';
3299-
var AMP_OP_SET = '$set';
3300-
var AMP_OP_SET_ONCE = '$setOnce';
3301-
var AMP_OP_UNSET = '$unset';
3298+
// maps operation to identify string and proxy key
3299+
// order listed also defines order of processing
3300+
var OPERATIONS = {
3301+
'AMP_OP_SET_ONCE': {'opString': '$setOnce', 'proxyKey': 'so'},
3302+
'AMP_OP_UNSET': {'opString': '$unset', 'proxyKey': 'u'},
3303+
'AMP_OP_SET': {'opString': '$set', 'proxyKey': 's'},
3304+
'AMP_OP_ADD': {'opString': '$add', 'proxyKey': 'a'}
3305+
};
33023306

33033307
var log = function(s) {
33043308
console.log('[Amplitude] ' + s);
33053309
};
33063310

3307-
33083311
var Identify = function() {
33093312
this.userPropertiesOperations = {};
33103313
this.properties = []; // keep track of keys that have been added
@@ -3314,59 +3317,59 @@ Identify.prototype.fromProxyObject = function(proxyObject) {
33143317
if (!('p' in proxyObject)) {
33153318
return this;
33163319
}
3317-
3318-
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET_ONCE, proxyObject.p, 'so');
3319-
this._addOperationsFromProxyObjectDictionary(AMP_OP_UNSET, proxyObject.p, 'u');
3320-
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET, proxyObject.p, 's');
3321-
this._addOperationsFromProxyObjectDictionary(AMP_OP_ADD, proxyObject.p, 'a');
3320+
Object.keys(OPERATIONS).forEach(function(operation) {
3321+
this._addOperationsFromProxyObjectDictionary(operation, proxyObject.p);
3322+
}.bind(this));
33223323
return this;
33233324
};
33243325

3325-
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, userPropertyOperations, dictionary) {
3326-
if (dictionary in userPropertyOperations) {
3327-
for (var key in userPropertyOperations[dictionary]) {
3328-
if (userPropertyOperations[dictionary].hasOwnProperty(key)) {
3329-
this._addOperation(operation, key, userPropertyOperations[dictionary][key]);
3330-
}
3331-
}
3326+
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, proxyObjectDict) {
3327+
var proxyKey = OPERATIONS[operation].proxyKey;
3328+
if (!(proxyKey in proxyObjectDict)) {
3329+
return;
33323330
}
3331+
var userPropertiesOperations = proxyObjectDict[proxyKey];
3332+
Object.keys(userPropertiesOperations).forEach(function(key) {
3333+
this._addOperation(operation, key, userPropertiesOperations[key]);
3334+
}.bind(this));
33333335
};
33343336

33353337
Identify.prototype.add = function(property, value) {
33363338
if (type(value) === 'number' || type(value) === 'string') {
3337-
this._addOperation(AMP_OP_ADD, property, value);
3339+
this._addOperation('AMP_OP_ADD', property, value);
33383340
} else {
33393341
log('Unsupported type for value: ' + type(value) + ', expecting number or string');
33403342
}
33413343
return this;
33423344
};
33433345

33443346
Identify.prototype.set = function(property, value) {
3345-
this._addOperation(AMP_OP_SET, property, value);
3347+
this._addOperation('AMP_OP_SET', property, value);
33463348
return this;
33473349
};
33483350

33493351
Identify.prototype.setOnce = function(property, value) {
3350-
this._addOperation(AMP_OP_SET_ONCE, property, value);
3352+
this._addOperation('AMP_OP_SET_ONCE', property, value);
33513353
return this;
33523354
};
33533355

33543356
Identify.prototype.unset = function(property) {
3355-
this._addOperation(AMP_OP_UNSET, property, '-');
3357+
this._addOperation('AMP_OP_UNSET', property, '-');
33563358
return this;
33573359
};
33583360

33593361
Identify.prototype._addOperation = function(operation, property, value) {
3362+
var opString = OPERATIONS[operation].opString;
33603363
// check that property wasn't already used in this Identify
33613364
if (this.properties.indexOf(property) !== -1) {
3362-
log('User property "' + property + '" already used in this identify, skipping operation ' + operation);
3365+
log('User property "' + property + '" already used in this identify, skipping operation ' + opString);
33633366
return;
33643367
}
33653368

3366-
if (!(operation in this.userPropertiesOperations)){
3367-
this.userPropertiesOperations[operation] = {};
3369+
if (!(opString in this.userPropertiesOperations)){
3370+
this.userPropertiesOperations[opString] = {};
33683371
}
3369-
this.userPropertiesOperations[operation][property] = value;
3372+
this.userPropertiesOperations[opString][property] = value;
33703373
this.properties.push(property);
33713374
};
33723375

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/identify.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ var type = require('./type');
66
* only the first operation will be saved, and the rest will be ignored.
77
*/
88

9-
var AMP_OP_ADD = '$add';
10-
var AMP_OP_SET = '$set';
11-
var AMP_OP_SET_ONCE = '$setOnce';
12-
var AMP_OP_UNSET = '$unset';
9+
// maps operation to identify string and proxy key
10+
// order listed also defines order of processing
11+
var OPERATIONS = {
12+
'AMP_OP_SET_ONCE': {'opString': '$setOnce', 'proxyKey': 'so'},
13+
'AMP_OP_UNSET': {'opString': '$unset', 'proxyKey': 'u'},
14+
'AMP_OP_SET': {'opString': '$set', 'proxyKey': 's'},
15+
'AMP_OP_ADD': {'opString': '$add', 'proxyKey': 'a'}
16+
};
1317

1418
var log = function(s) {
1519
console.log('[Amplitude] ' + s);
1620
};
1721

18-
1922
var Identify = function() {
2023
this.userPropertiesOperations = {};
2124
this.properties = []; // keep track of keys that have been added
@@ -25,59 +28,59 @@ Identify.prototype.fromProxyObject = function(proxyObject) {
2528
if (!('p' in proxyObject)) {
2629
return this;
2730
}
28-
29-
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET_ONCE, proxyObject.p, 'so');
30-
this._addOperationsFromProxyObjectDictionary(AMP_OP_UNSET, proxyObject.p, 'u');
31-
this._addOperationsFromProxyObjectDictionary(AMP_OP_SET, proxyObject.p, 's');
32-
this._addOperationsFromProxyObjectDictionary(AMP_OP_ADD, proxyObject.p, 'a');
31+
Object.keys(OPERATIONS).forEach(function(operation) {
32+
this._addOperationsFromProxyObjectDictionary(operation, proxyObject.p);
33+
}.bind(this));
3334
return this;
3435
};
3536

36-
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, userPropertyOperations, dictionary) {
37-
if (dictionary in userPropertyOperations) {
38-
for (var key in userPropertyOperations[dictionary]) {
39-
if (userPropertyOperations[dictionary].hasOwnProperty(key)) {
40-
this._addOperation(operation, key, userPropertyOperations[dictionary][key]);
41-
}
42-
}
37+
Identify.prototype._addOperationsFromProxyObjectDictionary = function(operation, proxyObjectDict) {
38+
var proxyKey = OPERATIONS[operation].proxyKey;
39+
if (!(proxyKey in proxyObjectDict)) {
40+
return;
4341
}
42+
var userPropertiesOperations = proxyObjectDict[proxyKey];
43+
Object.keys(userPropertiesOperations).forEach(function(key) {
44+
this._addOperation(operation, key, userPropertiesOperations[key]);
45+
}.bind(this));
4446
};
4547

4648
Identify.prototype.add = function(property, value) {
4749
if (type(value) === 'number' || type(value) === 'string') {
48-
this._addOperation(AMP_OP_ADD, property, value);
50+
this._addOperation('AMP_OP_ADD', property, value);
4951
} else {
5052
log('Unsupported type for value: ' + type(value) + ', expecting number or string');
5153
}
5254
return this;
5355
};
5456

5557
Identify.prototype.set = function(property, value) {
56-
this._addOperation(AMP_OP_SET, property, value);
58+
this._addOperation('AMP_OP_SET', property, value);
5759
return this;
5860
};
5961

6062
Identify.prototype.setOnce = function(property, value) {
61-
this._addOperation(AMP_OP_SET_ONCE, property, value);
63+
this._addOperation('AMP_OP_SET_ONCE', property, value);
6264
return this;
6365
};
6466

6567
Identify.prototype.unset = function(property) {
66-
this._addOperation(AMP_OP_UNSET, property, '-');
68+
this._addOperation('AMP_OP_UNSET', property, '-');
6769
return this;
6870
};
6971

7072
Identify.prototype._addOperation = function(operation, property, value) {
73+
var opString = OPERATIONS[operation].opString;
7174
// check that property wasn't already used in this Identify
7275
if (this.properties.indexOf(property) !== -1) {
73-
log('User property "' + property + '" already used in this identify, skipping operation ' + operation);
76+
log('User property "' + property + '" already used in this identify, skipping operation ' + opString);
7477
return;
7578
}
7679

77-
if (!(operation in this.userPropertiesOperations)){
78-
this.userPropertiesOperations[operation] = {};
80+
if (!(opString in this.userPropertiesOperations)){
81+
this.userPropertiesOperations[opString] = {};
7982
}
80-
this.userPropertiesOperations[operation][property] = value;
83+
this.userPropertiesOperations[opString][property] = value;
8184
this.properties.push(property);
8285
};
8386

test/browser/amplitudejs.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
s.parentNode.insertBefore(as, s);
1212
var identifyFuncs = {'add':['a', false], 'set':['s', false], 'setOnce':['so', false], 'unset':['u', true]};
1313
amplitude.Identify = function(){ this.p = {}; Object.keys(identifyFuncs).forEach(function (key) {
14-
this.p[identifyFuncs[key][0]] = {};
14+
this.p[identifyFuncs[key][0]] = {};
1515
}.bind(this));};
1616
function proxyIdentifyFunc(fn, dict, overrideValue) {
17-
amplitude.Identify.prototype[fn] = function(k,v) { this.p[dict][k]=overrideValue?'-':v; return this; };
17+
amplitude.Identify.prototype[fn] = function(k,v) { this.p[dict][k]=overrideValue?'-':v; return this; };
1818
}
1919
Object.keys(identifyFuncs).forEach(function (key) {
20-
proxyIdentifyFunc(key, identifyFuncs[key][0], identifyFuncs[key][1]);
20+
proxyIdentifyFunc(key, identifyFuncs[key][0], identifyFuncs[key][1]);
2121
});
2222
amplitude._q = [];
2323
function proxy(fn) {

0 commit comments

Comments
 (0)