Skip to content

Commit e6d4159

Browse files
committed
rollback callback for logRevenue, setUserProperties, clearUserProperties
1 parent 2a401fd commit e6d4159

File tree

7 files changed

+27
-182
lines changed

7 files changed

+27
-182
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 support for passing callback function to identify.
4+
35
### 2.9.1 (March 6, 2016)
46

57
* Fix bug where saveReferrer throws exception if sessionStorage is disabled.

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,18 @@ amplitude.setDeviceId('CUSTOM_DEVICE_ID');
238238

239239
**Note: this is not recommended unless you really know what you are doing** (like if you have your own system for tracking user devices). Make sure the deviceId you set is sufficiently unique (we recommend something like a UUID - see `src/uuid.js` for an example of how to generate) to prevent conflicts with other devices in our system.
240240

241-
### Callbacks for LogEvent, LogRevenue, Identify, SetUserProperties, ClearUserProperties, and How to Redirect ###
242-
You can pass a callback function to logEvent, identify, setUserProperties, and clearUserProperties, which will get called after receiving a response from the server:
241+
### Callbacks for LogEvent, Identify, and Redirect ###
242+
You can pass a callback function to logEvent and identify, which will get called after receiving a response from the server:
243243

244244
```javascript
245245
amplitude.logEvent("EVENT_IDENTIFIER_HERE", null, callback_function);
246246
```
247247

248+
```javascript
249+
var identify = new amplitude.Identify().set('key', 'value');
250+
amplitude.identify(identify, callback_function);
251+
```
252+
248253
The status and response body from the server are passed to the callback function, which you might find useful. An example of a callback function which redirects the browser to another site after a response:
249254

