diff --git a/modules/lotamePanoramaIdSystem.js b/modules/lotamePanoramaIdSystem.js index 82502356ab4..5616c6b2573 100644 --- a/modules/lotamePanoramaIdSystem.js +++ b/modules/lotamePanoramaIdSystem.js @@ -16,39 +16,7 @@ const MODULE_NAME = 'lotamePanoramaId'; const NINE_MONTHS_MS = 23328000 * 1000; export const storage = getStorageManager(null, MODULE_NAME); -const cookieDomain = findCookieDomain(String(document.domain)); - -/** - * Find the root dommain - * @param {string} fullDomain - * @return {?string} - */ -function findCookieDomain(fullDomain) { - const domainParts = fullDomain.split('.'); - if (domainParts.length == 2) { - return fullDomain; - } - let rootDomain; - let continueSearching = true; - let startIndex = -2; - const TEST_COOKIE_NAME = 'lotame_domain_check'; - const TEST_COOKIE_VALUE = 'writeable'; - do { - rootDomain = domainParts.slice(startIndex).join('.'); - let expirationDate = new Date( - utils.timestamp() + 10 * 1000 - ).toUTCString(); - storage.setCookie(TEST_COOKIE_NAME, TEST_COOKIE_VALUE, expirationDate, 'Lax', `${rootDomain}`, undefined); - const value = storage.getCookie(TEST_COOKIE_NAME, undefined); - if (value === TEST_COOKIE_VALUE) { - continueSearching = false; - } else { - startIndex += -1; - continueSearching = Math.abs(startIndex) <= domainParts.length; - } - } while (continueSearching); - return rootDomain; -} +let cookieDomain; /** * Set the Lotame First Party Profile ID in the first party namespace @@ -57,7 +25,7 @@ function findCookieDomain(fullDomain) { function setProfileId(profileId) { if (storage.cookiesAreEnabled()) { let expirationDate = new Date(utils.timestamp() + NINE_MONTHS_MS).toUTCString(); - storage.setCookie(KEY_PROFILE, profileId, expirationDate, 'Lax', undefined, undefined); + storage.setCookie(KEY_PROFILE, profileId, expirationDate, 'Lax', cookieDomain, undefined); } if (storage.hasLocalStorage()) { storage.setDataInLocalStorage(KEY_PROFILE, profileId, undefined); @@ -179,6 +147,42 @@ export const lotamePanoramaIdSubmodule = { */ name: MODULE_NAME, + /** + * Find the root domain + * @param {string} fullDomain + * @return {?string} + */ + findCookieDomain: function (fullDomain) { + if (!storage.cookiesAreEnabled()) { + return fullDomain; + } + + const domainParts = fullDomain.split('.'); + if (domainParts.length == 2) { + return fullDomain; + } + let rootDomain; + let continueSearching = true; + let startIndex = -2; + const TEST_COOKIE_NAME = 'lotame_domain_check'; + const TEST_COOKIE_VALUE = 'writeable'; + do { + rootDomain = domainParts.slice(startIndex).join('.'); + let expirationDate = new Date( + utils.timestamp() + 10 * 1000 + ).toUTCString(); + storage.setCookie(TEST_COOKIE_NAME, TEST_COOKIE_VALUE, expirationDate, 'Lax', `${rootDomain}`, undefined); + const value = storage.getCookie(TEST_COOKIE_NAME, undefined); + if (value === TEST_COOKIE_VALUE) { + continueSearching = false; + } else { + startIndex += -1; + continueSearching = Math.abs(startIndex) <= domainParts.length; + } + } while (continueSearching); + return rootDomain; + }, + /** * Decode the stored id value for passing to bid requests * @function decode @@ -199,6 +203,7 @@ export const lotamePanoramaIdSubmodule = { * @returns {IdResponse|undefined} */ getId(config, consentData, cacheIdObj) { + cookieDomain = this.findCookieDomain(String(document.domain)); let localCache = getLotameLocalCache(); let refreshNeeded = Date.now() > localCache.expiryTimestampMs; diff --git a/test/spec/modules/lotamePanoramaIdSystem_spec.js b/test/spec/modules/lotamePanoramaIdSystem_spec.js index c6d3383374a..b9f76cc701a 100644 --- a/test/spec/modules/lotamePanoramaIdSystem_spec.js +++ b/test/spec/modules/lotamePanoramaIdSystem_spec.js @@ -446,4 +446,24 @@ describe('LotameId', function() { 'lotamePanoramaId': '1234' }); }); + + describe('findCookieDomain', function() { + beforeEach(function() { + getCookieStub + .onFirstCall() + .returns(null) // .co.uk + .onSecondCall() + .returns('writeable') // realdomain.co.uk; + }); + + it('should just find the root domain', function() { + var domain = lotamePanoramaIdSubmodule.findCookieDomain('sub.realdomain.co.uk') + expect(domain).to.be.eq('realdomain.co.uk') + }); + + it('should find the full domain when no subdomain is present', function() { + var domain = lotamePanoramaIdSubmodule.findCookieDomain('realdomain.co.uk') + expect(domain).to.be.eq('realdomain.co.uk') + }); + }); });