Skip to content

Commit fcec3f9

Browse files
committed
add clear all operation
1 parent 1d667c3 commit fcec3f9

13 files changed

+166
-32
lines changed

CHANGELOG.md

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

3+
* Add ability to clear all user properties.
4+
35
## 2.8.0 (December 15, 2015)
46

57
* Add getSessionId helper method to fetch the current sessionId.

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Amplitude-Javascript
99

1010
```
1111
<script type="text/javascript">
12-
(function(t,e){var n=t.amplitude||{};var r=e.createElement("script");r.type="text/javascript";
12+
(function(e,t){var n=e.amplitude||{};var r=t.createElement("script");r.type="text/javascript";
1313
r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.8.0-min.gz.js";
14-
r.onload=function(){t.amplitude.runQueuedFunctions()};var s=e.getElementsByTagName("script")[0];
15-
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function o(t){
16-
i.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
17-
return this}}var a=["add","append","set","setOnce","unset"];for(var u=0;u<a.length;u++){o(a[u]);
18-
}n.Identify=i;n._q=[];function c(t){n[t]=function(){n._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
19-
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify"];
20-
for(var p=0;p<l.length;p++){c(l[p])}t.amplitude=n})(window,document);
14+
r.onload=function(){e.amplitude.runQueuedFunctions()};var s=t.getElementsByTagName("script")[0];
15+
s.parentNode.insertBefore(r,s);var i=function(){this._q=[];return this};function a(e){
16+
i.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
17+
return this}}var o=["add","append","clearAll","set","setOnce","unset"];for(var c=0;c<o.length;c++){
18+
a(o[c])}n.Identify=i;n._q=[];function u(e){n[e]=function(){n._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
19+
}}var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
20+
for(var p=0;p<l.length;p++){u(l[p])}e.amplitude=n})(window,document);
2121
2222
amplitude.init("YOUR_API_KEY_HERE");
2323
</script>
@@ -142,6 +142,14 @@ var userProperties = {
142142
amplitude.setUserProperties(userProperties);
143143
```
144144

145+
### Clearing User Properties ###
146+
147+
You may use `clearUserProperties` to clear all user properties at once. Note: the result is irreversible!
148+
149+
```javascript
150+
amplitude.clearUserProperties();
151+
```
152+
145153
# Tracking Revenue #
146154

147155
To track revenue from a user, call

amplitude-segment-snippet.min.js

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

amplitude-snippet.min.js

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

amplitude.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,13 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
573573
this.identify(identify);
574574
};
575575

