Skip to content

Commit 06b1ab4

Browse files
committed
updated readme and added tests for all callback cases
1 parent e632cf0 commit 06b1ab4

File tree

5 files changed

+269
-21
lines changed

5 files changed

+269
-21
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ 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, 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:
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+
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:
249249

250250
```javascript
251251
var callback_function = function(status, response) {

amplitude.js

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

577-
Amplitude.prototype.setUserProperties = function(userProperties) {
577+
Amplitude.prototype.setUserProperties = function(userProperties, callback) {
578578
if (!this._apiKeySet('setUserProperties()')) {
579+
if (callback && type(callback) === 'function') {
580+
callback(0, 'No request sent');
581+
}
579582
return;
580583
}
581584
// convert userProperties into an identify call
@@ -585,22 +588,28 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
585588
identify.set(property, userProperties[property]);
586589
}
587590
}
588-
this.identify(identify);
591+
this.identify(identify, callback);
589592
};
590593

591594
// Clearing user properties is irreversible!
592-
Amplitude.prototype.clearUserProperties = function(){
595+
Amplitude.prototype.clearUserProperties = function(callback){
593596
if (!this._apiKeySet('clearUserProperties()')) {
597+
if (callback && type(callback) === 'function') {
598+
callback(0, 'No request sent');
599+
}
594600
return;
595601
}
596602

597603
var identify = new Identify();
598604
identify.clearAll();
599-
this.identify(identify);
605+
this.identify(identify, callback);
600606
};
601607

602-
Amplitude.prototype.identify = function(identify) {
608+
Amplitude.prototype.identify = function(identify, callback) {
603609
if (!this._apiKeySet('identify()')) {
610+
if (callback && type(callback) === 'function') {
611+
callback(0, 'No request sent');
612+
}
604613
return;
605614
}
606615

@@ -617,7 +626,9 @@ Amplitude.prototype.identify = function(identify) {
617626
}
618627

619628
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
620-
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
629+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, callback);
630+
} else if (callback && type(callback) === 'function') {
631+
callback(0, 'No request sent');
621632
}
622633
};
623634

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

752763
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
753764
if (!this._apiKeySet('logEvent()')) {
765+
if (callback && type(callback) === 'function') {
766+
callback(0, 'No request sent');
767+
}
754768
return -1;
755769
}
756770
return this._logEvent(eventType, eventProperties, null, null, callback);
@@ -761,10 +775,13 @@ var _isNumber = function(n) {
761775
return !isNaN(parseFloat(n)) && isFinite(n);
762776
};
763777

764-
Amplitude.prototype.logRevenue = function(price, quantity, product) {
778+
Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
765779
// Test that the parameters are of the right type.
766780
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
767781
// utils.log('Price and quantity arguments to logRevenue must be numbers');
782+
if (callback && type(callback) === 'function') {
783+
callback(0, 'No request sent');
784+
}
768785
return -1;
769786
}
770787

@@ -773,7 +790,7 @@ Amplitude.prototype.logRevenue = function(price, quantity, product) {
773790
special: 'revenue_amount',
774791
quantity: quantity || 1,
775792
price: price
776-
});
793+
}, null, callback);
777794
};
778795

779796
/**
@@ -804,6 +821,9 @@ Amplitude.prototype.removeEvents = function (maxEventId, maxIdentifyId) {
804821

805822
Amplitude.prototype.sendEvents = function(callback) {
806823
if (!this._apiKeySet('sendEvents()')) {
824+
if (callback && type(callback) === 'function') {
825+
callback(0, 'No request sent');
826+
}
807827
return;
808828
}
809829

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: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,11 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
468468
}
469469
};
470470

471-
Amplitude.prototype.setUserProperties = function(userProperties) {
471+
Amplitude.prototype.setUserProperties = function(userProperties, callback) {
472472
if (!this._apiKeySet('setUserProperties()')) {
473+
if (callback && type(callback) === 'function') {
474+
callback(0, 'No request sent');
475+
}
473476
return;
474477
}
475478
// convert userProperties into an identify call
@@ -479,22 +482,28 @@ Amplitude.prototype.setUserProperties = function(userProperties) {
479482
identify.set(property, userProperties[property]);
480483
}
481484
}
482-
this.identify(identify);
485+
this.identify(identify, callback);
483486
};
484487

485488
// Clearing user properties is irreversible!
486-
Amplitude.prototype.clearUserProperties = function(){
489+
Amplitude.prototype.clearUserProperties = function(callback){
487490
if (!this._apiKeySet('clearUserProperties()')) {
491+
if (callback && type(callback) === 'function') {
492+
callback(0, 'No request sent');
493+
}
488494
return;
489495
}
490496

491497
var identify = new Identify();
492498
identify.clearAll();
493-
this.identify(identify);
499+
this.identify(identify, callback);
494500
};
495501

496-
Amplitude.prototype.identify = function(identify) {
502+
Amplitude.prototype.identify = function(identify, callback) {
497503
if (!this._apiKeySet('identify()')) {
504+
if (callback && type(callback) === 'function') {
505+
callback(0, 'No request sent');
506+
}
498507
return;
499508
}
500509

@@ -511,7 +520,9 @@ Amplitude.prototype.identify = function(identify) {
511520
}
512521

513522
if (identify instanceof Identify && Object.keys(identify.userPropertiesOperations).length > 0) {
514-
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
523+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, callback);
524+
} else if (callback && type(callback) === 'function') {
525+
callback(0, 'No request sent');
515526
}
516527
};
517528

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

646657
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
647658
if (!this._apiKeySet('logEvent()')) {
659+
if (callback && type(callback) === 'function') {
660+
callback(0, 'No request sent');
661+
}
648662
return -1;
649663
}
650664
return this._logEvent(eventType, eventProperties, null, null, callback);
@@ -655,10 +669,13 @@ var _isNumber = function(n) {
655669
return !isNaN(parseFloat(n)) && isFinite(n);
656670
};
657671

658-
Amplitude.prototype.logRevenue = function(price, quantity, product) {
672+
Amplitude.prototype.logRevenue = function(price, quantity, product, callback) {
659673
// Test that the parameters are of the right type.
660674
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
661675
// utils.log('Price and quantity arguments to logRevenue must be numbers');
676+
if (callback && type(callback) === 'function') {
677+
callback(0, 'No request sent');
678+
}
662679
return -1;
663680
}
664681

@@ -667,7 +684,7 @@ Amplitude.prototype.logRevenue = function(price, quantity, product) {
667684
special: 'revenue_amount',
668685
quantity: quantity || 1,
669686
price: price
670-
});
687+
}, null, callback);
671688
};
672689

673690
/**
@@ -698,6 +715,9 @@ Amplitude.prototype.removeEvents = function (maxEventId, maxIdentifyId) {
698715

699716
Amplitude.prototype.sendEvents = function(callback) {
700717
if (!this._apiKeySet('sendEvents()')) {
718+
if (callback && type(callback) === 'function') {
719+
callback(0, 'No request sent');
720+
}
701721
return;
702722
}
703723

0 commit comments

Comments
 (0)