Skip to content

Commit 83e1a20

Browse files
committed
Cookie bugfixes
1 parent 43274d6 commit 83e1a20

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

amplitude.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
178178
expirationDays: this.options.cookieExpiration,
179179
domain: this.options.domain
180180
});
181+
this.options.domain = Cookie.options().domain;
181182

182183
_loadCookieData(this);
183184

@@ -226,7 +227,6 @@ Amplitude.prototype.nextEventId = function() {
226227

227228
var _loadCookieData = function(scope) {
228229
var cookieData = Cookie.get(scope.options.cookieName);
229-
var cookieData = null;
230230
if (cookieData) {
231231
if (cookieData.deviceId) {
232232
scope.options.deviceId = cookieData.deviceId;
@@ -628,36 +628,36 @@ var get = function(name) {
628628

629629
var set = function(name, value) {
630630
try {
631-
_set(name, Base64.encode(JSON.stringify(value)));
631+
_set(name, Base64.encode(JSON.stringify(value)), _options);
632632
return true;
633633
} catch (e) {
634634
return false;
635635
}
636636
};
637637

638638

639-
var _set = function(name, value) {
640-
var expires = null;
641-
if (_options.expirationDays) {
639+
var _set = function(name, value, opts) {
640+
var expires = value != null ? opts.expirationDays : -1 ;
641+
if (expires) {
642642
var date = new Date();
643-
date.setTime(date.getTime() + (_options.expirationDays * 24 * 60 * 60 * 1000));
643+
date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000));
644644
expires = date;
645645
}
646646
var str = name + '=' + value;
647647
if (expires) {
648648
str += '; expires=' + expires.toUTCString();
649649
}
650650
str += '; path=/';
651-
if (_options.domain) {
652-
str += '; domain=' + _options.domain;
651+
if (opts.domain) {
652+
str += '; domain=' + opts.domain;
653653
}
654654
document.cookie = str;
655655
};
656656

657657

658658
var remove = function(name) {
659659
try {
660-
_set(name, '');
660+
_set(name, null, _options);
661661
return true;
662662
} catch (e) {
663663
return false;
@@ -671,6 +671,7 @@ module.exports = {
671671
get: get,
672672
set: set,
673673
remove: remove
674+
674675
};
675676

676677
}, {"./base64":3,"json":5,"top-domain":14}],
@@ -2343,7 +2344,7 @@ module.exports = localStorage;
23432344

23442345
}, {}],
23452346
12: [function(require, module, exports) {
2346-
module.exports = '1.4.0';
2347+
module.exports = '2.0.0';
23472348

23482349
}, {}],
23492350
13: [function(require, module, exports) {

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
6868
expirationDays: this.options.cookieExpiration,
6969
domain: this.options.domain
7070
});
71+
this.options.domain = Cookie.options().domain;
7172

7273
_loadCookieData(this);
7374

@@ -116,7 +117,6 @@ Amplitude.prototype.nextEventId = function() {
116117

117118
var _loadCookieData = function(scope) {
118119
var cookieData = Cookie.get(scope.options.cookieName);
119-
var cookieData = null;
120120
if (cookieData) {
121121
if (cookieData.deviceId) {
122122
scope.options.deviceId = cookieData.deviceId;

src/cookie.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,36 @@ var get = function(name) {
7272

7373
var set = function(name, value) {
7474
try {
75-
_set(name, Base64.encode(JSON.stringify(value)));
75+
_set(name, Base64.encode(JSON.stringify(value)), _options);
7676
return true;
7777
} catch (e) {
7878
return false;
7979
}
8080
};
8181

8282

83-
var _set = function(name, value) {
84-
var expires = null;
85-
if (_options.expirationDays) {
83+
var _set = function(name, value, opts) {
84+
var expires = value != null ? opts.expirationDays : -1 ;
85+
if (expires) {
8686
var date = new Date();
87-
date.setTime(date.getTime() + (_options.expirationDays * 24 * 60 * 60 * 1000));
87+
date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000));
8888
expires = date;
8989
}
9090
var str = name + '=' + value;
9191
if (expires) {
9292
str += '; expires=' + expires.toUTCString();
9393
}
9494
str += '; path=/';
95-
if (_options.domain) {
96-
str += '; domain=' + _options.domain;
95+
if (opts.domain) {
96+
str += '; domain=' + opts.domain;
9797
}
9898
document.cookie = str;
9999
};
100100

101101

102102
var remove = function(name) {
103103
try {
104-
_set(name, '');
104+
_set(name, null, _options);
105105
return true;
106106
} catch (e) {
107107
return false;
@@ -115,4 +115,5 @@ module.exports = {
115115
get: get,
116116
set: set,
117117
remove: remove
118+
118119
};

src/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = '1.4.0';
1+
module.exports = '2.0.0';

0 commit comments

Comments
 (0)