Skip to content

Commit ad61f0c

Browse files
committed
Merge pull request #42 from amplitude/append_operation
Append operation
2 parents b7a78c6 + 5de513f commit ad61f0c

File tree

10 files changed

+72
-12
lines changed

10 files changed

+72
-12
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 getSessionId helper method to fetch the current sessionId.
4+
* Add support for append user property operation.
45

56
## 2.7.0 (December 1, 2015)
67

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ The SDK supports the operations `set`, `setOnce`, `unset`, and `add` on individu
9797
amplitude.identify(identify);
9898
```
9999

100+
5. `append`: this will append 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 appended. 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 appended.
101+
102+
```javascript
103+
var identify = new amplitude.Identify().append('ab-tests', 'new-user-test').append('some_list', [1, 2, 3, 4, 'values']);
104+
amplitude.identify(identify);
105+
```
106+
100107
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:
101108

102109
```javascript

amplitude-segment-snippet.min.js

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

amplitude-snippet.min.js

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

amplitude.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3486,6 +3486,7 @@ var type = require('./type');
34863486
*/
34873487

34883488
var AMP_OP_ADD = '$add';
3489+
var AMP_OP_APPEND = '$append';
34893490
var AMP_OP_SET = '$set';
34903491
var AMP_OP_SET_ONCE = '$setOnce';
34913492
var AMP_OP_UNSET = '$unset';
@@ -3508,6 +3509,11 @@ Identify.prototype.add = function(property, value) {
35083509
return this;
35093510
};
35103511

3512+
Identify.prototype.append = function(property, value) {
3513+
this._addOperation(AMP_OP_APPEND, property, value);
3514+
return this;
3515+
};
3516+
35113517
Identify.prototype.set = function(property, value) {
35123518
this._addOperation(AMP_OP_SET, property, value);
35133519
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', 'set', 'setOnce', 'unset'];
16+
var identifyFuncs = ['add', 'append', 'set', 'setOnce', 'unset'];
1717
for (var i = 0; i < identifyFuncs.length; i++) {
1818
proxyIdentify(identifyFuncs[i]);
1919
}

src/identify.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var type = require('./type');
77
*/
88

99
var AMP_OP_ADD = '$add';
10+
var AMP_OP_APPEND = '$append';
1011
var AMP_OP_SET = '$set';
1112
var AMP_OP_SET_ONCE = '$setOnce';
1213
var AMP_OP_UNSET = '$unset';
@@ -29,6 +30,11 @@ Identify.prototype.add = function(property, value) {
2930
return this;
3031
};
3132

33+
Identify.prototype.append = function(property, value) {
34+
this._addOperation(AMP_OP_APPEND, property, value);
35+
return this;
36+
};
37+
3238
Identify.prototype.set = function(property, value) {
3339
this._addOperation(AMP_OP_SET, property, value);
3440
return this;

test/browser/amplitudejs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
this._q.push([fn].concat(Array.prototype.slice.call(arguments, 0))); return this;
1717
};
1818
}
19-
var identifyFuncs = ['add', 'set', 'setOnce', 'unset'];
19+
var identifyFuncs = ['add', 'append', 'set', 'setOnce', 'unset'];
2020
for (var i = 0; i < identifyFuncs.length; i++) {
2121
proxyIdentify(identifyFuncs[i]);
2222
}

test/identify.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,41 @@ describe('Identify', function() {
106106
assert.deepEqual([property1, property2], identify.properties);
107107
});
108108

109+
it ('should append properties', function () {
110+
var property1 = 'var value';
111+
var value1 = 'testValue';
112+
113+
var property2 = 'float value';
114+
var value2 = 0.123;
115+
116+
var property3 = 'bool value';
117+
var value3 = true;
118+
119+
var property4 = 'json value';
120+
var value4 = {};
121+
122+
var property5 = 'list value';
123+
var value5 = [1, 2, 'test'];
124+
125+
var identify = new Identify().append(property1, value1).append(property2, value2);
126+
identify.append(property3, value3).append(property4, value4).append(property5, value5);
127+
128+
// identify should ignore this since duplicate key
129+
identify.setOnce(property1, value3);
130+
131+
var expected = {
132+
'$append': {}
133+
}
134+
expected['$append'][property1] = value1;
135+
expected['$append'][property2] = value2;
136+
expected['$append'][property3] = value3;
137+
expected['$append'][property4] = value4;
138+
expected['$append'][property5] = value5;
139+
140+
assert.deepEqual(expected, identify.userPropertiesOperations);
141+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
142+
});
143+
109144
it ('should allow multiple operations', function () {
110145
var property1 = 'string value';
111146
var value1 = 'testValue';
@@ -118,14 +153,18 @@ describe('Identify', function() {
118153

119154
var property4 = 'json value';
120155

156+
var property5 = 'list value';
157+
var value5 = [1, 2, 'test'];
158+
121159
var identify = new Identify().setOnce(property1, value1).add(property2, value2);
122-
identify.set(property3, value3).unset(property4);
160+
identify.set(property3, value3).unset(property4).append(property5, value5);
123161

124162
// identify should ignore this since duplicate key
125163
identify.set(property4, value3);
126164

127165
var expected = {
128166
'$add': {},
167+
'$append': {},
129168
'$set': {},
130169
'$setOnce': {},
131170
'$unset': {}
@@ -134,9 +173,10 @@ describe('Identify', function() {
134173
expected['$add'][property2] = value2;
135174
expected['$set'][property3] = value3;
136175
expected['$unset'][property4] = '-';
176+
expected['$append'][property5] = value5;
137177

138178
assert.deepEqual(expected, identify.userPropertiesOperations);
139-
assert.deepEqual([property1, property2, property3, property4], identify.properties);
179+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
140180
});
141181

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

0 commit comments

Comments
 (0)