Skip to content

Commit

Permalink
Merge pull request #3 from SemanticSugar/cache-xid-js-null-response
Browse files Browse the repository at this point in the history
Cache XID.js null response using the Prebid.js cookie
  • Loading branch information
abijr authored Jun 11, 2020
2 parents ffe3cee + 187e09c commit b925854
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
100 changes: 81 additions & 19 deletions modules/nextrollIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
*/

import {submodule} from '../src/hook';
import * as utils from '../src/utils'
import * as utils from '../src/utils';
import { getStorageManager } from '../src/storageManager.js';
const storage = getStorageManager();

const NEXTROLL_ID_LS_KEY = "nextroll_id_ls_key";
const NEXTROLL_ID_TEST_KEY = NEXTROLL_ID_LS_KEY + "_test";
const NEXTROLL_ID_DATE_KEY = NEXTROLL_ID_LS_KEY + "_set_date";
const DEFAULT_MISSING_VALUE = 'undefined';
const DEFAULT_ATTEMPT_FREQUENCY_MS = 1000 * 60 * 60 * 2; // 2hrs

function loadRetrieval() {
// v3.6
Expand All @@ -32,37 +40,88 @@ function isValidUA() {
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1))
}

function removeAttemptTimestamp() {
if (storage.hasLocalStorage()) {
storage.removeDataFromLocalStorage(NEXTROLL_ID_DATE_KEY);
}
}

// Sets unsuccesful attempt timestamp
// Note: assumes window has localstorage enabled.
function setAttemptTimestamp() {
const date = (new Date()).getTime();
storage.setDataInLocalStorage(NEXTROLL_ID_DATE_KEY, date);
}

function getXIDCallback(cb) {
return (result) => {
if (isValidResult(result)) {
cb(result);
removeAttemptTimestamp()
return;
}
// No valid id, and no localstorage
if (!storage.hasLocalStorage()) {
cb(DEFAULT_MISSING_VALUE);
return;
}

// localstorage and no valid id
setAttemptTimestamp();
}
}

function shouldAttemptRetrieval(attemptFrequencyMS) {
if (attemptFrequencyMS === undefined ||
typeof attemptFrequencyMS !== "number") {
attemptFrequencyMS = DEFAULT_ATTEMPT_FREQUENCY_MS;
}

if (!storage.hasLocalStorage()) {
// It means we're using prebid cache for unsuccesful attempts.
return true;
}

const storedDate = storage.getDataFromLocalStorage(NEXTROLL_ID_DATE_KEY);
if (storedDate === null) {
return true;
}

const currentDate = (new Date).getTime();
const storedDateNum = parseInt(storedDate);

if (currentDate - storedDateNum > attemptFrequencyMS) {
return true
}

return false;
}

function getFetchIdCallback(metadata) {
return (cb) => {
loadRetrieval();
const xidCB = getXIDCallback(cb);
window.RetrieveAdID(xidCB, xidCB, metadata);
}
}

function fetchId(configParams, consentData, currentStoredId) {
if (!isValidConfig(configParams)) {
utils.logWarn("Partner ID - nextrollId submodule requires a partner ID");
return;
}

if(!isValidUA()) {
if (!isValidUA() ||
!shouldAttemptRetrieval(configParams.attempt_frequency_ms)) {
return;
}

loadRetrieval();

let metadata = {
'PID': configParams.partner_id,
'app': 'nextroll_id_module'
}

const callback = function (cb) {
window.RetrieveAdID(
function (result) {
if (isValidResult(result))
cb(result);
},
function (result) {
if (isValidResult(result))
cb(result);
},
metadata
);
};

const callback = getFetchIdCallback(metadata);
return { callback };
};

Expand All @@ -80,7 +139,10 @@ export const nextrollIdSubmodule = {
* @return {(Object|undefined}
*/
decode(value) {
return (value && typeof value === 'string') ? { 'nextroll': value } : undefined;
if (value && typeof value === 'string' && value !== DEFAULT_MISSING_VALUE) {
return { 'nextroll': value };
}
return undefined;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b925854

Please sign in to comment.