Skip to content

Commit 3ef9ab0

Browse files
committed
cleaning up
1 parent 60ca59e commit 3ef9ab0

File tree

5 files changed

+87
-91
lines changed

5 files changed

+87
-91
lines changed

amplitude.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
225225
this._loadStoredData();
226226

227227
this.setUserId(opt_userId || this.options.userId || null);
228-
this.setDeviceId(opt_config && opt_config.deviceId !== undefined &&
229-
opt_config.deviceId !== null && opt_config.deviceId ||
230-
this.options.deviceId || UUID());
228+
this.setDeviceId((opt_config && opt_config.deviceId) || this.options.deviceId || UUID());
231229

232230
this._sendEventsIfReady();
233231

@@ -261,7 +259,7 @@ Amplitude.prototype.runQueuedFunctions = function () {
261259
* @property value: the value to store
262260
*/
263261
Amplitude.prototype.setLocalStorage = function(item, value) {
264-
var key = LocalStorageKeys[item] + '_' + this.options.apiKey.slice(0, 6);
262+
var key = item + '_' + this.options.apiKey.slice(0, 6);
265263
localStorage.setItem(key, value);
266264
};
267265

@@ -270,98 +268,98 @@ Amplitude.prototype.setLocalStorage = function(item, value) {
270268
* to avoid collisions.
271269
*
272270
* @property item: name of the item to fetch
273-
* @property def: default value to return if the item does not exist
271+
* @property defaultValue: default value to return if the item does not exist
274272
*/
275-
Amplitude.prototype.getLocalStorage = function(item, def) {
276-
var key = LocalStorageKeys[item] + '_' + this.options.apiKey.slice(0, 6);
277-
return localStorage.getItem(key) || def;
273+
Amplitude.prototype.getLocalStorage = function(item, defaultValue) {
274+
var key = item + '_' + this.options.apiKey.slice(0, 6);
275+
return localStorage.getItem(key) || defaultValue;
278276
};
279277

280278
Amplitude.prototype._upgradeStoredData = function() {
281279
// migrate data from cookie to local storage
282280
var cookieData = Cookie.get(this.options.cookieName);
283281
if (cookieData) {
284282
if (cookieData.deviceId) {
285-
this.setLocalStorage('DEVICE_ID', cookieData.deviceId);
283+
this.setLocalStorage(LocalStorageKeys.DEVICE_ID, cookieData.deviceId);
286284
}
287285
if (cookieData.userId) {
288-
this.setLocalStorage('USER_ID', cookieData.userId);
286+
this.setLocalStorage(LocalStorageKeys.USER_ID, cookieData.userId);
289287
}
290288
if (cookieData.optOut !== undefined) {
291-
this.setLocalStorage('OPT_OUT', cookieData.optOut);
289+
this.setLocalStorage(LocalStorageKeys.OPT_OUT, cookieData.optOut);
292290
}
293291
Cookie.remove(this.options.cookieName);
294292
}
295293

296294
// update local storage keys to prevent conflicts
297295
var lastEventId = localStorage.getItem(LocalStorageKeys.LAST_EVENT_ID);
298296
if (lastEventId) {
299-
this.setLocalStorage('LAST_EVENT_ID', lastEventId);
297+
this.setLocalStorage(LocalStorageKeys.LAST_EVENT_ID, lastEventId);
300298
localStorage.removeItem(LocalStorageKeys.LAST_EVENT_ID);
301299
}
302300

303301
var lastIdentifyId = localStorage.getItem(LocalStorageKeys.LAST_IDENTIFY_ID);
304302
if (lastIdentifyId) {
305-
this.setLocalStorage('LAST_IDENTIFY_ID', lastIdentifyId);
303+
this.setLocalStorage(LocalStorageKeys.LAST_IDENTIFY_ID, lastIdentifyId);
306304
localStorage.removeItem(LocalStorageKeys.LAST_IDENTIFY_ID);
307305
}
308306

309307
var lastSequenceNumber = localStorage.getItem(LocalStorageKeys.LAST_SEQUENCE_NUMBER);
310308
if (lastSequenceNumber) {
311-
this.setLocalStorage('LAST_SEQUENCE_NUMBER', lastSequenceNumber);
309+
this.setLocalStorage(LocalStorageKeys.LAST_SEQUENCE_NUMBER, lastSequenceNumber);
312310
localStorage.removeItem(LocalStorageKeys.LAST_SEQUENCE_NUMBER);
313311
}
314312

315313
var lastEventTime = localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME);
316314
if (lastEventTime) {
317-
this.setLocalStorage('LAST_EVENT_TIME', lastEventTime);
315+
this.setLocalStorage(LocalStorageKeys.LAST_EVENT_TIME, lastEventTime);
318316
localStorage.removeItem(LocalStorageKeys.LAST_EVENT_TIME);
319317
}
320318

321319
var sessionId = localStorage.getItem(LocalStorageKeys.SESSION_ID);
322320
if (sessionId) {
323-
this.setLocalStorage('SESSION_ID', sessionId);
321+
this.setLocalStorage(LocalStorageKeys.SESSION_ID, sessionId);
324322
localStorage.removeItem(LocalStorageKeys.SESSION_ID);
325323
}
326324

327325
var unsentEventsString = localStorage.getItem(LocalStorageKeys.UNSENT_EVENTS);
328326
if (unsentEventsString) {
329-
this.setLocalStorage('UNSENT_EVENTS', unsentEventsString);
327+
this.setLocalStorage(LocalStorageKeys.UNSENT_EVENTS, unsentEventsString);
330328
localStorage.removeItem(LocalStorageKeys.UNSENT_EVENTS);
331329
}
332330

333331
var unsentIdentifysString = localStorage.getItem(LocalStorageKeys.UNSENT_IDENTIFYS);
334332
if (unsentIdentifysString) {
335-
this.setLocalStorage('UNSENT_IDENTIFYS', unsentIdentifysString);
333+
this.setLocalStorage(LocalStorageKeys.UNSENT_IDENTIFYS, unsentIdentifysString);
336334
localStorage.removeItem(LocalStorageKeys.UNSENT_IDENTIFYS);
337335
}
338336
};
339337

340338
Amplitude.prototype._loadStoredData = function() {
341339
if (this.options.saveEvents) {
342-
this._unsentEvents = this._loadSavedUnsentEvents('UNSENT_EVENTS');
343-
this._unsentIdentifys = this._loadSavedUnsentEvents('UNSENT_IDENTIFYS');
340+
this._unsentEvents = this._loadSavedUnsentEvents(LocalStorageKeys.UNSENT_EVENTS);
341+
this._unsentIdentifys = this._loadSavedUnsentEvents(LocalStorageKeys.UNSENT_IDENTIFYS);
344342
}
345343

346344
try {
347-
this.options.deviceId = this.getLocalStorage('DEVICE_ID', this.options.deviceId);
348-
this.options.userId = this.getLocalStorage('USER_ID', this.options.userId);
349-
this.options.optOut = (this.getLocalStorage('OPT_OUT', String(this.options.optOut || false)) === 'true');
345+
this.options.deviceId = this.getLocalStorage(LocalStorageKeys.DEVICE_ID, this.options.deviceId);
346+
this.options.userId = this.getLocalStorage(LocalStorageKeys.USER_ID, this.options.userId);
347+
this.options.optOut = (this.getLocalStorage(LocalStorageKeys.OPT_OUT, String(this.options.optOut || false)) === 'true');
350348

351-
this._lastEventTime = parseInt(this.getLocalStorage('LAST_EVENT_TIME'));
352-
this._sessionId = parseInt(this.getLocalStorage('SESSION_ID'));
353-
this._eventId = parseInt(this.getLocalStorage('LAST_EVENT_ID', 0));
354-
this._identifyId = parseInt(this.getLocalStorage('LAST_IDENTIFY_ID', 0));
355-
this._sequenceNumber = parseInt(this.getLocalStorage('LAST_SEQUENCE_NUMBER', 0));
349+
this._lastEventTime = parseInt(this.getLocalStorage(LocalStorageKeys.LAST_EVENT_TIME));
350+
this._sessionId = parseInt(this.getLocalStorage(LocalStorageKeys.SESSION_ID));
351+
this._eventId = parseInt(this.getLocalStorage(LocalStorageKeys.LAST_EVENT_ID, 0));
352+
this._identifyId = parseInt(this.getLocalStorage(LocalStorageKeys.LAST_IDENTIFY_ID, 0));
353+
this._sequenceNumber = parseInt(this.getLocalStorage(LocalStorageKeys.LAST_SEQUENCE_NUMBER, 0));
356354

357355
var now = new Date().getTime();
358356
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
359357
this._newSession = true;
360358
this._sessionId = now;
361-
this.setLocalStorage('SESSION_ID', this._sessionId);
359+
this.setLocalStorage(LocalStorageKeys.SESSION_ID, this._sessionId);
362360
}
363361
this._lastEventTime = now;
364-
this.setLocalStorage('LAST_EVENT_TIME', this._lastEventTime);
362+
this.setLocalStorage(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
365363
} catch (e) {
366364
log(e);
367365
}
@@ -480,8 +478,8 @@ Amplitude.prototype._getReferringDomain = function() {
480478

481479
Amplitude.prototype.saveEvents = function() {
482480
try {
483-
this.setLocalStorage('UNSENT_EVENTS', JSON.stringify(this._unsentEvents));
484-
this.setLocalStorage('UNSENT_IDENTIFYS', JSON.stringify(this._unsentIdentifys));
481+
this.setLocalStorage(LocalStorageKeys.UNSENT_EVENTS, JSON.stringify(this._unsentEvents));
482+
this.setLocalStorage(LocalStorageKeys.UNSENT_IDENTIFYS, JSON.stringify(this._unsentIdentifys));
485483
} catch (e) {
486484
//log(e);
487485
}
@@ -506,7 +504,7 @@ Amplitude.prototype.setDomain = function(domain) {
506504
Amplitude.prototype.setUserId = function(userId) {
507505
try {
508506
this.options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null;
509-
this.setLocalStorage('USER_ID', this.options.userId);
507+
this.setLocalStorage(LocalStorageKeys.USER_ID, this.options.userId);
510508
//log('set userId=' + userId);
511509
} catch (e) {
512510
log(e);
@@ -516,7 +514,7 @@ Amplitude.prototype.setUserId = function(userId) {
516514
Amplitude.prototype.setOptOut = function(enable) {
517515
try {
518516
this.options.optOut = enable;
519-
this.setLocalStorage('OPT_OUT', this.options.optOut);
517+
this.setLocalStorage(LocalStorageKeys.OPT_OUT, this.options.optOut);
520518
//log('set optOut=' + enable);
521519
} catch (e) {
522520
log(e);
@@ -527,7 +525,7 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
527525
try {
528526
if (deviceId) {
529527
this.options.deviceId = ('' + deviceId);
530-
this.setLocalStorage('DEVICE_ID', this.options.deviceId);
528+
this.setLocalStorage(LocalStorageKeys.DEVICE_ID, this.options.deviceId);
531529
}
532530
} catch (e) {
533531
log(e);
@@ -617,19 +615,19 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
617615
var eventId;
618616
if (eventType === IDENTIFY_EVENT) {
619617
eventId = this.nextIdentifyId();
620-
this.setLocalStorage('LAST_IDENTIFY_ID', eventId);
618+
this.setLocalStorage(LocalStorageKeys.LAST_IDENTIFY_ID, eventId);
621619
} else {
622620
eventId = this.nextEventId();
623-
this.setLocalStorage('LAST_EVENT_ID', eventId);
621+
this.setLocalStorage(LocalStorageKeys.LAST_EVENT_ID, eventId);
624622
}
625623
var eventTime = new Date().getTime();
626624
var ua = this._ua;
627625
if (!this._sessionId || !this._lastEventTime || eventTime - this._lastEventTime > this.options.sessionTimeout) {
628626
this._sessionId = eventTime;
629-
this.setLocalStorage('SESSION_ID', this._sessionId);
627+
this.setLocalStorage(LocalStorageKeys.SESSION_ID, this._sessionId);
630628
}
631629
this._lastEventTime = eventTime;
632-
this.setLocalStorage('LAST_EVENT_TIME', this._lastEventTime);
630+
this.setLocalStorage(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
633631

634632
userProperties = userProperties || {};
635633
// Only add utm properties to user properties for events

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.

0 commit comments

Comments
 (0)