250255
```javascript

amplitude.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,8 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
574574
}
575575
};
576576

577-
Amplitude.prototype.setUserProperties = function(userProperties, callback) {
577+
Amplitude.prototype.setUserProperties = function(userProperties) {
578578
if (!this._apiKeySet('setUserProperties()')) {
579-
if (callback && type(callback) === 'function') {
580-
callback(0, 'No request sent');
581-
}
582579
return;
583580
}
584581
// convert userProperties into an identify call
@@ -588,21 +585,18 @@ Amplitude.prototype.setUserProperties = function(userProperties, callback) {
588585
identify.set(property, userProperties[property]);
589586
}
590587
}
591-
this.identify(identify, callback);
588+
this.identify(identify);
592589
};
593590

594591
// Clearing user properties is irreversible!
595-
Amplitude.prototype.clearUserProperties = function(callback){
592+
Amplitude.prototype.clearUserProperties = function(){
596593
if (!this._apiKeySet('clearUserProperties()')) {
597-
if (callback && type(callback) === 'function') {
598-
callback(0, 'No request sent');
599-
}
600594
return;
601595
}
602596

603597
var identify = new Identify();
604598
identify.clearAll();
605-
this.identify(identify, callback);
599+
this.identify(identify);
606600
};
607601

608602
Amplitude.prototype.identify = function(identify, callback) {
@@ -775,13 +769,10 @@ var _isNumber = function(n) {
775769
return !isNaN(parseFloat(n)) && isFinite(n);
776770
};
777771

778-
Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
772+
Amplitude.prototype.logRevenue = function(price, quantity, product) {
779773
// Test that the parameters are of the right type.
780774
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
781775
// utils.log('Price and quantity arguments to logRevenue must be numbers');
782-
if (callback && type(callback) === 'function') {
783-
callback(0, 'No request sent');
784-
}
785776
return -1;
786777
}
787778

@@ -790,7 +781,7 @@ Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
790781
special: 'revenue_amount',
791782
quantity: quantity || 1,
792783
price: price
793-
}, null, callback);
784+
});
794785
};
795786

796787
/**

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.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,8 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
468468
}
469469
};
470470

471-
Amplitude.prototype.setUserProperties = function(userProperties, callback) {
471+
Amplitude.prototype.setUserProperties = function(userProperties) {
472472
if (!this._apiKeySet('setUserProperties()')) {
473-
if (callback && type(callback) === 'function') {
474-
callback(0, 'No request sent');
475-
}
476473
return;
477474
}
478475
// convert userProperties into an identify call
@@ -482,21 +479,18 @@ Amplitude.prototype.setUserProperties = function(userProperties, callback) {
482479
identify.set(property, userProperties[property]);
483480
}
484481
}
485-
this.identify(identify, callback);
482+
this.identify(identify);
486483
};
487484

488485
// Clearing user properties is irreversible!
489-
Amplitude.prototype.clearUserProperties = function(callback){
486+
Amplitude.prototype.clearUserProperties = function(){
490487
if (!this._apiKeySet('clearUserProperties()')) {
491-
if (callback && type(callback) === 'function') {
492-
callback(0, 'No request sent');
493-
}
494488
return;
495489
}
496490

497491
var identify = new Identify();
498492
identify.clearAll();
499-
this.identify(identify, callback);
493+
this.identify(identify);
500494
};
501495

502496
Amplitude.prototype.identify = function(identify, callback) {
@@ -669,13 +663,10 @@ var _isNumber = function(n) {
669663
return !isNaN(parseFloat(n)) && isFinite(n);
670664
};
671665

672-
Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
666+
Amplitude.prototype.logRevenue = function(price, quantity, product) {
673667
// Test that the parameters are of the right type.
674668
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
675669
// utils.log('Price and quantity arguments to logRevenue must be numbers');
676-
if (callback && type(callback) === 'function') {
677-
callback(0, 'No request sent');
678-
}
679670
return -1;
680671
}
681672

@@ -684,7 +675,7 @@ Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
684675
special: 'revenue_amount',
685676
quantity: quantity || 1,
686677
price: price
687-
}, null, callback);
678+
});
688679
};
689680

690681
/**

test/amplitude.js

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -503,48 +503,6 @@ describe('Amplitude', function() {
503503
};
504504
assert.deepEqual(events[0].user_properties, expected);
505505
});
506-
507-
it('should run the callback after making the identify call', function() {
508-
var counter = 0;
509-
var value = -1;
510-
var message = '';
511-
var callback = function (status, response) {
512-
counter++;
513-
value = status;
514-
message = response;
515-
}
516-
amplitude.setUserProperties({'key': 'value'}, callback);
517-
518-
// before server responds, callback should not fire
519-
assert.lengthOf(server.requests, 1);
520-
assert.equal(counter, 0);
521-
assert.equal(value, -1);
522-
assert.equal(message, '');
523-
524-
// after server response, fire callback
525-
server.respondWith('success');
526-
server.respond();
527-
assert.equal(counter, 1);
528-
assert.equal(value, 200);
529-
assert.equal(message, 'success');
530-
});
531-
532-
it('should run the callback even if client not initialized with apiKey', function() {
533-
var counter = 0;
534-
var value = -1;
535-
var message = '';
536-
var callback = function (status, response) {
537-
counter++;
538-
value = status;
539-
message = response;
540-
}
541-
new Amplitude().setUserProperties({'key': 'value'}, callback);
542-
543-
// verify callback fired
544-
assert.equal(counter, 1);
545-
assert.equal(value, 0);
546-
assert.equal(message, 'No request sent');
547-
});
548506
});
549507

550508
describe('clearUserProperties', function() {
@@ -574,48 +532,6 @@ describe('Amplitude', function() {
574532
};
575533
assert.deepEqual(events[0].user_properties, expected);
576534
});
577-
578-
it('should run the callback after making the identify call', function() {
579-
var counter = 0;
580-
var value = -1;
581-
var message = '';
582-
var callback = function (status, response) {
583-
counter++;
584-
value = status;
585-
message = response;
586-
}
587-
amplitude.clearUserProperties(callback);
588-
589-
// before server responds, callback should not fire
590-
assert.lengthOf(server.requests, 1);
591-
assert.equal(counter, 0);
592-
assert.equal(value, -1);
593-
assert.equal(message, '');
594-
595-
// after server response, fire callback
596-
server.respondWith('success');
597-
server.respond();
598-
assert.equal(counter, 1);
599-
assert.equal(value, 200);
600-
assert.equal(message, 'success');
601-
});
602-
603-
it('should run the callback even if client not initialized with apiKey', function() {
604-
var counter = 0;
605-
var value = -1;
606-
var message = '';
607-
var callback = function (status, response) {
608-
counter++;
609-
value = status;
610-
message = response;
611-
}
612-
new Amplitude().clearUserProperties(callback);
613-
614-
// verify callback fired
615-
assert.equal(counter, 1);
616-
assert.equal(value, 0);
617-
assert.equal(message, 'No request sent');
618-
});
619535
});
620536

621537
describe('setDeviceId', function() {
@@ -2010,69 +1926,6 @@ describe('Amplitude', function() {
20101926
productId: 'chicken.dinner'
20111927
});
20121928
});
2013-
2014-
it('should run the callback logging the revenue event', function() {
2015-
var counter = 0;
2016-
var value = -1;
2017-
var message = '';
2018-
var callback = function (status, response) {
2019-
counter++;
2020-
value = status;
2021-
message = response;
2022-
}
2023-
amplitude.logRevenue(9.99, 1, 'product', callback);
2024-
2025-
// before server responds, callback should not fire
2026-
assert.lengthOf(server.requests, 1);
2027-
assert.equal(counter, 0);
2028-
assert.equal(value, -1);
2029-
assert.equal(message, '');
2030-
2031-
// after server response, fire callback
2032-
server.respondWith('success');
2033-
server.respond();
2034-
assert.equal(counter, 1);
2035-
assert.equal(value, 200);
2036-
assert.equal(message, 'success');
2037-
});
2038-
2039-
it('should run the callback even if client not initialized with apiKey', function() {
2040-
var counter = 0;
2041-
var value = -1;
2042-
var message = '';
2043-
var callback = function (status, response) {
2044-
counter++;
2045-
value = status;
2046-
message = response;
2047-
}
2048-
new Amplitude().logRevenue(9.99, 1, 'product', callback);
2049-
2050-
// verify callback fired
2051-
assert.equal(counter, 1);
2052-
assert.equal(value, 0);
2053-
assert.equal(message, 'No request sent');
2054-
});
2055-
2056-
it('should run the callback even with invalid revenue arguments', function() {
2057-
var counter = 0;
2058-
var value = -1;
2059-
var message = '';
2060-
var callback = function (status, response) {
2061-
counter++;
2062-
value = status;
2063-
message = response;
2064-
}
2065-
2066-
amplitude.logRevenue('three', 1, 'product', callback);
2067-
assert.equal(counter, 1);
2068-
assert.equal(value, 0);
2069-
assert.equal(message, 'No request sent');
2070-
2071-
amplitude.logRevenue(9.99, true, 'product', callback);
2072-
assert.equal(counter, 2);
2073-
assert.equal(value, 0);
2074-
assert.equal(message, 'No request sent');
2075-
});
20761929
});
20771930

20781931
describe('sessionId', function() {

test/browser/amplitudejs.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@
5959
};
6060
var setPhotoCount = function() {
6161
var photoCount = parseInt(prompt('Input photo count to set', '2'), 10);
62-
amplitude.identify(new amplitude.Identify().set('photoCount', photoCount));
62+
amplitude.identify(new amplitude.Identify().set('photoCount', photoCount), function() {alert('setPhotoCount!');});
6363
};
6464
var setOncePhotoCount = function() {
6565
var photoCount = parseInt(prompt('Input photo count to setOnce', '2'), 10);
6666
amplitude.identify(new amplitude.Identify().setOnce('photoCount', photoCount));
6767
};
68+
var logRevenue = function() {
69+
amplitude.logRevenue(9.99, 1, 'productA');
70+
}
6871
</script>
6972
<script>
7073
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true}, function() {

0 commit comments

Comments
 (0)