Skip to content

Commit

Permalink
[CriteoId] Add local storage check and migrate cookie check to use St…
Browse files Browse the repository at this point in the history
…orageManager API (#5953)

* [CriteoId] Check if local storage is writable.

* [CriteoId] Check cookies are writable using StorageManager

Co-authored-by: Jesus Alberto Polo Garcia <ja.pologarcia@criteo.com>
  • Loading branch information
jesusalber1 and Jesus Alberto Polo Garcia authored Nov 16, 2020
1 parent 92a9a09 commit 840c04d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
17 changes: 6 additions & 11 deletions modules/criteoIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ export const storage = getStorageManager(gvlid, bidderCode);

const bididStorageKey = 'cto_bidid';
const bundleStorageKey = 'cto_bundle';
const cookieWriteableKey = 'cto_test_cookie';
const cookiesMaxAge = 13 * 30 * 24 * 60 * 60 * 1000;

const pastDateString = new Date(0).toString();
const expirationString = new Date(utils.timestamp() + cookiesMaxAge).toString();

function areCookiesWriteable() {
storage.setCookie(cookieWriteableKey, '1');
const canWrite = storage.getCookie(cookieWriteableKey) === '1';
storage.setCookie(cookieWriteableKey, '', pastDateString);
return canWrite;
}

function extractProtocolHost (url, returnOnlyHost = false) {
const parsedUrl = utils.parseUrl(url, {noDecodeWholeURL: true})
return returnOnlyHost
Expand Down Expand Up @@ -60,20 +52,22 @@ function getCriteoDataFromAllStorages() {
}
}

function buildCriteoUsersyncUrl(topUrl, domain, bundle, areCookiesWriteable, isPublishertagPresent, gdprString) {
function buildCriteoUsersyncUrl(topUrl, domain, bundle, areCookiesWriteable, isLocalStorageWritable, isPublishertagPresent, gdprString) {
const url = 'https://gum.criteo.com/sid/json?origin=prebid' +
`${topUrl ? '&topUrl=' + encodeURIComponent(topUrl) : ''}` +
`${domain ? '&domain=' + encodeURIComponent(domain) : ''}` +
`${bundle ? '&bundle=' + encodeURIComponent(bundle) : ''}` +
`${gdprString ? '&gdprString=' + encodeURIComponent(gdprString) : ''}` +
`${areCookiesWriteable ? '&cw=1' : ''}` +
`${isPublishertagPresent ? '&pbt=1' : ''}`
`${isPublishertagPresent ? '&pbt=1' : ''}` +
`${isLocalStorageWritable ? '&lsw=1' : ''}`;

return url;
}

function callCriteoUserSync(parsedCriteoData, gdprString) {
const cw = areCookiesWriteable();
const cw = storage.cookiesAreEnabled();
const lsw = storage.localStorageIsEnabled();
const topUrl = extractProtocolHost(getRefererInfo().referer);
const domain = extractProtocolHost(document.location.href, true);
const isPublishertagPresent = typeof criteo_pubtag !== 'undefined'; // eslint-disable-line camelcase
Expand All @@ -83,6 +77,7 @@ function callCriteoUserSync(parsedCriteoData, gdprString) {
domain,
parsedCriteoData.bundle,
cw,
lsw,
isPublishertagPresent,
gdprString
);
Expand Down
3 changes: 1 addition & 2 deletions test/spec/modules/criteoIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe('CriteoId module', function () {
});

it('should call user sync url with the right params', function () {
getCookieStub.withArgs('cto_test_cookie').returns('1');
getCookieStub.withArgs('cto_bundle').returns('bundle');
window.criteo_pubtag = {}

Expand All @@ -80,7 +79,7 @@ describe('CriteoId module', function () {
ajaxBuilderStub.callsFake(mockResponse(undefined, ajaxStub))

criteoIdSubmodule.getId();
const expectedUrl = `https://gum.criteo.com/sid/json?origin=prebid&topUrl=https%3A%2F%2Ftestdev.com%2F&domain=testdev.com&bundle=bundle&cw=1&pbt=1`;
const expectedUrl = `https://gum.criteo.com/sid/json?origin=prebid&topUrl=https%3A%2F%2Ftestdev.com%2F&domain=testdev.com&bundle=bundle&cw=1&pbt=1&lsw=1`;

expect(ajaxStub.calledWith(expectedUrl)).to.be.true;

Expand Down

0 comments on commit 840c04d

Please sign in to comment.