Repository navigation
Expand file tree
/
Copy pathidentify.js
More file actions
92 lines (78 loc) · 2.83 KB
/
Copy pathidentify.js
File metadata and controls
92 lines (78 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var type = require('./type');
/*
* Wrapper for a user properties JSON object that supports operations.
* Note: if a user property is used in multiple operations on the same Identify object,
* only the first operation will be saved, and the rest will be ignored.
*/
var AMP_OP_ADD = '$add';
var AMP_OP_APPEND = '$append';
var AMP_OP_CLEAR_ALL = '$clearAll';
var AMP_OP_SET = '$set';
var AMP_OP_SET_ONCE = '$setOnce';
var AMP_OP_UNSET = '$unset';
var log = function(s) {
try {
console.log('[Amplitude] ' + s);
} catch (e) {
// console logging not available
}
};
var Identify = function() {
this.userPropertiesOperations = {};
this.properties = []; // keep track of keys that have been added
};
Identify.prototype.add = function(property, value) {
if (type(value) === 'number' || type(value) === 'string') {
this._addOperation(AMP_OP_ADD, property, value);
} else {
log('Unsupported type for value: ' + type(value) + ', expecting number or string');
}
return this;
};
Identify.prototype.append = function(property, value) {
this._addOperation(AMP_OP_APPEND, property, value);
return this;
};
// clearAll should be sent on its own Identify object
// If there are already other operations, then don't add clearAll
// If clearAll already in Identify, don't add other operations
Identify.prototype.clearAll = function() {
if (Object.keys(this.userPropertiesOperations).length > 0) {
if (!this.userPropertiesOperations.hasOwnProperty(AMP_OP_CLEAR_ALL)) {
log('Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll');
}
return this;
}
this.userPropertiesOperations[AMP_OP_CLEAR_ALL] = '-';
return this;
};
Identify.prototype.set = function(property, value) {
this._addOperation(AMP_OP_SET, property, value);
return this;
};
Identify.prototype.setOnce = function(property, value) {
this._addOperation(AMP_OP_SET_ONCE, property, value);
return this;
};
Identify.prototype.unset = function(property) {
this._addOperation(AMP_OP_UNSET, property, '-');
return this;
};
Identify.prototype._addOperation = function(operation, property, value) {
// check that the identify doesn't already contain a clearAll
if (this.userPropertiesOperations.hasOwnProperty(AMP_OP_CLEAR_ALL)) {
log('This identify already contains a $clearAll operation, skipping operation ' + operation);
return;
}
// check that property wasn't already used in this Identify
if (this.properties.indexOf(property) !== -1) {
log('User property "' + property + '" already used in this identify, skipping operation ' + operation);
return;
}
if (!this.userPropertiesOperations.hasOwnProperty(operation)){
this.userPropertiesOperations[operation] = {};
}
this.userPropertiesOperations[operation][property] = value;
this.properties.push(property);
};
module.exports = Identify;