Skip to content

Commit c3aedbd

Browse files
committed
cleaning up
1 parent c4a0a0c commit c3aedbd

File tree

4 files changed

+126
-134
lines changed

4 files changed

+126
-134
lines changed

amplitude.js

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
172172
domain: this.options.domain
173173
});
174174
this.options.domain = this.cookieStorage.options().domain;
175-
176175
_upgradeCookeData(this);
177176
_loadCookieData(this);
178177

@@ -384,8 +383,7 @@ Amplitude.prototype._sendEventsIfReady = function _sendEventsIfReady(callback) {
384383
// otherwise schedule an upload after 30s
385384
if (!this._updateScheduled) { // make sure we only schedule 1 upload
386385
this._updateScheduled = true;
387-
setTimeout(
388-
function() {
386+
setTimeout(function() {
389387
this._updateScheduled = false;
390388
this.sendEvents();
391389
}.bind(this), this.options.eventUploadPeriodMillis
@@ -807,13 +805,9 @@ Amplitude.prototype.setVersionName = function setVersionName(versionName) {
807805
* @private
808806
*/
809807
Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, callback) {
810-
if (type(callback) !== 'function') {
811-
callback = null;
812-
}
813-
814808
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
815809
if (!eventType || this.options.optOut) {
816-
if (callback) {
810+
if (type(callback) === 'function') {
817811
callback(0, 'No request sent');
818812
}
819813
return;
@@ -834,9 +828,9 @@ Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, a
834828
this._lastEventTime = eventTime;
835829
_saveCookieData(this);
836830

837-
userProperties = (type(userProperties) === 'object' && userProperties) || {};
831+
userProperties = userProperties || {};
838832
apiProperties = apiProperties || {};
839-
eventProperties = (type(eventProperties) === 'object' && eventProperties) || {};
833+
eventProperties = eventProperties || {};
840834
var event = {
841835
device_id: this.options.deviceId,
842836
user_id: this.options.userId,
@@ -932,7 +926,7 @@ var _isNumber = function _isNumber(n) {
932926
*/
933927
Amplitude.prototype.logRevenue = function logRevenue(price, quantity, product) {
934928
// Test that the parameters are of the right type.
935-
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
929+
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || (quantity !== undefined && !_isNumber(quantity))) {
936930
// utils.log('Price and quantity arguments to logRevenue must be numbers');
937931
return -1;
938932
}
@@ -981,73 +975,68 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) {
981975
* Note the server response code and response body are passed to the callback as input arguments.
982976
*/
983977
Amplitude.prototype.sendEvents = function sendEvents(callback) {
984-
if (!this._apiKeySet('sendEvents()')) {
978+
if (!this._apiKeySet('sendEvents()') || this._sending || this.options.optOut || this._unsentCount() === 0) {
985979
if (type(callback) === 'function') {
986980
callback(0, 'No request sent');
987981
}
988982
return;
989983
}
990984

991-
if (!this._sending && !this.options.optOut && this._unsentCount() > 0) {
992-
this._sending = true;
993-
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' +
994-
this.options.apiEndpoint + '/';
995-
996-
// fetch events to send
997-
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);
998-
var mergedEvents = this._mergeEventsAndIdentifys(numEvents);
999-
var maxEventId = mergedEvents.maxEventId;
1000-
var maxIdentifyId = mergedEvents.maxIdentifyId;
1001-
var events = JSON.stringify(mergedEvents.eventsToSend);
1002-
var uploadTime = new Date().getTime();
1003-
1004-
var data = {
1005-
client: this.options.apiKey,
1006-
e: events,
1007-
v: Constants.API_VERSION,
1008-
upload_time: uploadTime,
1009-
checksum: md5(Constants.API_VERSION + this.options.apiKey + events + uploadTime)
1010-
};
985+
this._sending = true;
986+
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' + this.options.apiEndpoint + '/';
987+
988+
// fetch events to send
989+
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);
990+
var mergedEvents = this._mergeEventsAndIdentifys(numEvents);
991+
var maxEventId = mergedEvents.maxEventId;
992+
var maxIdentifyId = mergedEvents.maxIdentifyId;
993+
var events = JSON.stringify(mergedEvents.eventsToSend);
994+
var uploadTime = new Date().getTime();
995+
996+
var data = {
997+
client: this.options.apiKey,
998+
e: events,
999+
v: Constants.API_VERSION,
1000+
upload_time: uploadTime,
1001+
checksum: md5(Constants.API_VERSION + this.options.apiKey + events + uploadTime)
1002+
};
10111003

1012-
var scope = this;
1013-
new Request(url, data).send(function(status, response) {
1014-
scope._sending = false;
1015-
try {
1016-
if (status === 200 && response === 'success') {
1017-
scope.removeEvents(maxEventId, maxIdentifyId);
1004+
var scope = this;
1005+
new Request(url, data).send(function(status, response) {
1006+
scope._sending = false;
1007+
try {
1008+
if (status === 200 && response === 'success') {
1009+
scope.removeEvents(maxEventId, maxIdentifyId);
10181010

1019-
// Update the event cache after the removal of sent events.
1020-
if (scope.options.saveEvents) {
1021-
scope.saveEvents();
1022-
}
1011+
// Update the event cache after the removal of sent events.
1012+
if (scope.options.saveEvents) {
1013+
scope.saveEvents();
1014+
}
10231015

1024-
// Send more events if any queued during previous send.
1025-
if (!scope._sendEventsIfReady(callback) && type(callback) === 'function') {
1026-
callback(status, response);
1027-
}
1016+
// Send more events if any queued during previous send.
1017+
if (!scope._sendEventsIfReady(callback) && type(callback) === 'function') {
1018+
callback(status, response);
1019+
}
10281020

1029-
// handle payload too large
1030-
} else if (status === 413) {
1031-
// utils.log('request too large');
1032-
// Can't even get this one massive event through. Drop it, even if it is an identify.
1033-
if (scope.options.uploadBatchSize === 1) {
1034-
scope.removeEvents(maxEventId, maxIdentifyId);
1035-
}
1021+
// handle payload too large
1022+
} else if (status === 413) {
1023+
// utils.log('request too large');
1024+
// Can't even get this one massive event through. Drop it, even if it is an identify.
1025+
if (scope.options.uploadBatchSize === 1) {
1026+
scope.removeEvents(maxEventId, maxIdentifyId);
1027+
}
10361028

1037-
// The server complained about the length of the request. Backoff and try again.
1038-
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
1039-
scope.sendEvents(callback);
1029+
// The server complained about the length of the request. Backoff and try again.
1030+
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
1031+
scope.sendEvents(callback);
10401032

1041-
} else if (type(callback) === 'function') { // If server turns something like a 400
1042-
callback(status, response);
1043-
}
1044-
} catch (e) {
1045-
// utils.log('failed upload');
1033+
} else if (type(callback) === 'function') { // If server turns something like a 400
1034+
callback(status, response);
10461035
}
1047-
});
1048-
} else if (type(callback) === 'function') {
1049-
callback(0, 'No request sent');
1050-
}
1036+
} catch (e) {
1037+
// utils.log('failed upload');
1038+
}
1039+
});
10511040
};
10521041

10531042
/**

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: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
6666
domain: this.options.domain
6767
});
6868
this.options.domain = this.cookieStorage.options().domain;
69-
7069
_upgradeCookeData(this);
7170
_loadCookieData(this);
7271

@@ -278,8 +277,7 @@ Amplitude.prototype._sendEventsIfReady = function _sendEventsIfReady(callback) {
278277
// otherwise schedule an upload after 30s
279278
if (!this._updateScheduled) { // make sure we only schedule 1 upload
280279
this._updateScheduled = true;
281-
setTimeout(
282-
function() {
280+
setTimeout(function() {
283281
this._updateScheduled = false;
284282
this.sendEvents();
285283
}.bind(this), this.options.eventUploadPeriodMillis
@@ -701,13 +699,9 @@ Amplitude.prototype.setVersionName = function setVersionName(versionName) {
701699
* @private
702700
*/
703701
Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, callback) {
704-
if (type(callback) !== 'function') {
705-
callback = null;
706-
}
707-
708702
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
709703
if (!eventType || this.options.optOut) {
710-
if (callback) {
704+
if (type(callback) === 'function') {
711705
callback(0, 'No request sent');
712706
}
713707
return;
@@ -728,9 +722,9 @@ Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, a
728722
this._lastEventTime = eventTime;
729723
_saveCookieData(this);
730724

731-
userProperties = (type(userProperties) === 'object' && userProperties) || {};
725+
userProperties = userProperties || {};
732726
apiProperties = apiProperties || {};
733-
eventProperties = (type(eventProperties) === 'object' && eventProperties) || {};
727+
eventProperties = eventProperties || {};
734728
var event = {
735729
device_id: this.options.deviceId,
736730
user_id: this.options.userId,
@@ -826,7 +820,7 @@ var _isNumber = function _isNumber(n) {
826820
*/
827821
Amplitude.prototype.logRevenue = function logRevenue(price, quantity, product) {
828822
// Test that the parameters are of the right type.
829-
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || quantity !== undefined && !_isNumber(quantity)) {
823+
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || (quantity !== undefined && !_isNumber(quantity))) {
830824
// utils.log('Price and quantity arguments to logRevenue must be numbers');
831825
return -1;
832826
}
@@ -875,73 +869,68 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) {
875869
* Note the server response code and response body are passed to the callback as input arguments.
876870
*/
877871
Amplitude.prototype.sendEvents = function sendEvents(callback) {
878-
if (!this._apiKeySet('sendEvents()')) {
872+
if (!this._apiKeySet('sendEvents()') || this._sending || this.options.optOut || this._unsentCount() === 0) {
879873
if (type(callback) === 'function') {
880874
callback(0, 'No request sent');
881875
}
882876
return;
883877
}
884878

885-
if (!this._sending && !this.options.optOut && this._unsentCount() > 0) {
886-
this._sending = true;
887-
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' +
888-
this.options.apiEndpoint + '/';
889-
890-
// fetch events to send
891-
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);
892-
var mergedEvents = this._mergeEventsAndIdentifys(numEvents);
893-
var maxEventId = mergedEvents.maxEventId;
894-
var maxIdentifyId = mergedEvents.maxIdentifyId;
895-
var events = JSON.stringify(mergedEvents.eventsToSend);
896-
var uploadTime = new Date().getTime();
897-
898-
var data = {
899-
client: this.options.apiKey,
900-
e: events,
901-
v: Constants.API_VERSION,
902-
upload_time: uploadTime,
903-
checksum: md5(Constants.API_VERSION + this.options.apiKey + events + uploadTime)
904-
};
879+
this._sending = true;
880+
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' + this.options.apiEndpoint + '/';
905881

906-
var scope = this;
907-
new Request(url, data).send(function(status, response) {
908-
scope._sending = false;
909-
try {
910-
if (status === 200 && response === 'success') {
911-
scope.removeEvents(maxEventId, maxIdentifyId);
882+
// fetch events to send
883+
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);
884+
var mergedEvents = this._mergeEventsAndIdentifys(numEvents);
885+
var maxEventId = mergedEvents.maxEventId;
886+
var maxIdentifyId = mergedEvents.maxIdentifyId;
887+
var events = JSON.stringify(mergedEvents.eventsToSend);
888+
var uploadTime = new Date().getTime();
912889

913-
// Update the event cache after the removal of sent events.
914-
if (scope.options.saveEvents) {
915-
scope.saveEvents();
916-
}
917-
918-
// Send more events if any queued during previous send.
919-
if (!scope._sendEventsIfReady(callback) && type(callback) === 'function') {
920-
callback(status, response);
921-
}
922-
923-
// handle payload too large
924-
} else if (status === 413) {
925-
// utils.log('request too large');
926-
// Can't even get this one massive event through. Drop it, even if it is an identify.
927-
if (scope.options.uploadBatchSize === 1) {
928-
scope.removeEvents(maxEventId, maxIdentifyId);
929-
}
930-
931-
// The server complained about the length of the request. Backoff and try again.
932-
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
933-
scope.sendEvents(callback);
934-
935-
} else if (type(callback) === 'function') { // If server turns something like a 400
890+
var data = {
891+
client: this.options.apiKey,
892+
e: events,
893+
v: Constants.API_VERSION,
894+
upload_time: uploadTime,
895+
checksum: md5(Constants.API_VERSION + this.options.apiKey + events + uploadTime)
896+
};
897+
898+
var scope = this;
899+
new Request(url, data).send(function(status, response) {
900+
scope._sending = false;
901+
try {
902+
if (status === 200 && response === 'success') {
903+
scope.removeEvents(maxEventId, maxIdentifyId);
904+
905+
// Update the event cache after the removal of sent events.
906+
if (scope.options.saveEvents) {
907+
scope.saveEvents();
908+
}
909+
910+
// Send more events if any queued during previous send.
911+
if (!scope._sendEventsIfReady(callback) && type(callback) === 'function') {
936912
callback(status, response);
937913
}
938-
} catch (e) {
939-
// utils.log('failed upload');
914+
915+
// handle payload too large
916+
} else if (status === 413) {
917+
// utils.log('request too large');
918+
// Can't even get this one massive event through. Drop it, even if it is an identify.
919+
if (scope.options.uploadBatchSize === 1) {
920+
scope.removeEvents(maxEventId, maxIdentifyId);
921+
}
922+
923+
// The server complained about the length of the request. Backoff and try again.
924+
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
925+
scope.sendEvents(callback);
926+
927+
} else if (type(callback) === 'function') { // If server turns something like a 400
928+
callback(status, response);
940929
}
941-
});
942-
} else if (type(callback) === 'function') {
943-
callback(0, 'No request sent');
944-
}
930+
} catch (e) {
931+
// utils.log('failed upload');
932+
}
933+
});
945934
};
946935

947936
/**

test/amplitude.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ describe('Amplitude', function() {
4545
reset();
4646
});
4747

48+
it('fails on invalid apiKeys', function() {
49+
amplitude.init(null);
50+
assert.equal(amplitude.options.apiKey, undefined);
51+
assert.equal(amplitude.options.deviceId, undefined);
52+
53+
amplitude.init('');
54+
assert.equal(amplitude.options.apiKey, undefined);
55+
assert.equal(amplitude.options.deviceId, undefined);
56+
57+
amplitude.init(apiKey);
58+
assert.equal(amplitude.options.apiKey, apiKey);
59+
assert.lengthOf(amplitude.options.deviceId, 37);
60+
});
61+
4862
it('should accept userId', function() {
4963
amplitude.init(apiKey, userId);
5064
assert.equal(amplitude.options.userId, userId);

0 commit comments

Comments
 (0)