@segment/analytics.js-core
Advanced tools
Comparing version 3.8.0 to 3.8.1
@@ -109,9 +109,44 @@ 'use strict'; | ||
Entity.prototype._getId = function() { | ||
var ret = this._options.persist | ||
? this.storage().get(this._options.cookie.key) | ||
: this._id; | ||
return ret === undefined ? null : ret; | ||
if (!this._options.persist) { | ||
return this._id === undefined ? null : this._id; | ||
} | ||
// Check cookies. | ||
var cookieId = this._getIdFromCookie(); | ||
if (cookieId) { | ||
return cookieId; | ||
} | ||
// Check localStorage. | ||
var lsId = this._getIdFromLocalStorage(); | ||
if (lsId) { | ||
// Copy the id to cookies so we can read it directly from cookies next time. | ||
this._setIdInCookies(lsId); | ||
return lsId; | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Get the entity's id from cookies. | ||
* | ||
* @return {String} | ||
*/ | ||
Entity.prototype._getIdFromCookie = function() { | ||
return this.storage().get(this._options.cookie.key); | ||
}; | ||
/** | ||
* Get the entity's id from cookies. | ||
* | ||
* @return {String} | ||
*/ | ||
Entity.prototype._getIdFromLocalStorage = function() { | ||
return store.get(this._options.cookie.key); | ||
}; | ||
/** | ||
* Set the entity's `id`. | ||
@@ -124,3 +159,4 @@ * | ||
if (this._options.persist) { | ||
this.storage().set(this._options.cookie.key, id); | ||
this._setIdInCookies(id); | ||
this._setIdInLocalStorage(id); | ||
} else { | ||
@@ -132,2 +168,22 @@ this._id = id; | ||
/** | ||
* Set the entity's `id` in cookies. | ||
* | ||
* @param {String} id | ||
*/ | ||
Entity.prototype._setIdInCookies = function(id) { | ||
this.storage().set(this._options.cookie.key, id); | ||
}; | ||
/** | ||
* Set the entity's `id` in local storage. | ||
* | ||
* @param {String} id | ||
*/ | ||
Entity.prototype._setIdInLocalStorage = function(id) { | ||
store.set(this._options.cookie.key, id); | ||
}; | ||
/** | ||
* Get or set the entity's `traits`. | ||
@@ -207,4 +263,4 @@ * | ||
if (!this._options.persist) return false; | ||
cookie.set(this._options.cookie.key, this.id()); | ||
store.set(this._options.localStorage.key, this.traits()); | ||
this._setId(this.id()); | ||
this._setTraits(this.traits()); | ||
return true; | ||
@@ -220,3 +276,4 @@ }; | ||
this.traits({}); | ||
cookie.remove(this._options.cookie.key); | ||
this.storage().remove(this._options.cookie.key); | ||
store.remove(this._options.cookie.key); | ||
store.remove(this._options.localStorage.key); | ||
@@ -239,4 +296,4 @@ }; | ||
Entity.prototype.load = function() { | ||
this.id(cookie.get(this._options.cookie.key)); | ||
this.traits(store.get(this._options.localStorage.key)); | ||
this.id(this.id()); | ||
this.traits(this.traits()); | ||
}; |
@@ -14,2 +14,3 @@ 'use strict'; | ||
var uuid = require('uuid'); | ||
var localStorage = require('./store'); | ||
@@ -103,2 +104,3 @@ /** | ||
store.set('ajs_anonymous_id', anonymousId); | ||
localStorage.set('ajs_anonymous_id', anonymousId); | ||
return this; | ||
@@ -110,5 +112,17 @@ } | ||
if (anonymousId) { | ||
// value exist in cookie, copy it to localStorage | ||
localStorage.set('ajs_anonymous_id', anonymousId); | ||
// refresh cookie to extend expiry | ||
store.set('ajs_anonymous_id', anonymousId); | ||
return anonymousId; | ||
} | ||
// if anonymousId doesn't exist in cookies, check localStorage | ||
anonymousId = localStorage.get('ajs_anonymous_id'); | ||
if (anonymousId) { | ||
// Write to cookies if available in localStorage but not cookies | ||
store.set('ajs_anonymous_id', anonymousId); | ||
return anonymousId; | ||
} | ||
// old - it is not stringified so we use the raw cookie. | ||
@@ -119,2 +133,3 @@ anonymousId = rawCookie('_sio'); | ||
store.set('ajs_anonymous_id', anonymousId); | ||
localStorage.set('ajs_anonymous_id', anonymousId); | ||
store.remove('_sio'); | ||
@@ -127,2 +142,3 @@ return anonymousId; | ||
store.set('ajs_anonymous_id', anonymousId); | ||
localStorage.set('ajs_anonymous_id', anonymousId); | ||
return store.get('ajs_anonymous_id'); | ||
@@ -129,0 +145,0 @@ }; |
{ | ||
"name": "@segment/analytics.js-core", | ||
"author": "Segment <friends@segment.com>", | ||
"version": "3.8.0", | ||
"version": "3.8.1", | ||
"description": "The hassle-free way to integrate analytics into any web application.", | ||
@@ -18,12 +18,6 @@ "keywords": [ | ||
"precommit": "lint-staged", | ||
"np": "np --no-publish" | ||
"np": "np --no-publish", | ||
"cz": "git-cz", | ||
"commitmsg": "commitlint -E GIT_PARAMS" | ||
}, | ||
"lint-staged": { | ||
"linters": { | ||
"*.{js,json,md}": [ | ||
"prettier-eslint --write", | ||
"git add" | ||
] | ||
} | ||
}, | ||
"repository": { | ||
@@ -70,9 +64,11 @@ "type": "git", | ||
"json3": "^3.3.2", | ||
"spark-md5": "^2.0.2", | ||
"new-date": "^1.0.0", | ||
"next-tick": "^0.2.2", | ||
"segmentio-facade": "^3.0.2", | ||
"spark-md5": "^2.0.2", | ||
"uuid": "^2.0.2" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "^7.0.0", | ||
"@commitlint/config-conventional": "^7.0.1", | ||
"@segment/analytics.js-integration": "^3.2.1", | ||
@@ -83,4 +79,7 @@ "@segment/eslint-config": "^4.0.0", | ||
"codecov": "^3.0.2", | ||
"commitizen": "^2.10.1", | ||
"commitlint-circle": "^1.0.0", | ||
"compat-trigger-event": "^1.0.0", | ||
"component-each": "^0.2.6", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint": "^4.19.1", | ||
@@ -113,3 +112,21 @@ "eslint-config-prettier": "^2.9.0", | ||
"watchify": "^3.7.0" | ||
}, | ||
"commitlint": { | ||
"extends": [ | ||
"@commitlint/config-conventional" | ||
] | ||
}, | ||
"lint-staged": { | ||
"linters": { | ||
"*.{js,json,md}": [ | ||
"prettier-eslint --write", | ||
"git add" | ||
] | ||
} | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "cz-conventional-changelog" | ||
} | ||
} | ||
} |
@@ -42,2 +42,20 @@ 'use strict'; | ||
}); | ||
it('id() should fallback to localStorage', function() { | ||
var group = new Group(); | ||
group.id('gid'); | ||
// delete the cookie. | ||
cookie.remove(cookieKey); | ||
// verify cookie is deleted. | ||
assert.equal(cookie.get(cookieKey), null); | ||
// verify id() returns the id even when cookie is deleted. | ||
assert.equal(group.id(), 'gid'); | ||
// verify cookie value is retored from localStorage. | ||
assert.equal(cookie.get(cookieKey), 'gid'); | ||
}); | ||
}); | ||
@@ -230,2 +248,8 @@ | ||
it('should save an id to localStorage', function() { | ||
group.id('id'); | ||
group.save(); | ||
assert(store.get(cookieKey) === 'id'); | ||
}); | ||
it('should save properties to local storage', function() { | ||
@@ -254,3 +278,3 @@ group.properties({ property: true }); | ||
it('should clear a cookie', function() { | ||
it('should clear id in cookie', function() { | ||
group.id('id'); | ||
@@ -262,3 +286,10 @@ group.save(); | ||
it('should clear local storage', function() { | ||
it('should clear id in localStorage', function() { | ||
group.id('id'); | ||
group.save(); | ||
group.logout(); | ||
assert(store.get(cookieKey) === undefined); | ||
}); | ||
it('should clear traits in local storage', function() { | ||
group.properties({ property: true }); | ||
@@ -265,0 +296,0 @@ group.save(); |
@@ -46,2 +46,20 @@ 'use strict'; | ||
it('id() should fallback to localStorage', function() { | ||
var user = new User(); | ||
user.id('id'); | ||
// delete the cookie. | ||
cookie.remove(cookieKey); | ||
// verify cookie is deleted. | ||
assert.equal(cookie.get(cookieKey), null); | ||
// verify id() returns the id even when cookie is deleted. | ||
assert.equal(user.id(), 'id'); | ||
// verify cookie value is retored from localStorage. | ||
assert.equal(cookie.get(cookieKey), 'id'); | ||
}); | ||
it('should pick the old "_sio" anonymousId', function() { | ||
@@ -323,2 +341,41 @@ rawCookie('_sio', 'anonymous-id----user-id'); | ||
}); | ||
it('should set anonymousId in both cookie and localStorage', function() { | ||
var user = new User(); | ||
user.anonymousId('anon0'); | ||
assert.equal(cookie.get('ajs_anonymous_id'), 'anon0'); | ||
assert.equal(store.get('ajs_anonymous_id'), 'anon0'); | ||
}); | ||
it('should copy value from cookie to localStorage', function() { | ||
var user = new User(); | ||
cookie.set('ajs_anonymous_id', 'anon1'); | ||
assert.equal(user.anonymousId(), 'anon1'); | ||
assert.equal(store.get('ajs_anonymous_id'), 'anon1'); | ||
}); | ||
it('should fall back to localStorage when cookie is not set', function() { | ||
var user = new User(); | ||
user.anonymousId('anon12'); | ||
assert.equal(cookie.get('ajs_anonymous_id'), 'anon12'); | ||
// delete the cookie | ||
cookie.remove('ajs_anonymous_id'); | ||
assert.equal(cookie.get('ajs_anonymous_id'), null); | ||
// verify anonymousId() returns the correct id even when there's no cookie | ||
assert.equal(user.anonymousId(), 'anon12'); | ||
// verify cookie value is retored from localStorage | ||
assert.equal(cookie.get('ajs_anonymous_id'), 'anon12'); | ||
}); | ||
it('should write to both cookie and localStorage when generating a new anonymousId', function() { | ||
var user = new User(); | ||
var anonId = user.anonymousId(); | ||
assert.notEqual(anonId, null); | ||
assert.equal(cookie.get('ajs_anonymous_id'), anonId); | ||
assert.equal(store.get('ajs_anonymous_id'), anonId); | ||
}); | ||
}); | ||
@@ -405,2 +462,8 @@ }); | ||
it('should save an id to localStorage', function() { | ||
user.id('id'); | ||
user.save(); | ||
assert.equal(store.get(cookieKey), 'id'); | ||
}); | ||
it('should save traits to local storage', function() { | ||
@@ -431,3 +494,3 @@ user.traits({ trait: true }); | ||
it('should clear a cookie', function() { | ||
it('should clear id in cookie', function() { | ||
user.id('id'); | ||
@@ -439,3 +502,10 @@ user.save(); | ||
it('should clear local storage', function() { | ||
it('should clear id in local storage', function() { | ||
user.id('id'); | ||
user.save(); | ||
user.logout(); | ||
assert(store.get(cookieKey) === undefined); | ||
}); | ||
it('should clear traits in local storage', function() { | ||
user.traits({ trait: true }); | ||
@@ -442,0 +512,0 @@ user.save(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
451864
4932
0
39