Skip to content

Commit 83d869c

Browse files
committed
validate properties for events loaded from localstorage
1 parent 008d74d commit 83d869c

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

amplitude.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
373373
if (this.options.saveEvents) {
374374
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey) || this._unsentEvents;
375375
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey) || this._unsentIdentifys;
376+
377+
// validate event properties for unsent events
378+
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);
381+
}
382+
376383
this._sendEventsIfReady();
377384
}
378385

@@ -2394,10 +2401,8 @@ var validateProperties = function(properties) {
23942401
if (value === null) {
23952402
continue;
23962403
}
2397-
23982404
copy[key] = value;
23992405
}
2400-
24012406
return copy;
24022407
};
24032408

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ AmplitudeClient.prototype.init = function(apiKey, opt_userId, opt_config, callba
124124
if (this.options.saveEvents) {
125125
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey) || this._unsentEvents;
126126
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey) || this._unsentIdentifys;
127+
128+
// validate event properties for unsent events
129+
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);
132+
}
133+
127134
this._sendEventsIfReady();
128135
}
129136

src/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ var validateProperties = function(properties) {
3838
if (value === null) {
3939
continue;
4040
}
41-
4241
copy[key] = value;
4342
}
44-
4543
return copy;
4644
};
4745

test/amplitude-client.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe('AmplitudeClient', function() {
55
var CookieStorage = require('../src/cookiestorage.js');
66
var Base64 = require('../src/base64.js');
77
var cookie = require('../src/cookie.js');
8+
var utils = require('../src/utils.js');
89
var querystring = require('querystring');
910
var JSON = require('json');
1011
var Identify = require('../src/identify.js');
@@ -381,6 +382,21 @@ describe('AmplitudeClient', function() {
381382
assert.equal(localStorage.getItem('amplitude_unsent_identify_new_app'), existingIdentify);
382383
});
383384

385+
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);
392+
393+
var amplitude2 = new AmplitudeClient('$default_Instance');
394+
amplitude2.init(apiKey, null, {batchEvents: true});
395+
396+
// check event loaded into memory
397+
assert.deepEqual(amplitude2._unsentEvents[0].event_properties, {});
398+
});
399+
384400
it ('should load saved events from localStorage new keys and send events', function() {
385401
var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
386402
'"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
@@ -1494,6 +1510,55 @@ describe('AmplitudeClient', function() {
14941510
'sequenceNumber': 3
14951511
});
14961512
});
1513+
1514+
it('should validate event properties', function() {
1515+
var e = new Error('oops');
1516+
clock.tick(1);
1517+
amplitude.init(apiKey, null, {batchEvents: true, eventUploadThreshold: 5});
1518+
clock.tick(1);
1519+
amplitude.logEvent('String event properties', '{}');
1520+
clock.tick(1);
1521+
amplitude.logEvent('Bool event properties', true);
1522+
clock.tick(1);
1523+
amplitude.logEvent('Number event properties', 15);
1524+
clock.tick(1);
1525+
amplitude.logEvent('Array event properties', [1, 2, 3]);
1526+
clock.tick(1);
1527+
amplitude.logEvent('Object event properties', {
1528+
10: 'false', // coerce key
1529+
'bool': true,
1530+
'null': null, // should be ignored
1531+
'function': utils.log, // should be ignored
1532+
'regex': /afdg/, // should be ignored
1533+
'error': e, // coerce value
1534+
'string': 'test',
1535+
'array': [0, 1, 2, '3'],
1536+
'nested_array': ['a', {'key': 'value'}, ['b']],
1537+
'object': {'key':'value', 15: e},
1538+
'nested_object': {'k':'v', 'l':[0,1], 'o':{'k2':'v2', 'l2': ['e2', {'k3': 'v3'}]}}
1539+
});
1540+
clock.tick(1);
1541+
1542+
assert.lengthOf(amplitude._unsentEvents, 5);
1543+
assert.lengthOf(server.requests, 1);
1544+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1545+
assert.lengthOf(events, 5);
1546+
1547+
assert.deepEqual(events[0].event_properties, {});
1548+
assert.deepEqual(events[1].event_properties, {});
1549+
assert.deepEqual(events[2].event_properties, {});
1550+
assert.deepEqual(events[3].event_properties, {});
1551+
assert.deepEqual(events[4].event_properties, {
1552+
'10': 'false',
1553+
'bool': true,
1554+
'error': 'Error: oops',
1555+
'string': 'test',
1556+
'array': [0, 1, 2, '3'],
1557+
'nested_array': ['a'],
1558+
'object': {'key':'value', '15':'Error: oops'},
1559+
'nested_object': {'k':'v', 'l':[0,1], 'o':{'k2':'v2', 'l2': ['e2']}}
1560+
});
1561+
});
14971562
});
14981563

14991564
describe('optOut', function() {

0 commit comments

Comments
 (0)