Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 622b893

Browse files
authored
Merge pull request #109 from segmentio/gate-localStorage
feat(ids): add `localStorageFallbackDisabled` flag
2 parents abc4678 + 1e769c6 commit 622b893

File tree

4 files changed

+141
-15
lines changed

4 files changed

+141
-15
lines changed

lib/entity.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ Entity.prototype._getIdFromCookie = function() {
144144
*/
145145

146146
Entity.prototype._getIdFromLocalStorage = function() {
147-
return store.get(this._options.cookie.key);
147+
if (!this._options.localStorageFallbackDisabled) {
148+
return store.get(this._options.cookie.key);
149+
}
150+
return null;
148151
};
149152

150153
/**
@@ -179,7 +182,9 @@ Entity.prototype._setIdInCookies = function(id) {
179182
*/
180183

181184
Entity.prototype._setIdInLocalStorage = function(id) {
182-
store.set(this._options.cookie.key, id);
185+
if (!this._options.localStorageFallbackDisabled) {
186+
store.set(this._options.cookie.key, id);
187+
}
183188
};
184189

185190
/**

lib/user.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -100,45 +100,59 @@ User.prototype.anonymousId = function(anonymousId) {
100100
// set / remove
101101
if (arguments.length) {
102102
store.set('ajs_anonymous_id', anonymousId);
103-
localStorage.set('ajs_anonymous_id', anonymousId);
103+
this._setAnonymousIdInLocalStorage(anonymousId);
104104
return this;
105105
}
106106

107107
// new
108108
anonymousId = store.get('ajs_anonymous_id');
109109
if (anonymousId) {
110-
// value exist in cookie, copy it to localStorage
111-
localStorage.set('ajs_anonymous_id', anonymousId);
110+
// value exists in cookie, copy it to localStorage
111+
this._setAnonymousIdInLocalStorage(anonymousId);
112112
// refresh cookie to extend expiry
113113
store.set('ajs_anonymous_id', anonymousId);
114114
return anonymousId;
115115
}
116116

117-
// if anonymousId doesn't exist in cookies, check localStorage
118-
anonymousId = localStorage.get('ajs_anonymous_id');
119-
if (anonymousId) {
120-
// Write to cookies if available in localStorage but not cookies
121-
store.set('ajs_anonymous_id', anonymousId);
122-
return anonymousId;
117+
if (!this._options.localStorageFallbackDisabled) {
118+
// if anonymousId doesn't exist in cookies, check localStorage
119+
anonymousId = localStorage.get('ajs_anonymous_id');
120+
if (anonymousId) {
121+
// Write to cookies if available in localStorage but not cookies
122+
store.set('ajs_anonymous_id', anonymousId);
123+
return anonymousId;
124+
}
123125
}
124126

125127
// old - it is not stringified so we use the raw cookie.
126128
anonymousId = rawCookie('_sio');
127129
if (anonymousId) {
128130
anonymousId = anonymousId.split('----')[0];
129131
store.set('ajs_anonymous_id', anonymousId);
130-
localStorage.set('ajs_anonymous_id', anonymousId);
132+
this._setAnonymousIdInLocalStorage(anonymousId);
131133
store.remove('_sio');
132134
return anonymousId;
133135
}
134136

135137
// empty
136138
anonymousId = uuid.v4();
137139
store.set('ajs_anonymous_id', anonymousId);
138-
localStorage.set('ajs_anonymous_id', anonymousId);
140+
this._setAnonymousIdInLocalStorage(anonymousId);
139141
return store.get('ajs_anonymous_id');
140142
};
141143

144+
/**
145+
* Set the user's `anonymousid` in local storage.
146+
*
147+
* @param {String} id
148+
*/
149+
150+
User.prototype._setAnonymousIdInLocalStorage = function(id) {
151+
if (!this._options.localStorageFallbackDisabled) {
152+
localStorage.set('ajs_anonymous_id', id);
153+
}
154+
};
155+
142156
/**
143157
* Remove anonymous id on logout too.
144158
*/

test/group.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('group', function() {
5757
// verify cookie value is retored from localStorage.
5858
assert.equal(cookie.get(cookieKey), 'gid');
5959
});
60+
61+
it('id() should not fallback to localStorage when localStorage fallback is disabled', function() {
62+
var group = new Group();
63+
group.options({
64+
localStorageFallbackDisabled: true
65+
});
66+
67+
group.id('gid');
68+
69+
// delete the cookie.
70+
cookie.remove(cookieKey);
71+
72+
// verify cookie is deleted.
73+
assert.equal(cookie.get(cookieKey), null);
74+
75+
// verify id() does not return the id when cookie is deleted.
76+
assert.equal(group.id(), null);
77+
});
6078
});
6179

