Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add handling for a custom client id #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
markaconrad marked this conversation as resolved.
Show resolved Hide resolved
}

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) {
jialong marked this conversation as resolved.
Show resolved Hide resolved
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