Skip to content

Commit 7e9e992

Browse files
authored
feat(identify): add preInsert, postInsert, remove to Identify and gro… (#372)
* feat(identify): add preInsert, postInsert, remove to Identify and groupIdentify * docs improvement
1 parent ebe25d4 commit 7e9e992

File tree

3 files changed

+180
-3
lines changed

3 files changed

+180
-3
lines changed

src/amplitude-snippet.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@
3131
this._q = [];
3232
return this;
3333
};
34-
var identifyFuncs = ['add', 'append', 'clearAll', 'prepend', 'set', 'setOnce', 'unset'];
34+
var identifyFuncs = [
35+
'add',
36+
'append',
37+
'clearAll',
38+
'prepend',
39+
'set',
40+
'setOnce',
41+
'unset',
42+
'preInsert',
43+
'postInsert',
44+
'remove',
45+
];
3546
for (var i = 0; i < identifyFuncs.length; i++) {
3647
proxy(Identify, identifyFuncs[i]);
3748
}

src/identify.js

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ var AMP_OP_PREPEND = '$prepend';
1414
var AMP_OP_SET = '$set';
1515
var AMP_OP_SET_ONCE = '$setOnce';
1616
var AMP_OP_UNSET = '$unset';
17+
var AMP_OP_PREINSERT = '$preInsert';
18+
var AMP_OP_POSTINSERT = '$postInsert';
19+
var AMP_OP_REMOVE = '$remove';
1720

