Skip to content

Commit 03356dc

Browse files
committed
cleanup and updated reamde
1 parent 83d869c commit 03356dc

File tree

7 files changed

+43
-25
lines changed

7 files changed

+43
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ amplitude.getInstance().init('YOUR_API_KEY_HERE', 'USER_ID_HERE');
132132

133133
# Setting Event Properties #
134134

135-
You can attach additional data to any event by passing a Javascript object as the second argument to `logEvent`. The Javascript object should be in the form of key + value pairs that can be JSON serialized (we recommend using String values):
135+
You can attach additional data to any event by passing a Javascript object as the second argument to `logEvent`. The Javascript object should be in the form of key + value pairs that can be JSON serialized. The keys should be string values. The values can be booleans, strings, numbers, arrays of strings/numbers/booleans, nested Javascript objects, and errors (note you cannot nest arrays or Javascript objects inside array values). The SDK will validate the event properties that you set and will log any errors or warnings to console if there are any issues. Here is an example:
136136

137137
```javascript
138138
var eventProperties = {};

amplitude.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
376376

377377
// validate event properties for unsent events
378378
for (var i = 0; i < this._unsentEvents.length; i++) {
379-
var eventProperties = this._unsentEvents[0].event_properties;
380-
this._unsentEvents[0].event_properties = utils.validateProperties(eventProperties);
379+
var eventProperties = this._unsentEvents[i].event_properties;
380+
this._unsentEvents[i].event_properties = utils.validateProperties(eventProperties);
381381
}
382382

383383
this._sendEventsIfReady();
@@ -425,7 +425,7 @@ AmplitudeClient.prototype._loadSavedUnsentEvents = function(unsentKey) {
425425
try {
426426
return JSON.parse(savedUnsentEventsString);
427427
} catch (e) {
428-
//utils.log(e);
428+
// utils.log(e);
429429
}
430430
}
431431
return null;
@@ -846,7 +846,7 @@ AmplitudeClient.prototype._logEvent = function(eventType, eventProperties, apiPr
846846
}
847847

848848
apiProperties = apiProperties || {};
849-
eventProperties = utils.validateProperties(eventProperties) || {};
849+
eventProperties = eventProperties || {};
850850
var event = {
851851
device_id: this.options.deviceId,
852852
user_id: this.options.userId || this.options.deviceId,
@@ -861,7 +861,7 @@ AmplitudeClient.prototype._logEvent = function(eventType, eventProperties, apiPr
861861
device_model: ua.os.name || null,
862862
language: this.options.language,
863863
api_properties: apiProperties,
864-
event_properties: this._truncate(eventProperties),
864+
event_properties: this._truncate(utils.validateProperties(eventProperties)),
865865
user_properties: this._truncate(userProperties),
866866
uuid: UUID(),
867867
library: {
@@ -2413,7 +2413,7 @@ var invalidValueTypes = [
24132413
var validatePropertyValue = function(key, value) {
24142414
var valueType = type(value);
24152415
if (invalidValueTypes.indexOf(valueType) !== -1) {
2416-
log('WARNING: Property key "' + key + '" with value type ' + valueType + ', ignoring');
2416+
log('WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring');
24172417
value = null;
24182418
}
24192419
else if (valueType === 'error') {
@@ -2427,7 +2427,7 @@ var validatePropertyValue = function(key, value) {
24272427
var element = value[i];
24282428
var elemType = type(element);
24292429
if (elemType === 'array' || elemType === 'object') {
2430-
log('WARNING: Cannot have array or object nested in an array property value, skipping');
2430+
log('WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping');
24312431
continue;
24322432
}
24332433
arrayCopy.push(validatePropertyValue(key, element));

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-client.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
127127

128128
// validate event properties for unsent events
129129
for (var i = 0; i < this._unsentEvents.length; i++) {
130-
var eventProperties = this._unsentEvents[0].event_properties;
131-
this._unsentEvents[0].event_properties = utils.validateProperties(eventProperties);
130+
var eventProperties = this._unsentEvents[i].event_properties;
131+
this._unsentEvents[i].event_properties = utils.validateProperties(eventProperties);
132132
}
133133

134134
this._sendEventsIfReady();
@@ -176,7 +176,7 @@ AmplitudeClient.prototype._loadSavedUnsentEvents = function(unsentKey) {
176176
try {
177177
return JSON.parse(savedUnsentEventsString);
178178
} catch (e) {
179-
//utils.log(e);
179+
// utils.log(e);
180180
}
181181
}
182182
return null;
@@ -597,7 +597,7 @@ AmplitudeClient.prototype._logEvent = function(eventType, eventProperties, apiPr
597597
}
598598

599599
apiProperties = apiProperties || {};
600-
eventProperties = utils.validateProperties(eventProperties) || {};
600+
eventProperties = eventProperties || {};
601601
var event = {
602602
device_id: this.options.deviceId,
603603
user_id: this.options.userId || this.options.deviceId,
@@ -612,7 +612,7 @@ AmplitudeClient.prototype._logEvent = function(eventType, eventProperties, apiPr
612612
device_model: ua.os.name || null,
613613
language: this.options.language,
614614
api_properties: apiProperties,
615-
event_properties: this._truncate(eventProperties),
615+
event_properties: this._truncate(utils.validateProperties(eventProperties)),
616616
user_properties: this._truncate(userProperties),
617617
uuid: UUID(),
618618
library: {

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var invalidValueTypes = [
5050
var validatePropertyValue = function(key, value) {
5151
var valueType = type(value);
5252
if (invalidValueTypes.indexOf(valueType) !== -1) {
53-
log('WARNING: Property key "' + key + '" with value type ' + valueType + ', ignoring');
53+
log('WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring');
5454
value = null;
5555
}
5656
else if (valueType === 'error') {
@@ -64,7 +64,7 @@ var validatePropertyValue = function(key, value) {
6464
var element = value[i];
6565
var elemType = type(element);
6666
if (elemType === 'array' || elemType === 'object') {
67-
log('WARNING: Cannot have array or object nested in an array property value, skipping');
67+
log('WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping');
6868
continue;
6969
}
7070
arrayCopy.push(validatePropertyValue(key, element));

test/amplitude-client.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,36 @@ describe('AmplitudeClient', function() {
383383
});
384384

385385
it('should validate event properties when loading saved events from localStorage', function() {
386-
var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
387-
'"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
388-
',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
389-
'"event_properties":"{}","user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
390-
':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130}]';
391-
localStorage.setItem('amplitude_unsent', existingEvent);
386+
var existingEvents = '[{"device_id":"15a82aaa-0d9e-4083-a32d-2352191877e6","user_id":"15a82aaa-0d9e-4083-a32d' +
387+
'-2352191877e6","timestamp":1455744744413,"event_id":2,"session_id":1455744733865,"event_type":"clicked",' +
388+
'"version_name":"Web","platform":"Web","os_name":"Chrome","os_version":"48","device_model":"Mac","language"' +
389+
':"en-US","api_properties":{},"event_properties":"{}","user_properties":{},"uuid":"1b8859d9-e91e-403e-92d4-' +
390+
'c600dfb83432","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number":4},{"device_id":"15a82a' +
391+
'aa-0d9e-4083-a32d-2352191877e6","user_id":"15a82aaa-0d9e-4083-a32d-2352191877e6","timestamp":1455744746295,' +
392+
'"event_id":3,"session_id":1455744733865,"event_type":"clicked","version_name":"Web","platform":"Web",' +
393+
'"os_name":"Chrome","os_version":"48","device_model":"Mac","language":"en-US","api_properties":{},' +
394+
'"event_properties":{"10":"false","bool":true,"null":null,"string":"test","array":' +
395+
'[0,1,2,"3"],"nested_array":["a",{"key":"value"},["b"]],"object":{"key":"value"},"nested_object":' +
396+
'{"k":"v","l":[0,1],"o":{"k2":"v2","l2":["e2",{"k3":"v3"}]}}},"user_properties":{},"uuid":"650407a1-d705-' +
397+
'47a0-8918-b4530ce51f89","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number":5}]'
398+
localStorage.setItem('amplitude_unsent', existingEvents);
392399

393400
var amplitude2 = new AmplitudeClient('$default_Instance');
394401
amplitude2.init(apiKey, null, {batchEvents: true});
395402

396-
// check event loaded into memory
403+
var expected = {
404+
'10': 'false',
405+
'bool': true,
406+
'string': 'test',
407+
'array': [0, 1, 2, '3'],
408+
'nested_array': ['a'],
409+
'object': {'key':'value'},
410+
'nested_object': {'k':'v', 'l':[0,1], 'o':{'k2':'v2', 'l2': ['e2']}}
411+
}
412+
413+
// check that event loaded into memory
397414
assert.deepEqual(amplitude2._unsentEvents[0].event_properties, {});
415+
assert.deepEqual(amplitude2._unsentEvents[1].event_properties, expected);
398416
});
399417

400418
it ('should load saved events from localStorage new keys and send events', function() {

test/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('utils', function() {
8282
10: 'false', // coerce key
8383
'bool': true,
8484
'null': null, // should be ignored
85-
'function': utils.log, // should be ignored
85+
'function': console.log, // should be ignored
8686
'regex': /afdg/, // should be ignored
8787
'error': e, // coerce value
8888
'string': 'test',

0 commit comments

Comments
 (0)