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

Hadron RTD : cleaning things up #12480

Merged
merged 2 commits into from
Nov 20, 2024
Merged
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
113 changes: 16 additions & 97 deletions modules/hadronRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
* @module modules/hadronRtdProvider
* @requires module:modules/realTimeData
*/
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {getStorageManager} from '../src/storageManager.js';
import {submodule} from '../src/hook.js';
import {isFn, isStr, isArray, isEmpty, deepEqual, isPlainObject, logError, logInfo} from '../src/utils.js';
import {isFn, isStr, isArray, deepEqual, isPlainObject, logInfo} from '../src/utils.js';
import {loadExternalScript} from '../src/adloader.js';
import {MODULE_TYPE_RTD} from '../src/activities/modules.js';

Expand All @@ -22,8 +21,7 @@ const LOG_PREFIX = '[HadronRtdProvider] ';
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'hadron';
const AU_GVLID = 561;
const HADRON_ID_DEFAULT_URL = 'https://id.hadron.ad.gt/api/v1/hadronid?_it=prebid';
const HADRON_SEGMENT_URL = 'https://prebid-rtd.audigent.workers.dev'; // https://id.hadron.ad.gt/api/v1/rtd';
const HADRON_JS_URL = 'https://cdn.hadronid.net/hadron.js';
const LS_TAM_KEY = 'auHadronId';
const RTD_LOCAL_NAME = 'auHadronRtd';
export const storage = getStorageManager({moduleType: MODULE_TYPE_RTD, moduleName: SUBMODULE_NAME});
Expand All @@ -37,19 +35,6 @@ const urlAddParams = (url, params) => {
return url + (url.indexOf('?') > -1 ? '&' : '?') + params
};

/**
* Deep set an object unless value present.
* @param {Object} obj
* @param {String} path
* @param {Object} val
*/
function set(obj, path, val) {
const keys = path.split('.');
const lastKey = keys.pop();
const lastObj = keys.reduce((obj, key) => obj[key] = obj[key] || {}, obj);
lastObj[lastKey] = lastObj[lastKey] || val;
}

/**
* Deep object merging with array deduplication.
* @param {Object} target
Expand All @@ -62,11 +47,11 @@ function mergeDeep(target, ...sources) {
if (isPlainObject(target) && isPlainObject(source)) {
for (const key in source) {
if (isPlainObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
if (!target[key]) Object.assign(target, {[key]: {}});
mergeDeep(target[key], source[key]);
} else if (isArray(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: source[key] });
Object.assign(target, {[key]: source[key]});
} else if (isArray(target[key])) {
source[key].forEach(obj => {
let e = 1;
Expand All @@ -82,7 +67,7 @@ function mergeDeep(target, ...sources) {
});
}
} else {
Object.assign(target, { [key]: source[key] });
Object.assign(target, {[key]: source[key]});
}
}
}
Expand Down Expand Up @@ -150,6 +135,17 @@ export function addRealTimeData(bidConfig, rtd, rtdConfig) {
* @param {Object} userConsent
*/
export function getRealTimeData(bidConfig, onDone, rtdConfig, userConsent) {
if (!storage.getDataFromLocalStorage(LS_TAM_KEY)) {
const partnerId = rtdConfig.params.partnerId | 0;
const hadronIdUrl = rtdConfig.params && rtdConfig.params.hadronIdUrl;
const scriptUrl = urlAddParams(
paramOrDefault(hadronIdUrl, HADRON_JS_URL, {}),
`partner_id=${partnerId}&_it=prebid`
);
loadExternalScript(scriptUrl, SUBMODULE_NAME, () => {
logInfo(LOG_PREFIX, 'hadronId JS snippet loaded', scriptUrl);
})
}
if (rtdConfig && isPlainObject(rtdConfig.params) && rtdConfig.params.segmentCache) {
let jsonData = storage.getDataFromLocalStorage(RTD_LOCAL_NAME);

Expand Down Expand Up @@ -177,83 +173,6 @@ export function getRealTimeData(bidConfig, onDone, rtdConfig, userConsent) {
logInfo(LOG_PREFIX, 'hadronId TAM found', hadronId);
}
}
if (!isEmpty(userIds)) {
// if (typeof getGlobal().refreshUserIds === 'function') {
// (getGlobal()).refreshUserIds({submoduleNames: 'hadronId'});
// }
// userIds.hadronId = hadronId;
getRealTimeDataAsync(bidConfig, onDone, rtdConfig, userConsent, userIds);
} else {
// the hadronId was not found, reasons can be:
// 1) prebid wasn't compiled with hadronIdSystem
// 2) prebid wasn't configured to use hadronId user module
// 3) all previous and no other hadronId snippet configured in the page
// then need to load hadron.js from the CDN
window.pubHadronCb = (hadronId) => {
userIds.hadronId = hadronId;
getRealTimeDataAsync(bidConfig, onDone, rtdConfig, userConsent, userIds);
}
const partnerId = rtdConfig.params.partnerId | 0;
const hadronIdUrl = rtdConfig.params && rtdConfig.params.hadronIdUrl;
const scriptUrl = urlAddParams(
paramOrDefault(hadronIdUrl, HADRON_ID_DEFAULT_URL, userIds),
`partner_id=${partnerId}&_it=prebid`
);
loadExternalScript(scriptUrl, SUBMODULE_NAME, () => {
logInfo(LOG_PREFIX, 'hadronId JS snippet loaded', scriptUrl);
})
}
}

/**
* Async rtd retrieval from Audigent
* @param {Object} bidConfig
* @param {function} onDone
* @param {Object} rtdConfig
* @param {Object} userConsent
* @param {Object} userIds
*/
function getRealTimeDataAsync(bidConfig, onDone, rtdConfig, userConsent, userIds) {
let reqParams = {};

if (isPlainObject(rtdConfig)) {
set(rtdConfig, 'params.requestParams.ortb2', bidConfig.ortb2Fragments.global);
reqParams = rtdConfig.params.requestParams;
}

if (isPlainObject(window.pubHadronPm)) {
reqParams.pubHadronPm = window.pubHadronPm;
}

ajax(HADRON_SEGMENT_URL, {
success: function (response, req) {
if (req.status === 200) {
try {
const data = JSON.parse(response);
if (data && data.rtd) {
addRealTimeData(bidConfig, data.rtd, rtdConfig);
onDone();
storage.setDataInLocalStorage(RTD_LOCAL_NAME, JSON.stringify(data));
} else {
onDone();
}
} catch (err) {
logError(LOG_PREFIX, 'unable to parse audigent segment data');
onDone();
}
} else if (req.status === 204) {
// unrecognized partner config
onDone();
}
},
error: function () {
onDone();
logError(LOG_PREFIX, 'unable to get audigent segment data');
}
},
JSON.stringify({'userIds': userIds, 'config': reqParams}),
{contentType: 'application/json'}
);
}

/**
Expand Down