6280
describe('#id', function() {
@@ -250,6 +268,16 @@ describe('group', function() {
250268
assert(store.get(cookieKey) === 'id');
251269
});
252270

271+
it('should not save an id to localStorage when localStorage fallback is disabled', function() {
272+
group.options({
273+
localStorageFallbackDisabled: true
274+
});
275+
276+
group.id('id');
277+
group.save();
278+
assert.equal(store.get(cookieKey), null);
279+
});
280+
253281
it('should save properties to local storage', function() {
254282
group.properties({ property: true });
255283
group.save();

test/user.test.js

+81-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,28 @@ describe('user', function() {
5757
// verify id() returns the id even when cookie is deleted.
5858
assert.equal(user.id(), 'id');
5959

60-
// verify cookie value is retored from localStorage.
60+
// verify cookie value is restored from localStorage.
6161
assert.equal(cookie.get(cookieKey), 'id');
6262
});
6363

64+
it('id() should not fallback to localStorage when disabled', function() {
65+
var user = new User();
66+
user.options({
67+
localStorageFallbackDisabled: true
68+
});
69+
70+
user.id('id');
71+
72+
// delete the cookie.
73+
cookie.remove(cookieKey);
74+
75+
// verify cookie is deleted.
76+
assert.equal(cookie.get(cookieKey), null);
77+
78+
// verify id() does not return the id when cookie is deleted.
79+
assert.equal(user.id(), null);
80+
});
81+
6482
it('should pick the old "_sio" anonymousId', function() {
6583
rawCookie('_sio', 'anonymous-id----user-id');
6684
var user = new User();
@@ -345,13 +363,33 @@ describe('user', function() {
345363
assert.equal(store.get('ajs_anonymous_id'), 'anon0');
346364
});
347365

366+
it('should not set anonymousId in localStorage when localStorage fallback is disabled', function() {
367+
var user = new User();
368+
user.options({
369+
localStorageFallbackDisabled: true
370+
});
371+
user.anonymousId('anon0');
372+
assert.equal(cookie.get('ajs_anonymous_id'), 'anon0');
373+
assert.equal(store.get('ajs_anonymous_id'), null);
374+
});
375+
348376
it('should copy value from cookie to localStorage', function() {
349377
var user = new User();
350378
cookie.set('ajs_anonymous_id', 'anon1');
351379
assert.equal(user.anonymousId(), 'anon1');
352380
assert.equal(store.get('ajs_anonymous_id'), 'anon1');
353381
});
354382

383+
it('should not copy value from cookie to localStorage when localStorage fallback is disabled', function() {
384+
var user = new User();
385+
user.options({
386+
localStorageFallbackDisabled: true
387+
});
388+
cookie.set('ajs_anonymous_id', 'anon1');
389+
assert.equal(user.anonymousId(), 'anon1');
390+
assert.equal(store.get('ajs_anonymous_id'), null);
391+
});
392+
355393
it('should fall back to localStorage when cookie is not set', function() {
356394
var user = new User();
357395

@@ -365,17 +403,47 @@ describe('user', function() {
365403
// verify anonymousId() returns the correct id even when there's no cookie
366404
assert.equal(user.anonymousId(), 'anon12');
367405

368-
// verify cookie value is retored from localStorage
406+
// verify cookie value is restored from localStorage
369407
assert.equal(cookie.get('ajs_anonymous_id'), 'anon12');
370408
});
371409

410+
it('should not fall back to localStorage when cookie is not set and localStorage fallback is disabled', function() {
411+
var user = new User();
412+
user.options({
413+
localStorageFallbackDisabled: true
414+
});
415+
416+
user.anonymousId('anon12');
417+
assert.equal(cookie.get('ajs_anonymous_id'), 'anon12');
418+
419+
// delete the cookie
420+
cookie.remove('ajs_anonymous_id');
421+
assert.equal(cookie.get('ajs_anonymous_id'), null);
422+
423+
// verify anonymousId() does not return the id when there's no cookie.
424+
assert.notEqual(user.anonymousId(), 'anon12');
425+
});
426+
372427
it('should write to both cookie and localStorage when generating a new anonymousId', function() {
373428
var user = new User();
374429
var anonId = user.anonymousId();
375430
assert.notEqual(anonId, null);
376431
assert.equal(cookie.get('ajs_anonymous_id'), anonId);
377432
assert.equal(store.get('ajs_anonymous_id'), anonId);
378433
});
434+
435+
it('should not write to both cookie and localStorage when generating a new anonymousId and localStorage fallback is disabled', function() {
436+
var user = new User();
437+
user.options({
438+
localStorageFallbackDisabled: true
439+
});
440+
441+
var anonId = user.anonymousId();
442+
443+
assert.notEqual(anonId, null);
444+
assert.equal(cookie.get('ajs_anonymous_id'), anonId);
445+
assert.equal(store.get('ajs_anonymous_id'), null);
446+
});
379447
});
380448
});
381449

@@ -463,6 +531,17 @@ describe('user', function() {
463531
assert.equal(store.get(cookieKey), 'id');
464532
});
465533

534+
it('should not save an id to localStorage when localStorage fallback is disabled', function() {
535+
user.options({
536+
localStorageFallbackDisabled: true
537+
});
538+
user.id('id');
539+
540+
user.save();
541+
542+
assert.equal(store.get(cookieKey), null);
543+
});
544+
466545
it('should save traits to local storage', function() {
467546
user.traits({ trait: true });
468547
user.save();

0 commit comments

Comments
 (0)