Skip to content

Commit

Permalink
AMP-0 Fix cookie migration paths
Browse files Browse the repository at this point in the history
  • Loading branch information
blazzy committed Mar 23, 2018
1 parent 826ce88 commit 80596b4
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 14 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Unreleased

### 4.1.0 (January 18, 2017)
### 4.1.1 (March 22, 2018)

* Fix bug where cookie data such as device id from older releases were not updated

### 4.1.0 (January 18, 2018)

* Allow for numeric user ids

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Please see our [installation guide](https://amplitude.zendesk.com/hc/en-us/artic
[![npm version](https://badge.fury.io/js/amplitude-js.svg)](https://badge.fury.io/js/amplitude-js)
[![Bower version](https://badge.fury.io/bo/amplitude-js.svg)](https://badge.fury.io/bo/amplitude-js)

[4.1.0 - Released on January 18, 2018](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)
[4.1.1 - Released on March 22, 2018](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)


# JavaScript SDK Reference #
Expand Down
2 changes: 1 addition & 1 deletion amplitude-snippet.min.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script")
;r.type="text/javascript";r.async=true
;r.src="https://cdn.amplitude.com/libs/amplitude-4.1.0-min.gz.js"
;r.src="https://cdn.amplitude.com/libs/amplitude-4.1.1-min.gz.js"
;r.onload=function(){if(e.amplitude.runQueuedFunctions){
e.amplitude.runQueuedFunctions()}else{
console.log("[Amplitude] Error: could not load SDK")}}
Expand Down
9 changes: 7 additions & 2 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -5627,7 +5627,7 @@ var uuid$1 = function uuid(a) {
);
};

var version = '4.1.0';
var version = '4.1.1';

var getLanguage = function getLanguage() {
return navigator && (navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage) || undefined;
Expand Down Expand Up @@ -6026,8 +6026,13 @@ AmplitudeClient.prototype._setInStorage = function _setInStorage(storage, key, v
* @private
*/
var _upgradeCookeData = function _upgradeCookeData(scope) {
// skip if migration already happened
// skip if already migrated to 4.10+
var cookieData = scope.cookieStorage.get(scope.options.cookieName + scope._storageSuffix);
if (type(cookieData) === 'object') {
return;
}
// skip if already migrated to 2.70+
cookieData = scope.cookieStorage.get(scope.options.cookieName + scope._legacyStorageSuffix);
if (type(cookieData) === 'object' && cookieData.deviceId && cookieData.sessionId && cookieData.lastEventTime) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion amplitude.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "amplitude-js",
"author": "Amplitude <[email protected]>",
"version": "4.1.0",
"version": "4.1.1",
"license": "MIT",
"description": "Javascript library for Amplitude Analytics",
"keywords": [
Expand Down
7 changes: 6 additions & 1 deletion src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,13 @@ AmplitudeClient.prototype._setInStorage = function _setInStorage(storage, key, v
* @private
*/
var _upgradeCookeData = function _upgradeCookeData(scope) {
// skip if migration already happened
// skip if already migrated to 4.10+
var cookieData = scope.cookieStorage.get(scope.options.cookieName + scope._storageSuffix);
if (type(cookieData) === 'object') {
return;
}
// skip if already migrated to 2.70+
cookieData = scope.cookieStorage.get(scope.options.cookieName + scope._legacyStorageSuffix);
if (type(cookieData) === 'object' && cookieData.deviceId && cookieData.sessionId && cookieData.lastEventTime) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/amplitude-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var as = document.createElement('script');
as.type = 'text/javascript';
as.async = true;
as.src = 'https://cdn.amplitude.com/libs/amplitude-4.1.0-min.gz.js';
as.src = 'https://cdn.amplitude.com/libs/amplitude-4.1.1-min.gz.js';
as.onload = function() {if(window.amplitude.runQueuedFunctions) {window.amplitude.runQueuedFunctions();} else {console.log('[Amplitude] Error: could not load SDK');}};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(as, s);
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.1.0';
export default '4.1.1';
37 changes: 34 additions & 3 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,41 @@ describe('AmplitudeClient', function() {
assert.equal(cookieData.sequenceNumber, 5000);
});

it('should migrate cookie data from old cookie name and ignore local storage values', function(){
it('should load device id from the cookie', function(){
var now = new Date().getTime();

// deviceId and sequenceNumber not set, init should load value from localStorage
var cookieData = {
deviceId: 'current_device_id',
}

cookie.set(amplitude.options.cookieName + '_' + apiKey, cookieData);

amplitude.init(apiKey);
assert.equal(amplitude.options.deviceId, 'current_device_id');
});

it('should migrate device id from old non name spaced cookie name (pre 4.10)', function(){
var now = new Date().getTime();

var cookieData = {
deviceId: 'old_device_id',
optOut: false,
sessionId: now,
lastEventTime: now,
eventId: 50,
identifyId: 60
}

cookie.set(amplitude.options.cookieName, cookieData);

amplitude.init(apiKey);
assert.equal(amplitude.options.deviceId, 'old_device_id');
});

it('should migrate cookie data from old cookie name and ignore local storage values', function(){
var now = new Date().getTime();

var cookieData = {
userId: 'test_user_id',
optOut: false,
Expand All @@ -254,7 +285,7 @@ describe('AmplitudeClient', function() {
identifyId: 60
}

cookie.set(amplitude.options.cookieName + '_' + apiKey, cookieData);
cookie.set(amplitude.options.cookieName, cookieData);
localStorage.setItem('amplitude_deviceId' + keySuffix, 'old_device_id');
localStorage.setItem('amplitude_userId' + keySuffix, 'fake_user_id');
localStorage.setItem('amplitude_optOut' + keySuffix, true);
Expand All @@ -278,7 +309,7 @@ describe('AmplitudeClient', function() {
it('should skip the migration if the new cookie already has deviceId, sessionId, lastEventTime', function() {
var now = new Date().getTime();

cookie.set(amplitude.options.cookieName + '_' + apiKey, {
cookie.set(amplitude.options.cookieName, {
deviceId: 'new_device_id',
sessionId: now,
lastEventTime: now
Expand Down
2 changes: 1 addition & 1 deletion test/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ describe('Amplitude', function() {
identifyId: 60
}

cookie.set(amplitude.options.cookieName + '_' + apiKey, cookieData);
cookie.set(amplitude.options.cookieName, cookieData);
localStorage.setItem('amplitude_deviceId' + keySuffix, 'old_device_id');
localStorage.setItem('amplitude_userId' + keySuffix, 'fake_user_id');
localStorage.setItem('amplitude_optOut' + keySuffix, true);
Expand Down

0 comments on commit 80596b4

Please sign in to comment.