576+
// Clearing user properties is irreversible!
577+
Amplitude.prototype.clearUserProperties = function(){
578+
var identify = new Identify();
579+
identify.clearAll();
580+
this.identify(identify);
581+
};
582+
576583
Amplitude.prototype.identify = function(identify) {
577584

578585
if (type(identify) === 'object' && '_q' in identify) {
@@ -3522,6 +3529,7 @@ var type = require('./type');
35223529

35233530
var AMP_OP_ADD = '$add';
35243531
var AMP_OP_APPEND = '$append';
3532+
var AMP_OP_CLEAR_ALL = '$clearAll';
35253533
var AMP_OP_SET = '$set';
35263534
var AMP_OP_SET_ONCE = '$setOnce';
35273535
var AMP_OP_UNSET = '$unset';
@@ -3549,6 +3557,20 @@ Identify.prototype.append = function(property, value) {
35493557
return this;
35503558
};
35513559

3560+
// clearAll should be sent on its own Identify object
3561+
// If there are already other operations, then don't add clearAll
3562+
// If clearAll already in Identify, don't add other operations
3563+
Identify.prototype.clearAll = function() {
3564+
if (Object.keys(this.userPropertiesOperations).length > 0) {
3565+
if (!(AMP_OP_CLEAR_ALL in this.userPropertiesOperations)) {
3566+
log('Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll');
3567+
}
3568+
return this;
3569+
}
3570+
this.userPropertiesOperations[AMP_OP_CLEAR_ALL] = '-';
3571+
return this;
3572+
};
3573+
35523574
Identify.prototype.set = function(property, value) {
35533575
this._addOperation(AMP_OP_SET, property, value);
35543576
return this;
@@ -3565,6 +3587,12 @@ Identify.prototype.unset = function(property) {
35653587
};
35663588

35673589
Identify.prototype._addOperation = function(operation, property, value) {
3590+
// check that the identify doesn't already contain a clearAll
3591+
if (AMP_OP_CLEAR_ALL in this.userPropertiesOperations) {
3592+
log('This identify already contains a $clearAll operation, skipping operation ' + operation);
3593+
return;
3594+
}
3595+
35683596
// check that property wasn't already used in this Identify
35693597
if (this.properties.indexOf(property) !== -1) {
35703598
log('User property "' + property + '" already used in this identify, skipping operation ' + operation);

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-snippet.js

Lines changed: 2 additions & 2 deletions
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', 'set', 'setOnce', 'unset'];
16+
var identifyFuncs = ['add', 'append', 'clearAll', 'set', 'setOnce', 'unset'];
1717
for (var i = 0; i < identifyFuncs.length; i++) {
1818
proxyIdentify(identifyFuncs[i]);
1919
}
@@ -26,7 +26,7 @@
2626
}
2727
var funcs = ['init', 'logEvent', 'logRevenue', 'setUserId', 'setUserProperties',
2828
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
29-
'setGlobalUserProperties', 'identify'];
29+
'setGlobalUserProperties', 'identify', 'clearUserProperties'];
3030
for (var j = 0; j < funcs.length; j++) {
3131
proxy(funcs[j]);
3232
}

src/amplitude.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
467467
this.identify(identify);
468468
};
469469

470+
// Clearing user properties is irreversible!
471+
Amplitude.prototype.clearUserProperties = function(){
472+
var identify = new Identify();
473+
identify.clearAll();
474+
this.identify(identify);
475+
};
476+
470477
Amplitude.prototype.identify = function(identify) {
471478

472479
if (type(identify) === 'object' && '_q' in identify) {

src/identify.js

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

99
var AMP_OP_ADD = '$add';
1010
var AMP_OP_APPEND = '$append';
11+
var AMP_OP_CLEAR_ALL = '$clearAll';
1112
var AMP_OP_SET = '$set';
1213
var AMP_OP_SET_ONCE = '$setOnce';
1314
var AMP_OP_UNSET = '$unset';
@@ -35,6 +36,20 @@ Identify.prototype.append = function(property, value) {
3536
return this;
3637
};
3738

39+
// clearAll should be sent on its own Identify object
40+
// If there are already other operations, then don't add clearAll
41+
// If clearAll already in Identify, don't add other operations
42+
Identify.prototype.clearAll = function() {
43+
if (Object.keys(this.userPropertiesOperations).length > 0) {
44+
if (!(AMP_OP_CLEAR_ALL in this.userPropertiesOperations)) {
45+
log('Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll');
46+
}
47+
return this;
48+
}
49+
this.userPropertiesOperations[AMP_OP_CLEAR_ALL] = '-';
50+
return this;
51+
};
52+
3853
Identify.prototype.set = function(property, value) {
3954
this._addOperation(AMP_OP_SET, property, value);
4055
return this;
@@ -51,6 +66,12 @@ Identify.prototype.unset = function(property) {
5166
};
5267

5368
Identify.prototype._addOperation = function(operation, property, value) {
69+
// check that the identify doesn't already contain a clearAll
70+
if (AMP_OP_CLEAR_ALL in this.userPropertiesOperations) {
71+
log('This identify already contains a $clearAll operation, skipping operation ' + operation);
72+
return;
73+
}
74+
5475
// check that property wasn't already used in this Identify
5576
if (this.properties.indexOf(property) !== -1) {
5677
log('User property "' + property + '" already used in this identify, skipping operation ' + operation);

test/amplitude.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,35 @@ describe('Amplitude', function() {
332332
});
333333
});
334334

335+
describe('clearUserProperties', function() {
336+
beforeEach(function() {
337+
amplitude.init(apiKey);
338+
});
339+
340+
afterEach(function() {
341+
reset();
342+
});
343+
344+
it('should log identify call from clear user properties', function() {
345+
assert.equal(amplitude._unsentCount(), 0);
346+
amplitude.clearUserProperties();
347+
348+
assert.lengthOf(amplitude._unsentEvents, 0);
349+
assert.lengthOf(amplitude._unsentIdentifys, 1);
350+
assert.equal(amplitude._unsentCount(), 1);
351+
assert.lengthOf(server.requests, 1);
352+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
353+
assert.lengthOf(events, 1);
354+
assert.equal(events[0].event_type, '$identify');
355+
assert.deepEqual(events[0].event_properties, {});
356+
357+
var expected = {
358+
'$clearAll': '-'
359+
};
360+
assert.deepEqual(events[0].user_properties, expected);
361+
});
362+
});
363+
335364
describe('setDeviceId', function() {
336365

337366
afterEach(function() {

0 commit comments

Comments
 (0)