Skip to content

Commit f238339

Browse files
committed
prepend operation
1 parent 5e7fab9 commit f238339

File tree

9 files changed

+71
-10
lines changed

9 files changed

+71
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
* Add support for passing callback function to identify.
4+
* Add support for prepend user property operation.
45

56
### 2.9.1 (March 6, 2016)
67

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ Amplitude-Javascript
1414
r.onload=function(){e.amplitude.runQueuedFunctions()};var i=t.getElementsByTagName("script")[0];
1515
i.parentNode.insertBefore(r,i);var s=function(){this._q=[];return this};function o(e){
1616
s.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
17-
return this}}var a=["add","append","clearAll","set","setOnce","unset"];for(var c=0;c<a.length;c++){
17+
return this}}var a=["add","append","clearAll","prepend","set","setOnce","unset"];for(var c=0;c<a.length;c++){
1818
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
19-
function l(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
20-
}}for(var n=0;n<u.length;n++){t(u[n])}}l(n);e.amplitude=n})(window,document);
19+
function p(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
20+
}}for(var n=0;n<u.length;n++){t(u[n])}}p(n);e.amplitude=n})(window,document);
2121
2222
amplitude.init("YOUR_API_KEY_HERE");
2323
</script>
@@ -120,6 +120,13 @@ The SDK supports the operations `set`, `setOnce`, `unset`, and `add` on individu
120120
amplitude.identify(identify);
121121
```
122122

123+
6. `prepend`: this will prepend a value or values to a user property. If the user property does not have a value set yet, it will be initialized to an empty list before the new values are prepended. If the user property has an existing value and it is not a list, it will be converted into a list with the new value prepended.
124+
125+
```javascript
126+
var identify = new amplitude.Identify().prepend('ab-tests', 'new-user-test').prepend('some_list', [1, 2, 3, 4, 'values']);
127+
amplitude.identify(identify);
128+
```
129+
123130
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. In this example, only the set operation will be saved, and the add and unset will be ignored:
124131

125132
```javascript

amplitude-segment-snippet.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function(e,t){var n=e.amplitude||{_q:[]};var r=function(){this._q=[];return this;
22
};function i(e){r.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
3-
return this}}var o=["add","append","clearAll","set","setOnce","unset"];for(var s=0;s<o.length;s++){
3+
return this}}var o=["add","append","clearAll","prepend","set","setOnce","unset"];for(var s=0;s<o.length;s++){
44
i(o[s])}n.Identify=r;var a=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
55
function c(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
66
}}for(var n=0;n<a.length;n++){t(a[n])}}c(n);e.amplitude=n})(window,document);

amplitude-snippet.min.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.9.1-m
33
r.onload=function(){e.amplitude.runQueuedFunctions()};var i=t.getElementsByTagName("script")[0];
44
i.parentNode.insertBefore(r,i);var s=function(){this._q=[];return this};function o(e){
55
s.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
6-
return this}}var a=["add","append","clearAll","set","setOnce","unset"];for(var c=0;c<a.length;c++){
6+
return this}}var a=["add","append","clearAll","prepend","set","setOnce","unset"];for(var c=0;c<a.length;c++){
77
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
8-
function l(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
9-
}}for(var n=0;n<u.length;n++){t(u[n])}}l(n);e.amplitude=n})(window,document);
8+
function p(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
9+
}}for(var n=0;n<u.length;n++){t(u[n])}}p(n);e.amplitude=n})(window,document);

amplitude.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,7 @@ var utils = require('./utils');
21022102
var AMP_OP_ADD = '$add';
21032103
var AMP_OP_APPEND = '$append';
21042104
var AMP_OP_CLEAR_ALL = '$clearAll';
2105+
var AMP_OP_PREPEND = '$prepend';
21052106
var AMP_OP_SET = '$set';
21062107
var AMP_OP_SET_ONCE = '$setOnce';
21072108
var AMP_OP_UNSET = '$unset';
@@ -2139,6 +2140,11 @@ Identify.prototype.clearAll = function() {
21392140
return this;
21402141
};
21412142

