@@ -127,13 +127,13 @@ var DEFAULT_OPTIONS = require('./options');
127127 * @example var amplitude = new Amplitude();
128128 */
129129var 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){
21632163var constants = require ( './constants' ) ;
21642164var type = require ( './type' ) ;
21652165
2166-
21672166var log = function log ( s ) {
21682167 try {
21692168 console . log ( '[Amplitude] ' + s ) ;
@@ -2172,12 +2171,10 @@ var log = function log(s) {
21722171 }
21732172} ;
21742173
2175-
21762174var isEmptyString = function isEmptyString ( str ) {
21772175 return ( ! str || str . length === 0 ) ;
21782176} ;
21792177
2180-
21812178var 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
21922188var truncate = function truncate ( value ) {
21932189 if ( type ( value ) === 'array' ) {
@@ -2207,15 +2203,13 @@ var truncate = function truncate(value) {
22072203 return value ;
22082204} ;
22092205
2210-
22112206var _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-
22192213var 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-
22282221var 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-
22602252var 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-
22922283module . exports = {
22932284 log : log ,
22942285 isEmptyString : isEmptyString ,
@@ -4159,12 +4150,9 @@ var language = require('./language');
41594150// default options
41604151module . 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 } ] ,
0 commit comments