Skip to content

Commit

Permalink
Lotame panorama id submodule: handle consent (#7644)
Browse files Browse the repository at this point in the history
* feat: Add handling for a custom client id
feat: Send us privacy string

* test: Add test case to make sure the expiry is also not touched and update the mocked response

Co-authored-by: Mike Marcus <mike@lotame.com>
  • Loading branch information
markaconrad and Tonsil authored Nov 22, 2021
1 parent 329deb8 commit 2a60735
Show file tree
Hide file tree
Showing 2 changed files with 307 additions and 8 deletions.
88 changes: 80 additions & 8 deletions modules/lotamePanoramaIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
* @module modules/lotamePanoramaId
* @requires module:modules/userId
*/
import { timestamp, isStr, logError, isBoolean, buildUrl, isEmpty, isArray } from '../src/utils.js';
import {
timestamp,
isStr,
logError,
isBoolean,
buildUrl,
isEmpty,
isArray,
isEmptyStr
} from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import { getStorageManager } from '../src/storageManager.js';
import { uspDataHandler } from '../src/adapterManager.js';

const KEY_ID = 'panoramaId';
const KEY_EXPIRY = `${KEY_ID}_expiry`;
Expand Down Expand Up @@ -115,14 +125,23 @@ function saveLotameCache(

/**
* Retrieve all the cached values from cookies and/or local storage
* @param {Number} clientId
*/
function getLotameLocalCache() {
function getLotameLocalCache(clientId = undefined) {
let cache = {
data: getFromStorage(KEY_ID),
expiryTimestampMs: 0,
clientExpiryTimestampMs: 0,
};

try {
if (clientId) {
const rawClientExpiry = getFromStorage(`${KEY_EXPIRY}_${clientId}`);
if (isStr(rawClientExpiry)) {
cache.clientExpiryTimestampMs = parseInt(rawClientExpiry, 10);
}
}

const rawExpiry = getFromStorage(KEY_EXPIRY);
if (isStr(rawExpiry)) {
cache.expiryTimestampMs = parseInt(rawExpiry, 10);
Expand Down Expand Up @@ -191,18 +210,44 @@ export const lotamePanoramaIdSubmodule = {
*/
getId(config, consentData, cacheIdObj) {
cookieDomain = lotamePanoramaIdSubmodule.findRootDomain();
let localCache = getLotameLocalCache();
const configParams = (config && config.params) || {};
const clientId = configParams.clientId;
const hasCustomClientId = !isEmpty(clientId);
const localCache = getLotameLocalCache(clientId);

let refreshNeeded = Date.now() > localCache.expiryTimestampMs;
const hasExpiredPanoId = Date.now() > localCache.expiryTimestampMs;

if (hasCustomClientId) {
const hasFreshClientNoConsent = Date.now() < localCache.clientExpiryTimestampMs;
if (hasFreshClientNoConsent) {
// There is no consent
return {
id: undefined,
reason: 'NO_CLIENT_CONSENT',
};
}
}

if (!refreshNeeded) {
if (!hasExpiredPanoId) {
return {
id: localCache.data,
};
}

const storedUserId = getProfileId();

// Add CCPA Consent data handling
const usp = uspDataHandler.getConsentData();

let usPrivacy;
if (typeof usp !== 'undefined' && !isEmpty(usp) && !isEmptyStr(usp)) {
usPrivacy = usp;
}
if (!usPrivacy) {
// fallback to 1st party cookie
usPrivacy = getFromStorage('us_privacy');
}

const resolveIdFunction = function (callback) {
let queryParams = {};
if (storedUserId) {
Expand All @@ -226,6 +271,17 @@ export const lotamePanoramaIdSubmodule = {
if (consentString) {
queryParams.gdpr_consent = consentString;
}

// Add usPrivacy to the url
if (usPrivacy) {
queryParams.us_privacy = usPrivacy;
}

// Add clientId to the url
if (hasCustomClientId) {
queryParams.c = clientId;
}

const url = buildUrl({
protocol: 'https',
host: `id.crwdcntrl.net`,
Expand All @@ -239,15 +295,31 @@ export const lotamePanoramaIdSubmodule = {
if (response) {
try {
let responseObj = JSON.parse(response);
const shouldUpdateProfileId = !(
const hasNoConsentErrors = !(
isArray(responseObj.errors) &&
responseObj.errors.indexOf(MISSING_CORE_CONSENT) !== -1
);

if (hasCustomClientId) {
if (hasNoConsentErrors) {
clearLotameCache(`${KEY_EXPIRY}_${clientId}`);
} else if (isStr(responseObj.no_consent) && responseObj.no_consent === 'CLIENT') {
saveLotameCache(
`${KEY_EXPIRY}_${clientId}`,
responseObj.expiry_ts,
responseObj.expiry_ts
);

// End Processing
callback();
return;
}
}

saveLotameCache(KEY_EXPIRY, responseObj.expiry_ts, responseObj.expiry_ts);

if (isStr(responseObj.profile_id)) {
if (shouldUpdateProfileId) {
if (hasNoConsentErrors) {
setProfileId(responseObj.profile_id);
}

Expand All @@ -262,7 +334,7 @@ export const lotamePanoramaIdSubmodule = {
clearLotameCache(KEY_ID);
}
} else {
if (shouldUpdateProfileId) {
if (hasNoConsentErrors) {
clearLotameCache(KEY_PROFILE);
}
clearLotameCache(KEY_ID);
Expand Down
Loading

0 comments on commit 2a60735

Please sign in to comment.