Skip to content

Commit 8ef0be7

Browse files
committed
adding local storage keys
1 parent 48e45c7 commit 8ef0be7

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

amplitude.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ var LocalStorageKeys = {
148148
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
149149
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
150150
LAST_EVENT_TIME: 'amplitude_lastEventTime',
151-
SESSION_ID: 'amplitude_sessionId'
151+
SESSION_ID: 'amplitude_sessionId',
152+
153+
DEVICE_ID: 'amplitude_deviceId',
154+
USER_ID: 'amplitude_userId',
155+
OPT_OUT: 'amplitude_optOut'
152156
};
153157

154158
/*
@@ -337,8 +341,7 @@ Amplitude.prototype._sendEventsIfReady = function(callback) {
337341

338342
var _migrateLocalStorageDataToCookie = function(scope) {
339343
var cookieData = Cookie.get(scope.options.cookieName);
340-
if (cookieData && cookieData.deviceId && cookieData.userId &&
341-
cookieData.optOut !== null && cookieData.optOut !== undefined) {
344+
if (cookieData && cookieData.deviceId) {
342345
return; // migration not needed
343346
}
344347

@@ -348,17 +351,17 @@ var _migrateLocalStorageDataToCookie = function(scope) {
348351
cookieData.optOut : null;
349352

350353
var keySuffix = '_' + scope.options.apiKey.slice(0, 6);
351-
var localStorageDeviceId = localStorage.getItem('amplitude_deviceId' + keySuffix);
354+
var localStorageDeviceId = localStorage.getItem(LocalStorageKeys.DEVICE_ID + keySuffix);
352355
if (localStorageDeviceId) {
353-
localStorage.removeItem('amplitude_deviceId' + keySuffix);
356+
localStorage.removeItem(LocalStorageKeys.DEVICE_ID + keySuffix);
354357
}
355-
var localStorageUserId = localStorage.getItem('amplitude_userId' + keySuffix);
358+
var localStorageUserId = localStorage.getItem(LocalStorageKeys.USER_ID + keySuffix);
356359
if (localStorageUserId) {
357-
localStorage.removeItem('amplitude_userId' + keySuffix);
360+
localStorage.removeItem(LocalStorageKeys.USER_ID + keySuffix);
358361
}
359-
var localStorageOptOut = localStorage.getItem('amplitude_optOut' + keySuffix);
362+
var localStorageOptOut = localStorage.getItem(LocalStorageKeys.OPT_OUT + keySuffix);
360363
if (localStorageOptOut !== null && localStorageOptOut !== undefined) {
361-
localStorage.removeItem('amplitude_optOut' + keySuffix);
364+
localStorage.removeItem(LocalStorageKeys.OPT_OUT + keySuffix);
362365
localStorageOptOut = String(localStorageOptOut) === 'true'; // convert to boolean
363366
}
364367

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ var LocalStorageKeys = {
4242
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
4343
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
4444
LAST_EVENT_TIME: 'amplitude_lastEventTime',
45-
SESSION_ID: 'amplitude_sessionId'
45+
SESSION_ID: 'amplitude_sessionId',
46+
47+
DEVICE_ID: 'amplitude_deviceId',
48+
USER_ID: 'amplitude_userId',
49+
OPT_OUT: 'amplitude_optOut'
4650
};
4751

4852
/*
@@ -231,8 +235,7 @@ Amplitude.prototype._sendEventsIfReady = function(callback) {
231235

232236
var _migrateLocalStorageDataToCookie = function(scope) {
233237
var cookieData = Cookie.get(scope.options.cookieName);
234-
if (cookieData && cookieData.deviceId && cookieData.userId &&
235-
cookieData.optOut !== null && cookieData.optOut !== undefined) {
238+
if (cookieData && cookieData.deviceId) {
236239
return; // migration not needed
237240
}
238241

@@ -242,17 +245,17 @@ var _migrateLocalStorageDataToCookie = function(scope) {
242245
cookieData.optOut : null;
243246

244247
var keySuffix = '_' + scope.options.apiKey.slice(0, 6);
245-
var localStorageDeviceId = localStorage.getItem('amplitude_deviceId' + keySuffix);
248+
var localStorageDeviceId = localStorage.getItem(LocalStorageKeys.DEVICE_ID + keySuffix);
246249
if (localStorageDeviceId) {
247-
localStorage.removeItem('amplitude_deviceId' + keySuffix);
250+
localStorage.removeItem(LocalStorageKeys.DEVICE_ID + keySuffix);
248251
}
249-
var localStorageUserId = localStorage.getItem('amplitude_userId' + keySuffix);
252+
var localStorageUserId = localStorage.getItem(LocalStorageKeys.USER_ID + keySuffix);
250253
if (localStorageUserId) {
251-
localStorage.removeItem('amplitude_userId' + keySuffix);
254+
localStorage.removeItem(LocalStorageKeys.USER_ID + keySuffix);
252255
}
253-
var localStorageOptOut = localStorage.getItem('amplitude_optOut' + keySuffix);
256+
var localStorageOptOut = localStorage.getItem(LocalStorageKeys.OPT_OUT + keySuffix);
254257
if (localStorageOptOut !== null && localStorageOptOut !== undefined) {
255-
localStorage.removeItem('amplitude_optOut' + keySuffix);
258+
localStorage.removeItem(LocalStorageKeys.OPT_OUT + keySuffix);
256259
localStorageOptOut = String(localStorageOptOut) === 'true'; // convert to boolean
257260
}
258261

test/amplitude.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,25 @@ describe('Amplitude', function() {
104104
var userId = 'test_user_id2';
105105

106106
// use amplitude1 to set cookie values
107-
amplitude.init(apiKey, null, {deviceId: deviceId});
107+
amplitude.init(apiKey, userId, {deviceId: deviceId});
108108
assert.equal(amplitude.options.deviceId, deviceId);
109-
assert.isNull(amplitude.options.userId);
109+
assert.equal(amplitude.options.userId, userId);
110110
assert.isFalse(amplitude.options.optOut);
111111

112112
var cookieData = cookie.get(amplitude.options.cookieName);
113113
assert.equal(cookieData.deviceId, deviceId);
114-
assert.isNull(cookieData.userId);
114+
assert.equal(cookieData.userId, userId);
115115
assert.isFalse(cookieData.optOut);
116116

117+
// remove deviceId to make amplitude2 go through migration process
118+
cookie.set(amplitude.options.cookieName, {
119+
'userId': userId,
120+
'optOut': false
121+
});
122+
117123
// set local storage values and verify that they are ignored by the init migration
118-
localStorage.setItem('amplitude_deviceId' + '_' + apiKey, 'bad_test_device_id'); // ignored
119-
localStorage.setItem('amplitude_userId' + '_' + apiKey, userId); // since userId null, use localStorage value
124+
localStorage.setItem('amplitude_deviceId' + '_' + apiKey, deviceId);
125+
localStorage.setItem('amplitude_userId' + '_' + apiKey, 'test_bad_user_id'); // ignored
120126
localStorage.setItem('amplitude_optOut' + '_' + apiKey, true); // ignored
121127

122128
var amplitude2 = new Amplitude();

0 commit comments

Comments
 (0)