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

fallback to localstorage #441

Merged
merged 2 commits into from
Mar 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,40 @@ module.exports = Entity;
*/

function Entity(options){
this.protocol = window.location.protocol;
this.options(options);
this.initialize();
}

/**
* Get the storage.
*
* When .protocol is `file:` or `chrome-extension:`
* the method will return the localstorage (store)
* otherwise it will return the cookie.
* Initialize picks the storage.
*
* @return {Object}
* Checks to see if cookies can be set
* otherwise fallsback to localStorage.
*/

Entity.prototype.initialize = function(){
cookie.set('ajs:cookies', true);

// cookies are enabled.
if (cookie.get('ajs:cookies')) {
cookie.remove('ajs:cookies');
this._storage = cookie;
return;
}

// fallback to localstorage.
this._storage = store;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can local storage ever be unavailable? And should we fallback to just generating a new anonymousId every time in that case with another temp store (or similar)?

};

/**
* Get the storage.
*/

Entity.prototype.storage = function(){
return 'file:' == this.protocol
|| 'chrome-extension:' == this.protocol
? store
: cookie;
return this._storage;
};


/**
* Get or set storage `options`.
*
Expand Down Expand Up @@ -80,9 +93,8 @@ Entity.prototype.id = function (id) {
*/

Entity.prototype._getId = function () {
var storage = this.storage();
var ret = this._options.persist
? storage.get(this._options.cookie.key)
? this.storage().get(this._options.cookie.key)
: this._id;
return ret === undefined ? null : ret;
};
Expand All @@ -95,9 +107,8 @@ Entity.prototype._getId = function () {
*/

Entity.prototype._setId = function (id) {
var storage = this.storage();
if (this._options.persist) {
storage.set(this._options.cookie.key, id);
this.storage().set(this._options.cookie.key, id);
} else {
this._id = id;
}
Expand Down
45 changes: 10 additions & 35 deletions test/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ describe('group', function () {
var cookie = Analytics.cookie;
var equal = require('equals');
var json = require('json');
var sinon = require('sinon');
var store = Analytics.store;
var group = analytics.group();
var Group = group.Group;

var cookieKey = group._options.cookie.key;
var localStorageKey = group._options.localStorage.key;

before(function () {
assert.equal(location.protocol, group.protocol);
beforeEach(function () {
group = new Group
group.reset();
});

Expand All @@ -41,41 +42,14 @@ describe('group', function () {
})

describe('#id', function () {
describe('when file:', function(){
describe('when cookies are disabled', function(){
beforeEach(function(){
group.protocol = 'file:';
sinon.stub(cookie, 'get', function(){});
group = new Group;
});

it('should get an id from the store', function () {
store.set(cookieKey, 'id');
assert('id' == group.id());
});

it('should get an id when not persisting', function () {
group.options({ persist: false });
group._id = 'id';
assert('id' == group.id());
});

it('should set an id to the store', function () {
group.id('id');
assert('id' === store.get(cookieKey));
});

it('should set the id when not persisting', function () {
group.options({ persist: false });
group.id('id');
assert('id' == group._id);
});

it('should be null by default', function () {
assert(null === group.id());
});
});

describe('when chrome-extension:', function(){
beforeEach(function(){
group.protocol = 'chrome-extension:';
afterEach(function(){
cookie.get.restore();
});

it('should get an id from the store', function () {
Expand Down Expand Up @@ -105,9 +79,10 @@ describe('group', function () {
});
});

describe('http:', function(){
describe('when cookies are enabled', function(){
it('should get an id from the cookie', function () {
cookie.set(cookieKey, 'id');

assert('id' == group.id());
});

Expand Down
93 changes: 17 additions & 76 deletions test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ describe('user', function () {
var store = Analytics.store;
var user = analytics.user();
var rawCookie = require('cookie');
var sinon = require('sinon');
var User = user.User;

var cookieKey = user._options.cookie.key;
var localStorageKey = user._options.localStorage.key;

before(function () {
assert.equal(location.protocol, user.protocol);
beforeEach(function () {
user = new User
user.reset();
});

Expand All @@ -28,7 +29,6 @@ describe('user', function () {
store.remove('_sio');
cookie.remove('_sio');
rawCookie('_sio', null);
user.protocol = location.protocol;
});

describe('()', function(){
Expand Down Expand Up @@ -67,11 +67,16 @@ describe('user', function () {
})

describe('#id', function () {
describe('when file:', function(){
describe('when cookies are disabled', function(){
beforeEach(function(){
user.protocol = 'file:';
sinon.stub(cookie, 'get', function(){});
user = new User;
});

afterEach(function(){
cookie.get.restore();
})

it('should get an id from the store', function () {
store.set(cookieKey, 'id');
assert('id' == user.id());
Expand Down Expand Up @@ -123,55 +128,7 @@ describe('user', function () {
});
});

describe('when chrome-extension:', function(){
beforeEach(function(){
user.protocol = 'chrome-extension:';
});

it('should get an id from the store', function () {
store.set(cookieKey, 'id');
assert('id' == user.id());
});

it('should get an id when not persisting', function () {
user.options({ persist: false });
user._id = 'id';
assert('id' == user.id());
});

it('should set an id to the store', function () {
user.id('id');
assert('id' === store.get(cookieKey));
});

it('should set the id when not persisting', function () {
user.options({ persist: false });
user.id('id');
assert('id' == user._id);
});

it('should be null by default', function () {
assert(null === user.id());
});

it('should not reset anonymousId if the user didnt have previous id', function(){
var prev = user.anonymousId();
user.id('foo');
user.id('foo');
user.id('foo');
assert.equal(prev, user.anonymousId());
});

it('should reset anonymousId if the user id changed', function(){
var prev = user.anonymousId();
user.id('foo');
user.id('baz');
assert.notEqual(prev, user.anonymousId());
assert.equal(36, user.anonymousId().length);
});
});

describe('when http:', function(){
describe('when cookies are enabled', function(){
it('should get an id from the cookie', function () {
cookie.set(cookieKey, 'id');
assert('id' == user.id());
Expand Down Expand Up @@ -224,30 +181,14 @@ describe('user', function () {
user.storage = storage;
});

describe('when file:', function(){
describe('when cookies are disabled', function(){
beforeEach(function(){
user.protocol = 'file:';
});

it('should get an id from the store', function () {
store.set('ajs_anonymous_id', 'anon-id');
assert('anon-id' == user.anonymousId());
});

it('should set an id to the store', function () {
user.anonymousId('anon-id');
assert('anon-id' === store.get('ajs_anonymous_id'));
});

it('should return anonymousId using the store', function(){
user.storage = function(){ return noop; };
assert(null == user.anonymousId());
sinon.stub(cookie, 'get', function(){});
user = new User;
});
});

describe('when chrome-extension:', function(){
beforeEach(function(){
user.protocol = 'chrome-extension:';
afterEach(function(){
cookie.get.restore();
});

it('should get an id from the store', function () {
Expand All @@ -266,7 +207,7 @@ describe('user', function () {
});
});

describe('when http:', function(){
describe('when cookies are enabled', function(){
it('should get an id from the cookie', function () {
cookie.set('ajs_anonymous_id', 'anon-id');
assert('anon-id' == user.anonymousId());
Expand Down