diff --git a/src/amplitude-client.js b/src/amplitude-client.js index d4f5de02..1d704d74 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -47,7 +47,6 @@ var AmplitudeClient = function AmplitudeClient(instanceName) { plan: { ...DEFAULT_OPTIONS.plan }, trackingOptions: { ...DEFAULT_OPTIONS.trackingOptions }, }; - this.cookieStorage = new cookieStorage().getStorage(); this._q = []; // queue for proxied functions before script load this._sending = false; this._updateScheduled = false; @@ -127,6 +126,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o this._cookieName = Constants.COOKIE_PREFIX + '_' + this._storageSuffixV5; + this.cookieStorage = new cookieStorage().getStorage(this.options.disableCookies); this.cookieStorage.options({ expirationDays: this.options.cookieExpiration, domain: this.options.domain, diff --git a/src/cookiestorage.js b/src/cookiestorage.js index 367c4fea..5a9e9140 100644 --- a/src/cookiestorage.js +++ b/src/cookiestorage.js @@ -12,12 +12,12 @@ var cookieStorage = function () { this.storage = null; }; -cookieStorage.prototype.getStorage = function () { +cookieStorage.prototype.getStorage = function (disableCookies) { if (this.storage !== null) { return this.storage; } - if (baseCookie.areCookiesEnabled()) { + if (!disableCookies && baseCookie.areCookiesEnabled()) { this.storage = Cookie; } else { // if cookies disabled, fallback to localstorage diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 0174b94f..e647e59c 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -13,6 +13,17 @@ import { mockCookie, restoreCookie, getCookie } from './mock-cookie'; import { AmplitudeServerZone } from '../src/server-zone.js'; import Request from '../src/xhr'; +const deleteAllCookies = () => + document.cookie.split(';').forEach(function (c) { + document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); + }); + +const getAllCookies = () => + document.cookie + .split(';') + .map((c) => c.trimStart()) + .filter((c) => !utils.isEmptyString(c)); + // maintain for testing backwards compatability describe('AmplitudeClient', function () { var apiKey = '000000'; @@ -888,11 +899,11 @@ describe('AmplitudeClient', function () { var onErrorSpy = sinon.spy(); var amplitude = new AmplitudeClient(); - sinon.stub(amplitude.cookieStorage, 'options').throws(); + sinon.stub(amplitude, '_refreshDynamicConfig').throws(); amplitude.init(apiKey, null, { onError: onErrorSpy }); assert.isTrue(onErrorSpy.calledOnce); - amplitude.cookieStorage.options.restore(); + amplitude['_refreshDynamicConfig'].restore(); }); it('should set observer plan options', function () { @@ -2728,6 +2739,44 @@ describe('AmplitudeClient', function () { }); }); + it('should not create any cookies if disabledCookies = true', function () { + deleteAllCookies(); + clock.tick(20); + + var cookieArray = getAllCookies(); + assert.equal(cookieArray.length, 0); + + var deviceId = 'test_device_id'; + var amplitude2 = new AmplitudeClient(); + + amplitude2.init(apiKey, null, { + deviceId: deviceId, + disableCookies: true, + }); + + cookieArray = getAllCookies(); + assert.equal(cookieArray.length, 0); + }); + + it('should create cookies if disabledCookies = false', function () { + deleteAllCookies(); + clock.tick(20); + + var cookieArray = getAllCookies(); + assert.equal(cookieArray.length, 0); + + var deviceId = 'test_device_id'; + var amplitude2 = new AmplitudeClient(); + + amplitude2.init(apiKey, null, { + deviceId: deviceId, + disableCookies: false, + }); + + cookieArray = getAllCookies(); + assert.equal(cookieArray.length, 1); + }); + it('should validate event properties', function () { var e = new Error('oops'); clock.tick(1);