@@ -2042,7 +2042,44 @@ var type = function (val) {
2042
2042
return typeof val === 'undefined' ? 'undefined' : _typeof ( val ) ;
2043
2043
} ;
2044
2044
2045
- var log = function log ( s ) {
2045
+ var logLevel = 'WARN' ;
2046
+
2047
+ var logLevels = {
2048
+ DISABLE : 0 ,
2049
+ ERROR : 1 ,
2050
+ WARN : 2 ,
2051
+ INFO : 3
2052
+ } ;
2053
+
2054
+ var setLogLevel = function setLogLevel ( logLevelName ) {
2055
+ logLevel = logLevels [ logLevelName ] || logLevel ;
2056
+ } ;
2057
+
2058
+ var getLogLevel = function getLogLevel ( ) {
2059
+ return logLevel ;
2060
+ } ;
2061
+
2062
+ var log = {
2063
+ error : function error ( s ) {
2064
+ if ( logLevel >= logLevels . ERROR ) {
2065
+ _log ( s ) ;
2066
+ }
2067
+ } ,
2068
+
2069
+ warn : function warn ( s ) {
2070
+ if ( logLevel >= logLevels . WARN ) {
2071
+ _log ( s ) ;
2072
+ }
2073
+ } ,
2074
+
2075
+ info : function info ( s ) {
2076
+ if ( logLevel >= logLevels . INFO ) {
2077
+ _log ( s ) ;
2078
+ }
2079
+ }
2080
+ } ;
2081
+
2082
+ var _log = function _log ( s ) {
2046
2083
try {
2047
2084
console . log ( '[Amplitude] ' + s ) ;
2048
2085
} catch ( e ) {
@@ -2091,7 +2128,7 @@ var _truncateValue = function _truncateValue(value) {
2091
2128
2092
2129
var validateInput = function validateInput ( input , name , expectedType ) {
2093
2130
if ( type ( input ) !== expectedType ) {
2094
- log ( 'Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type ( input ) ) ;
2131
+ log . error ( 'Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type ( input ) ) ;
2095
2132
return false ;
2096
2133
}
2097
2134
return true ;
@@ -2101,12 +2138,12 @@ var validateInput = function validateInput(input, name, expectedType) {
2101
2138
var validateProperties = function validateProperties ( properties ) {
2102
2139
var propsType = type ( properties ) ;
2103
2140
if ( propsType !== 'object' ) {
2104
- log ( 'Error: invalid properties format. Expecting Javascript object, received ' + propsType + ', ignoring' ) ;
2141
+ log . error ( 'Error: invalid properties format. Expecting Javascript object, received ' + propsType + ', ignoring' ) ;
2105
2142
return { } ;
2106
2143
}
2107
2144
2108
2145
if ( Object . keys ( properties ) . length > constants . MAX_PROPERTY_KEYS ) {
2109
- log ( 'Error: too many properties (more than 1000), ignoring' ) ;
2146
+ log . error ( 'Error: too many properties (more than 1000), ignoring' ) ;
2110
2147
return { } ;
2111
2148
}
2112
2149
@@ -2121,7 +2158,7 @@ var validateProperties = function validateProperties(properties) {
2121
2158
var keyType = type ( key ) ;
2122
2159
if ( keyType !== 'string' ) {
2123
2160
key = String ( key ) ;
2124
- log ( 'WARNING: Non-string property key, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2161
+ log . warn ( 'WARNING: Non-string property key, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2125
2162
}
2126
2163
2127
2164
// validate value
@@ -2139,19 +2176,19 @@ var invalidValueTypes = ['null', 'nan', 'undefined', 'function', 'arguments', 'r
2139
2176
var validatePropertyValue = function validatePropertyValue ( key , value ) {
2140
2177
var valueType = type ( value ) ;
2141
2178
if ( invalidValueTypes . indexOf ( valueType ) !== - 1 ) {
2142
- log ( 'WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring' ) ;
2179
+ log . warn ( 'WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring' ) ;
2143
2180
value = null ;
2144
2181
} else if ( valueType === 'error' ) {
2145
2182
value = String ( value ) ;
2146
- log ( 'WARNING: Property key "' + key + '" with value type error, coercing to ' + value ) ;
2183
+ log . warn ( 'WARNING: Property key "' + key + '" with value type error, coercing to ' + value ) ;
2147
2184
} else if ( valueType === 'array' ) {
2148
2185
// check for nested arrays or objects
2149
2186
var arrayCopy = [ ] ;
2150
2187
for ( var i = 0 ; i < value . length ; i ++ ) {
2151
2188
var element = value [ i ] ;
2152
2189
var elemType = type ( element ) ;
2153
2190
if ( elemType === 'array' || elemType === 'object' ) {
2154
- log ( 'WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping' ) ;
2191
+ log . warn ( 'WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping' ) ;
2155
2192
continue ;
2156
2193
}
2157
2194
arrayCopy . push ( validatePropertyValue ( key , element ) ) ;
@@ -2166,7 +2203,7 @@ var validatePropertyValue = function validatePropertyValue(key, value) {
2166
2203
var validateGroups = function validateGroups ( groups ) {
2167
2204
var groupsType = type ( groups ) ;
2168
2205
if ( groupsType !== 'object' ) {
2169
- log ( 'Error: invalid groups format. Expecting Javascript object, received ' + groupsType + ', ignoring' ) ;
2206
+ log . error ( 'Error: invalid groups format. Expecting Javascript object, received ' + groupsType + ', ignoring' ) ;
2170
2207
return { } ;
2171
2208
}
2172
2209
@@ -2181,7 +2218,7 @@ var validateGroups = function validateGroups(groups) {
2181
2218
var keyType = type ( key ) ;
2182
2219
if ( keyType !== 'string' ) {
2183
2220
key = String ( key ) ;
2184
- log ( 'WARNING: Non-string groupType, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2221
+ log . warn ( 'WARNING: Non-string groupType, received type ' + keyType + ', coercing to string "' + key + '"' ) ;
2185
2222
}
2186
2223
2187
2224
// validate value
@@ -2201,7 +2238,7 @@ var validateGroupName = function validateGroupName(key, groupName) {
2201
2238
}
2202
2239
if ( groupNameType === 'date' || groupNameType === 'number' || groupNameType === 'boolean' ) {
2203
2240
groupName = String ( groupName ) ;
2204
- log ( 'WARNING: Non-string groupName, received type ' + groupNameType + ', coercing to string "' + groupName + '"' ) ;
2241
+ log . warn ( 'WARNING: Non-string groupName, received type ' + groupNameType + ', coercing to string "' + groupName + '"' ) ;
2205
2242
return groupName ;
2206
2243
}
2207
2244
if ( groupNameType === 'array' ) {
@@ -2211,19 +2248,19 @@ var validateGroupName = function validateGroupName(key, groupName) {
2211
2248
var element = groupName [ i ] ;
2212
2249
var elemType = type ( element ) ;
2213
2250
if ( elemType === 'array' || elemType === 'object' ) {
2214
- log ( 'WARNING: Skipping nested ' + elemType + ' in array groupName' ) ;
2251
+ log . warn ( 'WARNING: Skipping nested ' + elemType + ' in array groupName' ) ;
2215
2252
continue ;
2216
2253
} else if ( elemType === 'string' ) {
2217
2254
arrayCopy . push ( element ) ;
2218
2255
} else if ( elemType === 'date' || elemType === 'number' || elemType === 'boolean' ) {
2219
2256
element = String ( element ) ;
2220
- log ( 'WARNING: Non-string groupName, received type ' + elemType + ', coercing to string "' + element + '"' ) ;
2257
+ log . warn ( 'WARNING: Non-string groupName, received type ' + elemType + ', coercing to string "' + element + '"' ) ;
2221
2258
arrayCopy . push ( element ) ;
2222
2259
}
2223
2260
}
2224
2261
return arrayCopy ;
2225
2262
}
2226
- log ( 'WARNING: Non-string groupName, received type ' + groupNameType + '. Please use strings or array of strings for groupName' ) ;
2263
+ log . warn ( 'WARNING: Non-string groupName, received type ' + groupNameType + '. Please use strings or array of strings for groupName' ) ;
2227
2264
} ;
2228
2265
2229
2266
// parses the value of a url param (for example ?gclid=1234&...)
@@ -2235,6 +2272,8 @@ var getQueryParam = function getQueryParam(name, query) {
2235
2272
} ;
2236
2273
2237
2274
var utils = {
2275
+ setLogLevel : setLogLevel ,
2276
+ getLogLevel : getLogLevel ,
2238
2277
log : log ,
2239
2278
isEmptyString : isEmptyString ,
2240
2279
getQueryParam : getQueryParam ,
@@ -2617,7 +2656,7 @@ Identify.prototype.add = function (property, value) {
2617
2656
if ( type ( value ) === 'number' || type ( value ) === 'string' ) {
2618
2657
this . _addOperation ( AMP_OP_ADD , property , value ) ;
2619
2658
} else {
2620
- utils . log ( 'Unsupported type for value: ' + type ( value ) + ', expecting number or string' ) ;
2659
+ utils . log . error ( 'Unsupported type for value: ' + type ( value ) + ', expecting number or string' ) ;
2621
2660
}
2622
2661
return this ;
2623
2662
} ;
@@ -2652,7 +2691,7 @@ Identify.prototype.append = function (property, value) {
2652
2691
Identify . prototype . clearAll = function ( ) {
2653
2692
if ( Object . keys ( this . userPropertiesOperations ) . length > 0 ) {
2654
2693
if ( ! this . userPropertiesOperations . hasOwnProperty ( AMP_OP_CLEAR_ALL ) ) {
2655
- utils . log ( 'Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll' ) ;
2694
+ utils . log . error ( 'Need to send $clearAll on its own Identify object without any other operations, skipping $clearAll' ) ;
2656
2695
}
2657
2696
return this ;
2658
2697
}
@@ -2735,13 +2774,13 @@ Identify.prototype.unset = function (property) {
2735
2774
Identify . prototype . _addOperation = function ( operation , property , value ) {
2736
2775
// check that the identify doesn't already contain a clearAll
2737
2776
if ( this . userPropertiesOperations . hasOwnProperty ( AMP_OP_CLEAR_ALL ) ) {
2738
- utils . log ( 'This identify already contains a $clearAll operation, skipping operation ' + operation ) ;
2777
+ utils . log . error ( 'This identify already contains a $clearAll operation, skipping operation ' + operation ) ;
2739
2778
return ;
2740
2779
}
2741
2780
2742
2781
// check that property wasn't already used in this Identify
2743
2782
if ( this . properties . indexOf ( property ) !== - 1 ) {
2744
- utils . log ( 'User property "' + property + '" already used in this identify, skipping operation ' + operation ) ;
2783
+ utils . log . error ( 'User property "' + property + '" already used in this identify, skipping operation ' + operation ) ;
2745
2784
return ;
2746
2785
}
2747
2786
@@ -4539,9 +4578,9 @@ var Revenue = function Revenue() {
4539
4578
*/
4540
4579
Revenue . prototype . setProductId = function setProductId ( productId ) {
4541
4580
if ( type ( productId ) !== 'string' ) {
4542
- utils . log ( 'Unsupported type for productId: ' + type ( productId ) + ', expecting string' ) ;
4581
+ utils . log . error ( 'Unsupported type for productId: ' + type ( productId ) + ', expecting string' ) ;
4543
4582
} else if ( utils . isEmptyString ( productId ) ) {
4544
- utils . log ( 'Invalid empty productId' ) ;
4583
+ utils . log . error ( 'Invalid empty productId' ) ;
4545
4584
} else {
4546
4585
this . _productId = productId ;
4547
4586
}
@@ -4558,7 +4597,7 @@ Revenue.prototype.setProductId = function setProductId(productId) {
4558
4597
*/
4559
4598
Revenue . prototype . setQuantity = function setQuantity ( quantity ) {
4560
4599
if ( type ( quantity ) !== 'number' ) {
4561
- utils . log ( 'Unsupported type for quantity: ' + type ( quantity ) + ', expecting number' ) ;
4600
+ utils . log . error ( 'Unsupported type for quantity: ' + type ( quantity ) + ', expecting number' ) ;
4562
4601
} else {
4563
4602
this . _quantity = parseInt ( quantity ) ;
4564
4603
}
@@ -4576,7 +4615,7 @@ Revenue.prototype.setQuantity = function setQuantity(quantity) {
4576
4615
*/
4577
4616
Revenue . prototype . setPrice = function setPrice ( price ) {
4578
4617
if ( type ( price ) !== 'number' ) {
4579
- utils . log ( 'Unsupported type for price: ' + type ( price ) + ', expecting number' ) ;
4618
+ utils . log . error ( 'Unsupported type for price: ' + type ( price ) + ', expecting number' ) ;
4580
4619
} else {
4581
4620
this . _price = price ;
4582
4621
}
@@ -4593,7 +4632,7 @@ Revenue.prototype.setPrice = function setPrice(price) {
4593
4632
*/
4594
4633
Revenue . prototype . setRevenueType = function setRevenueType ( revenueType ) {
4595
4634
if ( type ( revenueType ) !== 'string' ) {
4596
- utils . log ( 'Unsupported type for revenueType: ' + type ( revenueType ) + ', expecting string' ) ;
4635
+ utils . log . error ( 'Unsupported type for revenueType: ' + type ( revenueType ) + ', expecting string' ) ;
4597
4636
} else {
4598
4637
this . _revenueType = revenueType ;
4599
4638
}
@@ -4611,7 +4650,7 @@ Revenue.prototype.setRevenueType = function setRevenueType(revenueType) {
4611
4650
*/
4612
4651
Revenue . prototype . setEventProperties = function setEventProperties ( eventProperties ) {
4613
4652
if ( type ( eventProperties ) !== 'object' ) {
4614
- utils . log ( 'Unsupported type for eventProperties: ' + type ( eventProperties ) + ', expecting object' ) ;
4653
+ utils . log . error ( 'Unsupported type for eventProperties: ' + type ( eventProperties ) + ', expecting object' ) ;
4615
4654
} else {
4616
4655
this . _properties = utils . validateProperties ( eventProperties ) ;
4617
4656
}
@@ -4623,7 +4662,7 @@ Revenue.prototype.setEventProperties = function setEventProperties(eventProperti
4623
4662
*/
4624
4663
Revenue . prototype . _isValidRevenue = function _isValidRevenue ( ) {
4625
4664
if ( type ( this . _price ) !== 'number' ) {
4626
- utils . log ( 'Invalid revenue, need to set price field' ) ;
4665
+ utils . log . error ( 'Invalid revenue, need to set price field' ) ;
4627
4666
return false ;
4628
4667
}
4629
4668
return true ;
@@ -5607,6 +5646,7 @@ var DEFAULT_OPTIONS = {
5607
5646
includeReferrer : false ,
5608
5647
includeUtm : false ,
5609
5648
language : language . language ,
5649
+ logLevel : 'WARN' ,
5610
5650
optOut : false ,
5611
5651
platform : 'Web' ,
5612
5652
savedMaxCount : 1000 ,
@@ -5670,7 +5710,7 @@ AmplitudeClient.prototype.Revenue = Revenue;
5670
5710
*/
5671
5711
AmplitudeClient . prototype . init = function init ( apiKey , opt_userId , opt_config , opt_callback ) {
5672
5712
if ( type ( apiKey ) !== 'string' || utils . isEmptyString ( apiKey ) ) {
5673
- utils . log ( 'Invalid apiKey. Please re-initialize with a valid apiKey' ) ;
5713
+ utils . log . error ( 'Invalid apiKey. Please re-initialize with a valid apiKey' ) ;
5674
5714
return ;
5675
5715
}
5676
5716
@@ -5734,7 +5774,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
5734
5774
5735
5775
this . _sendEventsIfReady ( ) ; // try sending unsent events
5736
5776
} catch ( e ) {
5737
- utils . log ( e ) ;
5777
+ utils . log . error ( e ) ;
5738
5778
} finally {
5739
5779
if ( type ( opt_callback ) === 'function' ) {
5740
5780
opt_callback ( this ) ;
@@ -5812,7 +5852,7 @@ AmplitudeClient.prototype.runQueuedFunctions = function () {
5812
5852
*/
5813
5853
AmplitudeClient . prototype . _apiKeySet = function _apiKeySet ( methodName ) {
5814
5854
if ( utils . isEmptyString ( this . options . apiKey ) ) {
5815
- utils . log ( 'Invalid apiKey. Please set a valid apiKey with init() before calling ' + methodName ) ;
5855
+ utils . log . error ( 'Invalid apiKey. Please set a valid apiKey with init() before calling ' + methodName ) ;
5816
5856
return false ;
5817
5857
}
5818
5858
return true ;
@@ -5837,7 +5877,7 @@ AmplitudeClient.prototype._loadSavedUnsentEvents = function _loadSavedUnsentEven
5837
5877
}
5838
5878
} catch ( e ) { }
5839
5879
}
5840
- utils . log ( 'Unable to load ' + unsentKey + ' events. Restart with a new empty queue.' ) ;
5880
+ utils . log . error ( 'Unable to load ' + unsentKey + ' events. Restart with a new empty queue.' ) ;
5841
5881
return [ ] ;
5842
5882
} ;
5843
5883
@@ -6185,7 +6225,7 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
6185
6225
_loadCookieData ( this ) ;
6186
6226
_saveCookieData ( this ) ;
6187
6227
} catch ( e ) {
6188
- utils . log ( e ) ;
6228
+ utils . log . error ( e ) ;
6189
6229
}
6190
6230
} ;
6191
6231
@@ -6200,7 +6240,7 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId) {
6200
6240
this . options . userId = userId !== undefined && userId !== null && '' + userId || null ;
6201
6241
_saveCookieData ( this ) ;
6202
6242
} catch ( e ) {
6203
- utils . log ( e ) ;
6243
+ utils . log . error ( e ) ;
6204
6244
}
6205
6245
} ;
6206
6246
@@ -6243,7 +6283,7 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
6243
6283
this . options . optOut = enable ;
6244
6284
_saveCookieData ( this ) ;
6245
6285
} catch ( e ) {
6246
- utils . log ( e ) ;
6286
+ utils . log . error ( e ) ;
6247
6287
}
6248
6288
} ;
6249
6289
@@ -6256,7 +6296,7 @@ AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
6256
6296
this . _sessionId = sessionId ;
6257
6297
_saveCookieData ( this ) ;
6258
6298
} catch ( e ) {
6259
- utils . log ( e ) ;
6299
+ utils . log . error ( e ) ;
6260
6300
}
6261
6301
} ;
6262
6302
@@ -6290,7 +6330,7 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
6290
6330
_saveCookieData ( this ) ;
6291
6331
}
6292
6332
} catch ( e ) {
6293
- utils . log ( e ) ;
6333
+ utils . log . error ( e ) ;
6294
6334
}
6295
6335
} ;
6296
6336
@@ -6382,7 +6422,7 @@ AmplitudeClient.prototype.identify = function (identify_obj, opt_callback) {
6382
6422
return this . _logEvent ( constants . IDENTIFY_EVENT , null , null , identify_obj . userPropertiesOperations , null , null , opt_callback ) ;
6383
6423
}
6384
6424
} else {
6385
- utils . log ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify_obj ) ) ;
6425
+ utils . log . error ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify_obj ) ) ;
6386
6426
}
6387
6427
6388
6428
if ( type ( opt_callback ) === 'function' ) {
@@ -6480,7 +6520,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
6480
6520
6481
6521
return eventId ;
6482
6522
} catch ( e ) {
6483
- utils . log ( e ) ;
6523
+ utils . log . error ( e ) ;
6484
6524
}
6485
6525
} ;
6486
6526
@@ -6594,7 +6634,7 @@ AmplitudeClient.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) {
6594
6634
return this . logEvent ( constants . REVENUE_EVENT , revenue_obj . _toJSONObject ( ) ) ;
6595
6635
}
6596
6636
} else {
6597
- utils . log ( 'Invalid revenue input type. Expected Revenue object but saw ' + type ( revenue_obj ) ) ;
6637
+ utils . log . error ( 'Invalid revenue input type. Expected Revenue object but saw ' + type ( revenue_obj ) ) ;
6598
6638
}
6599
6639
} ;
6600
6640
@@ -6743,7 +6783,7 @@ AmplitudeClient.prototype._mergeEventsAndIdentifys = function _mergeEventsAndIde
6743
6783
// case 0: no events or identifys left
6744
6784
// note this should not happen, this means we have less events and identifys than expected
6745
6785
if ( noEvents && noIdentifys ) {
6746
- utils . log ( 'Merging Events and Identifys, less events and identifys than expected' ) ;
6786
+ utils . log . error ( 'Merging Events and Identifys, less events and identifys than expected' ) ;
6747
6787
break ;
6748
6788
}
6749
6789
0 commit comments