Skip to content

Commit b6d990f

Browse files
committed
log warning message for invalid option input type
1 parent c3aedbd commit b6d990f

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

amplitude.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ var DEFAULT_OPTIONS = require('./options');
127127
* @example var amplitude = new Amplitude();
128128
*/
129129
var Amplitude = function Amplitude() {
130-
this.cookieStorage = new cookieStorage().getStorage();
130+
this._unsentEvents = [];
131+
this._unsentIdentifys = [];
132+
this._ua = new UAParser(navigator.userAgent).getResult();
131133
this.options = object.merge({}, DEFAULT_OPTIONS);
134+
this.cookieStorage = new cookieStorage().getStorage();
132135
this._q = []; // queue for proxied functions before script load
133136
this._sending = false;
134-
this._ua = new UAParser(navigator.userAgent).getResult();
135-
this._unsentEvents = [];
136-
this._unsentIdentifys = [];
137137
this._updateScheduled = false;
138138

139139
// event meta data
@@ -172,6 +172,7 @@ 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+
175176
_upgradeCookeData(this);
176177
_loadCookieData(this);
177178

@@ -181,7 +182,6 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
181182
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
182183
this.options.userId || null;
183184

184-
// determine if starting new session
185185
var now = new Date().getTime();
186186
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
187187
this._newSession = true;
@@ -212,13 +212,12 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
212212
if (this.options.includeUtm) {
213213
this._initUtmData();
214214
}
215+
215216
if (this.options.includeReferrer) {
216217
this._saveReferrer(this._getReferrer());
217218
}
218-
219219
} catch (e) {
220220
utils.log(e);
221-
222221
} finally {
223222
if (type(opt_callback) === 'function') {
224223
opt_callback();
@@ -238,14 +237,15 @@ var _parseConfig = function _parseConfig(options, config) {
238237

239238
// verifies config value is defined, is the correct type, and some additional value verification
240239
var parseValidateLoad = function parseValidateLoad(key, expectedType) {
241-
if (type(config[key]) !== expectedType) {
240+
var input_value = config[key];
241+
if(input_value === undefined || !utils.validateInput(input_value, key + ' option', expectedType)) {
242242
return;
243243
}
244244
if (expectedType === 'boolean') {
245-
options[key] = !!config[key];
245+
options[key] = !!input_value;
246246
} else {
247-
options[key] = (expectedType === 'string' && !utils.isEmptyString(config[key]) && config[key]) ||
248-
(expectedType === 'number' && config[key] > 0 && config[key]) ||
247+
options[key] = (expectedType === 'string' && !utils.isEmptyString(input_value) && input_value) ||
248+
(expectedType === 'number' && config[key] > 0 && input_value) ||
249249
options[key];
250250
}
251251
};
@@ -717,7 +717,6 @@ Amplitude.prototype.setUserProperties = function setUserProperties(userPropertie
717717
if (!this._apiKeySet('setUserProperties()') || !utils.validateInput(userProperties, 'userProperties', 'object')) {
718718
return;
719719
}
720-
721720
// convert userProperties into an identify call
722721
var identify = new Identify();
723722
for (var property in userProperties) {
@@ -737,6 +736,7 @@ Amplitude.prototype.clearUserProperties = function clearUserProperties(){
737736
if (!this._apiKeySet('clearUserProperties()')) {
738737
return;
739738
}
739+
740740
var identify = new Identify();
741741
identify.clearAll();
742742
this.identify(identify);
@@ -2163,7 +2163,6 @@ function port (protocol){
21632163
var constants = require('./constants');
21642164
var type = require('./type');
21652165

2166-
21672166
var log = function log(s) {
21682167
try {
21692168
console.log('[Amplitude] ' + s);
@@ -2172,12 +2171,10 @@ var log = function log(s) {
21722171
}
21732172
};
21742173

2175-
21762174
var isEmptyString = function isEmptyString(str) {
21772175
return (!str || str.length === 0);
21782176
};
21792177

2180-
21812178
var sessionStorageEnabled = function sessionStorageEnabled() {
21822179
try {
21832180
if (window.sessionStorage) {
@@ -2187,7 +2184,6 @@ var sessionStorageEnabled = function sessionStorageEnabled() {
21872184
return false;
21882185
};
21892186

2190-
21912187
// truncate string values in event and user properties so that request size does not get too large
21922188
var truncate = function truncate(value) {
21932189
if (type(value) === 'array') {
@@ -2207,15 +2203,13 @@ var truncate = function truncate(value) {
22072203
return value;
22082204
};
22092205

2210-
22112206
var _truncateValue = function _truncateValue(value) {
22122207
if (type(value) === 'string') {
22132208
return value.length > constants.MAX_STRING_LENGTH ? value.substring(0, constants.MAX_STRING_LENGTH) : value;
22142209
}
22152210
return value;
22162211
};
22172212

2218-
22192213
var validateInput = function validateInput(input, name, expectedType) {
22202214
if (type(input) !== expectedType) {
22212215
log('Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type(input));
@@ -2224,7 +2218,6 @@ var validateInput = function validateInput(input, name, expectedType) {
22242218
return true;
22252219
};
22262220

2227-
22282221
var validateProperties = function validateProperties(properties) {
22292222
var propsType = type(properties);
22302223
if (propsType !== 'object') {
@@ -2256,7 +2249,6 @@ var validateProperties = function validateProperties(properties) {
22562249
return copy;
22572250
};
22582251

2259-
22602252
var invalidValueTypes = [
22612253
'null', 'nan', 'undefined', 'function', 'arguments', 'regexp', 'element'
22622254
];
@@ -2288,7 +2280,6 @@ var validatePropertyValue = function validatePropertyValue(key, value) {
22882280
return value;
22892281
};
22902282

2291-
22922283
module.exports = {
22932284
log: log,
22942285
isEmptyString: isEmptyString,
@@ -4159,12 +4150,9 @@ var language = require('./language');
41594150
// default options
41604151
module.exports = {
41614152
apiEndpoint: 'api.amplitude.com',
4162-
batchEvents: false,
41634153
cookieExpiration: 365 * 10,
41644154
cookieName: 'amplitude_id',
41654155
domain: '',
4166-
eventUploadPeriodMillis: 30 * 1000, // 30s
4167-
eventUploadThreshold: 30,
41684156
includeReferrer: false,
41694157
includeUtm: false,
41704158
language: language.language,
@@ -4175,7 +4163,10 @@ module.exports = {
41754163
sessionTimeout: 30 * 60 * 1000,
41764164
unsentKey: 'amplitude_unsent',
41774165
unsentIdentifyKey: 'amplitude_unsent_identify',
4178-
uploadBatchSize: 100
4166+
uploadBatchSize: 100,
4167+
batchEvents: false,
4168+
eventUploadThreshold: 30,
4169+
eventUploadPeriodMillis: 30 * 1000, // 30s
41794170
};
41804171

41814172
}, {"./language":27}],

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: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ var DEFAULT_OPTIONS = require('./options');
2121
* @example var amplitude = new Amplitude();
2222
*/
2323
var Amplitude = function Amplitude() {
24-
this.cookieStorage = new cookieStorage().getStorage();
24+
this._unsentEvents = [];
25+
this._unsentIdentifys = [];
26+
this._ua = new UAParser(navigator.userAgent).getResult();
2527
this.options = object.merge({}, DEFAULT_OPTIONS);
28+
this.cookieStorage = new cookieStorage().getStorage();
2629
this._q = []; // queue for proxied functions before script load
2730
this._sending = false;
28-
this._ua = new UAParser(navigator.userAgent).getResult();
29-
this._unsentEvents = [];
30-
this._unsentIdentifys = [];
3131
this._updateScheduled = false;
3232

3333
// event meta data
@@ -66,6 +66,7 @@ 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+
6970
_upgradeCookeData(this);
7071
_loadCookieData(this);
7172

@@ -75,7 +76,6 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
7576
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
7677
this.options.userId || null;
7778

78-
// determine if starting new session
7979
var now = new Date().getTime();
8080
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
8181
this._newSession = true;
@@ -106,13 +106,12 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
106106
if (this.options.includeUtm) {
107107
this._initUtmData();
108108
}
109+
109110
if (this.options.includeReferrer) {
110111
this._saveReferrer(this._getReferrer());
111112
}
112-
113113
} catch (e) {
114114
utils.log(e);
115-
116115
} finally {
117116
if (type(opt_callback) === 'function') {
118117
opt_callback();
@@ -132,14 +131,15 @@ var _parseConfig = function _parseConfig(options, config) {
132131

133132
// verifies config value is defined, is the correct type, and some additional value verification
134133
var parseValidateLoad = function parseValidateLoad(key, expectedType) {
135-
if (type(config[key]) !== expectedType) {
134+
var input_value = config[key];
135+
if(input_value === undefined || !utils.validateInput(input_value, key + ' option', expectedType)) {
136136
return;
137137
}
138138
if (expectedType === 'boolean') {
139-
options[key] = !!config[key];
139+
options[key] = !!input_value;
140140
} else {
141-
options[key] = (expectedType === 'string' && !utils.isEmptyString(config[key]) && config[key]) ||
142-
(expectedType === 'number' && config[key] > 0 && config[key]) ||
141+
options[key] = (expectedType === 'string' && !utils.isEmptyString(input_value) && input_value) ||
142+
(expectedType === 'number' && config[key] > 0 && input_value) ||
143143
options[key];
144144
}
145145
};
@@ -611,7 +611,6 @@ Amplitude.prototype.setUserProperties = function setUserProperties(userPropertie
611611
if (!this._apiKeySet('setUserProperties()') || !utils.validateInput(userProperties, 'userProperties', 'object')) {
612612
return;
613613
}
614-
615614
// convert userProperties into an identify call
616615
var identify = new Identify();
617616
for (var property in userProperties) {
@@ -631,6 +630,7 @@ Amplitude.prototype.clearUserProperties = function clearUserProperties(){
631630
if (!this._apiKeySet('clearUserProperties()')) {
632631
return;
633632
}
633+
634634
var identify = new Identify();
635635
identify.clearAll();
636636
this.identify(identify);

src/options.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ var language = require('./language');
33
// default options
44
module.exports = {
55
apiEndpoint: 'api.amplitude.com',
6-
batchEvents: false,
76
cookieExpiration: 365 * 10,
87
cookieName: 'amplitude_id',
98
domain: '',
10-
eventUploadPeriodMillis: 30 * 1000, // 30s
11-
eventUploadThreshold: 30,
129
includeReferrer: false,
1310
includeUtm: false,
1411
language: language.language,
@@ -19,5 +16,8 @@ module.exports = {
1916
sessionTimeout: 30 * 60 * 1000,
2017
unsentKey: 'amplitude_unsent',
2118
unsentIdentifyKey: 'amplitude_unsent_identify',
22-
uploadBatchSize: 100
19+
uploadBatchSize: 100,
20+
batchEvents: false,
21+
eventUploadThreshold: 30,
22+
eventUploadPeriodMillis: 30 * 1000, // 30s
2323
};

src/utils.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var constants = require('./constants');
22
var type = require('./type');
33

4-
54
var log = function log(s) {
65
try {
76
console.log('[Amplitude] ' + s);
@@ -10,12 +9,10 @@ var log = function log(s) {
109
}
1110
};
1211

13-
1412
var isEmptyString = function isEmptyString(str) {
1513
return (!str || str.length === 0);
1614
};
1715

18-
1916
var sessionStorageEnabled = function sessionStorageEnabled() {
2017
try {
2118
if (window.sessionStorage) {
@@ -25,7 +22,6 @@ var sessionStorageEnabled = function sessionStorageEnabled() {
2522
return false;
2623
};
2724

28-
2925
// truncate string values in event and user properties so that request size does not get too large
3026
var truncate = function truncate(value) {
3127
if (type(value) === 'array') {
@@ -45,15 +41,13 @@ var truncate = function truncate(value) {
4541
return value;
4642
};
4743

48-
4944
var _truncateValue = function _truncateValue(value) {
5045
if (type(value) === 'string') {
5146
return value.length > constants.MAX_STRING_LENGTH ? value.substring(0, constants.MAX_STRING_LENGTH) : value;
5247
}
5348
return value;
5449
};
5550

56-
5751
var validateInput = function validateInput(input, name, expectedType) {
5852
if (type(input) !== expectedType) {
5953
log('Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type(input));
@@ -62,7 +56,6 @@ var validateInput = function validateInput(input, name, expectedType) {
6256
return true;
6357
};
6458

65-
6659
var validateProperties = function validateProperties(properties) {
6760
var propsType = type(properties);
6861
if (propsType !== 'object') {
@@ -94,7 +87,6 @@ var validateProperties = function validateProperties(properties) {
9487
return copy;
9588
};
9689

97-
9890
var invalidValueTypes = [
9991
'null', 'nan', 'undefined', 'function', 'arguments', 'regexp', 'element'
10092
];
@@ -126,7 +118,6 @@ var validatePropertyValue = function validatePropertyValue(key, value) {
126118
return value;
127119
};
128120

129-
130121
module.exports = {
131122
log: log,
132123
isEmptyString: isEmptyString,

0 commit comments

Comments
 (0)