1821
/**
1922
* Identify API - instance constructor. Identify objects are a wrapper for user property operations.
@@ -156,6 +159,45 @@ Identify.prototype.unset = function (property) {
156159
return this;
157160
};
158161

162+
/**
163+
* Preinsert a value or values to a user property, if it does not exist in the user property already.
164+
* Preinsert means inserting the value or values to the beginning of the specified user property.
165+
* If the item already exists in the user property, it will be a no-op.
166+
* @public
167+
* @param {string} property - The user property key.
168+
* @param {number|string|list|object} value - A value or values to insert.
169+
* @returns {Identify} Returns the same Identify object, allowing you to chain multiple method calls together.
170+
*/
171+
Identify.prototype.preInsert = function (property, value) {
172+
this._addOperation(AMP_OP_PREINSERT, property, value);
173+
return this;
174+
};
175+
176+
/**
177+
* Postinsert a value or values to a user property, if it does not exist in the user property already.
178+
* Postinsert means inserting the value or values to the beginning of the specified user property.
179+
* If the item already exists in the user property, it will be a no-op.
180+
* @param {string} property - The user property key.
181+
* @param {number|string|list|object} value - A value or values to insert.
182+
* @returns {Identify} Returns the same Identify object, allowing you to chain multiple method calls together.
183+
*/
184+
Identify.prototype.postInsert = function (property, value) {
185+
this._addOperation(AMP_OP_POSTINSERT, property, value);
186+
return this;
187+
};
188+
189+
/**
190+
* Remove a value or values to a user property, if it does exist in the user property.
191+
* If the item does not exist in the user property, it will be a no-op.
192+
* @param {string} property - The user property key.
193+
* @param {number|string|list|object} value - A value or values to remove.
194+
* @returns {Identify} Returns the same Identify object, allowing you to chain multiple method calls together.
195+
*/
196+
Identify.prototype.remove = function (property, value) {
197+
this._addOperation(AMP_OP_REMOVE, property, value);
198+
return this;
199+
};
200+
159201
/**
160202
* Helper function that adds operation to the Identify's object
161203
* Handle's filtering of duplicate user property keys, and filtering for clearAll.

test/identify.js

+126-2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,111 @@ describe('Identify', function () {
176176
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
177177
});
178178

179+
it('should preInsert properties', function () {
180+
var property1 = 'var value';
181+
var value1 = 'testValue';
182+
183+
var property2 = 'float value';
184+
var value2 = 0.123;
185+
186+
var property3 = 'bool value';
187+
var value3 = true;
188+
189+
var property4 = 'json value';
190+
var value4 = {};
191+
192+
var property5 = 'list value';
193+
var value5 = [1, 2, 'test'];
194+
195+
var identify = new Identify().preInsert(property1, value1).preInsert(property2, value2);
196+
identify.preInsert(property3, value3).preInsert(property4, value4).preInsert(property5, value5);
197+
198+
// identify should ignore this since duplicate key
199+
identify.setOnce(property1, value3);
200+
201+
var expected = {
202+
$preInsert: {},
203+
};
204+
expected['$preInsert'][property1] = value1;
205+
expected['$preInsert'][property2] = value2;
206+
expected['$preInsert'][property3] = value3;
207+
expected['$preInsert'][property4] = value4;
208+
expected['$preInsert'][property5] = value5;
209+
210+
assert.deepEqual(expected, identify.userPropertiesOperations);
211+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
212+
});
213+
214+
it('should postInsert properties', function () {
215+
var property1 = 'var value';
216+
var value1 = 'testValue';
217+
218+
var property2 = 'float value';
219+
var value2 = 0.123;
220+
221+
var property3 = 'bool value';
222+
var value3 = true;
223+
224+
var property4 = 'json value';
225+
var value4 = {};
226+
227+
var property5 = 'list value';
228+
var value5 = [1, 2, 'test'];
229+
230+
var identify = new Identify().postInsert(property1, value1).postInsert(property2, value2);
231+
identify.postInsert(property3, value3).postInsert(property4, value4).postInsert(property5, value5);
232+
233+
// identify should ignore this since duplicate key
234+
identify.setOnce(property1, value3);
235+
236+
var expected = {
237+
$postInsert: {},
238+
};
239+
expected['$postInsert'][property1] = value1;
240+
expected['$postInsert'][property2] = value2;
241+
expected['$postInsert'][property3] = value3;
242+
expected['$postInsert'][property4] = value4;
243+
expected['$postInsert'][property5] = value5;
244+
245+
assert.deepEqual(expected, identify.userPropertiesOperations);
246+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
247+
});
248+
249+
it('should remove properties', function () {
250+
var property1 = 'var value';
251+
var value1 = 'testValue';
252+
253+
var property2 = 'float value';
254+
var value2 = 0.123;
255+
256+
var property3 = 'bool value';
257+
var value3 = true;
258+
259+
var property4 = 'json value';
260+
var value4 = {};
261+
262+
var property5 = 'list value';
263+
var value5 = [1, 2, 'test'];
264+
265+
var identify = new Identify().remove(property1, value1).remove(property2, value2);
266+
identify.remove(property3, value3).remove(property4, value4).remove(property5, value5);
267+
268+
// identify should ignore this since duplicate key
269+
identify.setOnce(property1, value3);
270+
271+
var expected = {
272+
$remove: {},
273+
};
274+
expected['$remove'][property1] = value1;
275+
expected['$remove'][property2] = value2;
276+
expected['$remove'][property3] = value3;
277+
expected['$remove'][property4] = value4;
278+
expected['$remove'][property5] = value5;
279+
280+
assert.deepEqual(expected, identify.userPropertiesOperations);
281+
assert.deepEqual([property1, property2, property3, property4, property5], identify.properties);
282+
});
283+
179284
it('should allow multiple operations', function () {
180285
var property1 = 'string value';
181286
var value1 = 'testValue';
@@ -194,9 +299,20 @@ describe('Identify', function () {
194299
var property6 = 'int value';
195300
var value6 = 100;
196301

302+
var property7 = 'string value2';
303+
var value7 = 'testValue2';
304+
305+
var property8 = 'fload value2';
306+
var value8 = 0.456;
307+
308+
var property9 = 'bool value2';
309+
var value9 = false;
310+
197311
var identify = new Identify().setOnce(property1, value1).add(property2, value2);
198312
identify.set(property3, value3).unset(property4).append(property5, value5);
199313
identify.prepend(property6, value6);
314+
identify.preInsert(property7, value7).postInsert(property8, value8);
315+
identify.remove(property9, value9);
200316

201317
// identify should ignore this since duplicate key
202318
identify.set(property4, value3);
@@ -208,16 +324,24 @@ describe('Identify', function () {
208324
$set: {},
209325
$setOnce: {},
210326
$unset: {},
327+
$preInsert: {},
328+
$postInsert: {},
329+
$remove: {},
211330
};
212331
expected['$setOnce'][property1] = value1;
213332
expected['$add'][property2] = value2;
214333
expected['$set'][property3] = value3;
215334
expected['$unset'][property4] = '-';
216335
expected['$append'][property5] = value5;
217336
expected['$prepend'][property6] = value6;
218-
337+
expected['$preInsert'][property7] = value7;
338+
expected['$postInsert'][property8] = value8;
339+
expected['$remove'][property9] = value9;
219340
assert.deepEqual(expected, identify.userPropertiesOperations);
220-
assert.deepEqual([property1, property2, property3, property4, property5, property6], identify.properties);
341+
assert.deepEqual(
342+
[property1, property2, property3, property4, property5, property6, property7, property8, property9],
343+
identify.properties,
344+
);
221345
});
222346

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

0 commit comments

Comments
 (0)