Skip to content

Commit 5e7fab9

Browse files
committed
Merge pull request #57 from amplitude/add_callback_identify_setuserproperties
add callback support for identify calls
2 parents 4823fb6 + e6d4159 commit 5e7fab9

File tree

7 files changed

+103
-10
lines changed

7 files changed

+103
-10
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,19 @@ 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-
### Log Event Callbacks and Redirects ###
242-
You can pass a callback function to logEvent, 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-
The status and response 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:
248+
```javascript
249+
var identify = new amplitude.Identify().set('key', 'value');
250+
amplitude.identify(identify, callback_function);
251+
```
252+
253+
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
251256
var callback_function = function(status, response) {

amplitude.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,11 @@ Amplitude.prototype.clearUserProperties = function(){
599599
this.identify(identify);
600600
};
601601

602-
Amplitude.prototype.identify = function(identify) {
602+
Amplitude.prototype.identify = function(identify, callback) {
603603
if (!this._apiKeySet('identify()')) {
604+
if (callback && type(callback) === 'function') {
605+
callback(0, 'No request sent');
606+
}
604607
return;
605608
}
606609

@@ -617,7 +620,9 @@ Amplitude.prototype.identify = function(identify) {
617620
}
618621

619622
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
620-
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
623+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, callback);
624+
} else if (callback && type(callback) === 'function') {
625+
callback(0, 'No request sent');
621626
}
622627
};
623628

@@ -751,6 +756,9 @@ Amplitude.prototype._limitEventsQueued = function(queue) {
751756

752757
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
753758
if (!this._apiKeySet('logEvent()')) {
759+
if (callback && type(callback) === 'function') {
760+
callback(0, 'No request sent');
761+
}
754762
return -1;
755763
}
756764
return this._logEvent(eventType, eventProperties, null, null, callback);
@@ -804,6 +812,9 @@ Amplitude.prototype.removeEvents = function (maxEventId, maxIdentifyId) {
804812

805813
Amplitude.prototype.sendEvents = function(callback) {
806814
if (!this._apiKeySet('sendEvents()')) {
815+
if (callback && type(callback) === 'function') {
816+
callback(0, 'No request sent');
817+
}
807818
return;
808819
}
809820

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,11 @@ Amplitude.prototype.clearUserProperties = function(){
493493
this.identify(identify);
494494
};
495495

496-
Amplitude.prototype.identify = function(identify) {
496+
Amplitude.prototype.identify = function(identify, callback) {
497497
if (!this._apiKeySet('identify()')) {
498+
if (callback && type(callback) === 'function') {
499+
callback(0, 'No request sent');
500+
}
498501
return;
499502
}
500503

@@ -511,7 +514,9 @@ Amplitude.prototype.identify = function(identify) {
511514
}
512515

513516
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
514-
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
517+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, callback);
518+
} else if (callback && type(callback) === 'function') {
519+
callback(0, 'No request sent');
515520
}
516521
};
517522

@@ -645,6 +650,9 @@ Amplitude.prototype._limitEventsQueued = function(queue) {
645650

646651
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
647652
if (!this._apiKeySet('logEvent()')) {
653+
if (callback && type(callback) === 'function') {
654+
callback(0, 'No request sent');
655+
}
648656
return -1;
649657
}
650658
return this._logEvent(eventType, eventProperties, null, null, callback);
@@ -698,6 +706,9 @@ Amplitude.prototype.removeEvents = function (maxEventId, maxIdentifyId) {
698706

699707
Amplitude.prototype.sendEvents = function(callback) {
700708
if (!this._apiKeySet('sendEvents()')) {
709+
if (callback && type(callback) === 'function') {
710+
callback(0, 'No request sent');
711+
}
701712
return;
702713
}
703714

test/amplitude.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,67 @@ describe('Amplitude', function() {
670670
'$set': {'key4': 'value5'}
671671
});
672672
});
673+
674+
it('should run the callback after making the identify call', function() {
675+
var counter = 0;
676+
var value = -1;
677+
var message = '';
678+
var callback = function (status, response) {
679+
counter++;
680+
value = status;
681+
message = response;
682+
}
683+
var identify = new amplitude.Identify().set('key', 'value');
684+
amplitude.identify(identify, callback);
685+
686+
// before server responds, callback should not fire
687+
assert.lengthOf(server.requests, 1);
688+
assert.equal(counter, 0);
689+
assert.equal(value, -1);
690+
assert.equal(message, '');
691+
692+
// after server response, fire callback
693+
server.respondWith('success');
694+
server.respond();
695+
assert.equal(counter, 1);
696+
assert.equal(value, 200);
697+
assert.equal(message, 'success');
698+
});
699+
700+
it('should run the callback even if client not initialized with apiKey', function() {
701+
var counter = 0;
702+
var value = -1;
703+
var message = '';
704+
var callback = function (status, response) {
705+
counter++;
706+
value = status;
707+
message = response;
708+
}
709+
var identify = new amplitude.Identify().set('key', 'value');
710+
new Amplitude().identify(identify, callback);
711+
712+
// verify callback fired
713+
assert.equal(counter, 1);
714+
assert.equal(value, 0);
715+
assert.equal(message, 'No request sent');
716+
});
717+
718+
it('should run the callback even with an invalid identify object', function() {
719+
var counter = 0;
720+
var value = -1;
721+
var message = '';
722+
var callback = function (status, response) {
723+
counter++;
724+
value = status;
725+
message = response;
726+
}
727+
amplitude.identify(null, callback);
728+
729+
// verify callback fired
730+
assert.equal(counter, 1);
731+
assert.equal(value, 0);
732+
assert.equal(message, 'No request sent');
733+
});
673734
});
674735

675736
describe('logEvent', 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)