Skip to content

Commit 81d2ac1

Browse files
committed
fix: skip top domain when cookie is disabled
1 parent 04ed78d commit 81d2ac1

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/base-cookie.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Constants from './constants';
2-
import base64Id from './base64Id';
32
import utils from './utils';
43

54
const get = (name) => {
@@ -94,7 +93,7 @@ const sortByEventTime = (cookies) => {
9493

9594
// test that cookies are enabled - navigator.cookiesEnabled yields false positives in IE, need to test directly
9695
const areCookiesEnabled = (opts = {}) => {
97-
const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();
96+
const cookieName = Constants.COOKIE_TEST_PREFIX;
9897
if (typeof document === 'undefined') {
9998
return false;
10099
}

src/metadata-storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MetadataStorage {
3636

3737
this.cookieDomain = '';
3838
const loc = getLocation() ? getLocation().href : undefined;
39-
const writableTopDomain = topDomain(loc);
39+
const writableTopDomain = !disableCookies ? topDomain(loc) : '';
4040
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);
4141

4242
if (storageOptionExists[storage]) {

test/amplitude-client.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,27 @@ describe('AmplitudeClient', function () {
27392739
});
27402740
});
27412741

2742-
it('should not create any cookies if disabledCookies = true', function () {
2742+
it('should not create any cookies with simple host if disabledCookies = true', function () {
2743+
deleteAllCookies();
2744+
clock.tick(20);
2745+
2746+
var cookieArray = getAllCookies();
2747+
assert.equal(cookieArray.length, 0);
2748+
2749+
var deviceId = 'test_device_id';
2750+
var amplitude2 = new AmplitudeClient();
2751+
2752+
amplitude2.init(apiKey, null, {
2753+
deviceId: deviceId,
2754+
disableCookies: true,
2755+
});
2756+
2757+
cookieArray = getAllCookies();
2758+
assert.equal(cookieArray.length, 0);
2759+
});
2760+
2761+
it('should not create any cookies with multiple domains in host if disabledCookies = true', function () {
2762+
window.href = 'https://abc.def.xyz/test';
27432763
deleteAllCookies();
27442764
clock.tick(20);
27452765

0 commit comments

Comments
 (0)