Skip to content

Commit a682c5b

Browse files
fix: wait to create cookie storage based on user options in init() (#579)
* fix: wait to create cookie storage based on user options in init() * chore: add test for disableCookies cookie creation * chore: add test for disableCookies=false cookie creation * chore: remove duplicated test
1 parent e4b5e83 commit a682c5b

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/amplitude-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
4747
plan: { ...DEFAULT_OPTIONS.plan },
4848
trackingOptions: { ...DEFAULT_OPTIONS.trackingOptions },
4949
};
50-
this.cookieStorage = new cookieStorage().getStorage();
5150
this._q = []; // queue for proxied functions before script load
5251
this._sending = false;
5352
this._updateScheduled = false;
@@ -127,6 +126,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
127126

128127
this._cookieName = Constants.COOKIE_PREFIX + '_' + this._storageSuffixV5;
129128

129+
this.cookieStorage = new cookieStorage().getStorage(this.options.disableCookies);
130130
this.cookieStorage.options({
131131
expirationDays: this.options.cookieExpiration,
132132
domain: this.options.domain,

src/cookiestorage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ var cookieStorage = function () {
1212
this.storage = null;
1313
};
1414

15-
cookieStorage.prototype.getStorage = function () {
15+
cookieStorage.prototype.getStorage = function (disableCookies) {
1616
if (this.storage !== null) {
1717
return this.storage;
1818
}
1919

20-
if (baseCookie.areCookiesEnabled()) {
20+
if (!disableCookies && baseCookie.areCookiesEnabled()) {
2121
this.storage = Cookie;
2222
} else {
2323
// if cookies disabled, fallback to localstorage

test/amplitude-client.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import { mockCookie, restoreCookie, getCookie } from './mock-cookie';
1313
import { AmplitudeServerZone } from '../src/server-zone.js';
1414
import Request from '../src/xhr';
1515

16+
const deleteAllCookies = () =>
17+
document.cookie.split(';').forEach(function (c) {
18+
document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
19+
});
20+
21+
const getAllCookies = () =>
22+
document.cookie
23+
.split(';')
24+
.map((c) => c.trimStart())
25+
.filter((c) => !utils.isEmptyString(c));
26+
1627
// maintain for testing backwards compatability
1728
describe('AmplitudeClient', function () {
1829
var apiKey = '000000';
@@ -888,11 +899,11 @@ describe('AmplitudeClient', function () {
888899
var onErrorSpy = sinon.spy();
889900

890901
var amplitude = new AmplitudeClient();
891-
sinon.stub(amplitude.cookieStorage, 'options').throws();
902+
sinon.stub(amplitude, '_refreshDynamicConfig').throws();
892903
amplitude.init(apiKey, null, { onError: onErrorSpy });
893904
assert.isTrue(onErrorSpy.calledOnce);
894905

895-
amplitude.cookieStorage.options.restore();
906+
amplitude['_refreshDynamicConfig'].restore();
896907
});
897908

898909
it('should set observer plan options', function () {
@@ -2728,6 +2739,44 @@ describe('AmplitudeClient', function () {
27282739
});
27292740
});
27302741

2742+
it('should not create any cookies 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 create cookies if disabledCookies = false', function () {
2762+
deleteAllCookies();
2763+
clock.tick(20);
2764+
2765+
var cookieArray = getAllCookies();
2766+
assert.equal(cookieArray.length, 0);
2767+
2768+
var deviceId = 'test_device_id';
2769+
var amplitude2 = new AmplitudeClient();
2770+
2771+
amplitude2.init(apiKey, null, {
2772+
deviceId: deviceId,
2773+
disableCookies: false,
2774+
});
2775+
2776+
cookieArray = getAllCookies();
2777+
assert.equal(cookieArray.length, 1);
2778+
});
2779+
27312780
it('should validate event properties', function () {
27322781
var e = new Error('oops');
27332782
clock.tick(1);

0 commit comments

Comments
 (0)