2143+
Identify.prototype.prepend = function(property, value) {
2144+
this._addOperation(AMP_OP_PREPEND, property, value);
2145+
return this;
2146+
};
2147+
21422148
Identify.prototype.set = function(property, value) {
21432149
this._addOperation(AMP_OP_SET, property, value);
21442150
return this;

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/amplitude-snippet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
this._q.push([fn].concat(Array.prototype.slice.call(arguments, 0))); return this;
1414
};
1515
}
16-
var identifyFuncs = ['add', 'append', 'clearAll', 'set', 'setOnce', 'unset'];
16+
var identifyFuncs = ['add', 'append', 'clearAll', 'prepend', 'set', 'setOnce', 'unset'];
1717
for (var i = 0; i < identifyFuncs.length; i++) {proxyIdentify(identifyFuncs[i]);}
1818
amplitude.Identify = Identify;
1919
var funcs = ['init', 'logEvent', 'logRevenue', 'setUserId', 'setUserProperties',

src/identify.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var utils = require('./utils');
1010
var AMP_OP_ADD = '$add';
1111
var AMP_OP_APPEND = '$append';
1212
var AMP_OP_CLEAR_ALL = '$clearAll';
13+
var AMP_OP_PREPEND = '$prepend';
1314
var AMP_OP_SET = '$set';
1415
var AMP_OP_SET_ONCE = '$setOnce';
1516
var AMP_OP_UNSET = '$unset';
@@ -47,6 +48,11 @@ Identify.prototype.clearAll = function() {
4748
return this;
4849
};
4950

51+
Identify.prototype.prepend = function(property, value) {
52+
this._addOperation(AMP_OP_PREPEND, property, value);
53+
return this;
54+
};
55+
5056
Identify.prototype.set = function(property, value) {
5157
this._addOperation(AMP_OP_SET, property, value);
5258
return this;

test/identify.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,41 @@ describe('Identify', function() {
141141
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
142142
});
143143

144+
it ('should prepend properties', function () {
145+
var property1 = 'var value';
146+
var value1 = 'testValue';
147+
148+
var property2 = 'float value';
149+
var value2 = 0.123;
150+
151+
var property3 = 'bool value';
152+
var value3 = true;
153+
154+
var property4 = 'json value';
155+
var value4 = {};
156+
157+
var property5 = 'list value';
158+
var value5 = [1, 2, 'test'];
159+
160+
var identify = new Identify().prepend(property1, value1).prepend(property2, value2);
161+
identify.prepend(property3, value3).prepend(property4, value4).prepend(property5, value5);
162+
163+
// identify should ignore this since duplicate key
164+
identify.setOnce(property1, value3);
165+
166+
var expected = {
167+
'$prepend': {}
168+
}
169+
expected['$prepend'][property1] = value1;
170+
expected['$prepend'][property2] = value2;
171+
expected['$prepend'][property3] = value3;
172+
expected['$prepend'][property4] = value4;
173+
expected['$prepend'][property5] = value5;
174+
175+
assert.deepEqual(expected, identify.userPropertiesOperations);
176+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
177+
});
178+
144179
it ('should allow multiple operations', function () {
145180
var property1 = 'string value';
146181
var value1 = 'testValue';
@@ -156,15 +191,20 @@ describe('Identify', function() {
156191
var property5 = 'list value';
157192
var value5 = [1, 2, 'test'];
158193

194+
var property6 = 'int value';
195+
var value6 = 100;
196+
159197
var identify = new Identify().setOnce(property1, value1).add(property2, value2);
160198
identify.set(property3, value3).unset(property4).append(property5, value5);
199+
identify.prepend(property6, value6);
161200

162201
// identify should ignore this since duplicate key
163202
identify.set(property4, value3);
164203

165204
var expected = {
166205
'$add': {},
167206
'$append': {},
207+
'$prepend': {},
168208
'$set': {},
169209
'$setOnce': {},
170210
'$unset': {}
@@ -174,9 +214,10 @@ describe('Identify', function() {
174214
expected['$set'][property3] = value3;
175215
expected['$unset'][property4] = '-';
176216
expected['$append'][property5] = value5;
217+
expected['$prepend'][property6] = value6;
177218

178219
assert.deepEqual(expected, identify.userPropertiesOperations);
179-
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
220+
assert.deepEqual([property1, property2, property3, property4, property5, property6], identify.properties);
180221
});
181222

182223
it ('should disallow duplicate properties', function () {

0 commit comments

Comments
 (0)