Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arielmtk committed Dec 18, 2024
1 parent 5bdf5d4 commit 4eee43b
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions modules/mobianRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function makeMemoizedFetch() {

export const getContextData = makeMemoizedFetch();

const entriesToObjectReducer = (acc, [key, value]) => ({ ...acc, [key]: value });

/**
* @param {MobianConfig} config
* @param {MobianContextData} contextData
Expand All @@ -107,6 +109,22 @@ export function contextDataToKeyValues(config, contextData) {
return keyValues;
}

function makeContextDataToKeyValuesReducer(config) {
const { prefix } = config;
return function contextDataToKeyValuesReducer(keyValues, [key, value]) {
if (key === AP_VALUES) {
AP_KEYS.forEach((apKey) => {
if (!value?.[apKey]?.length) return;
keyValues.push([`${prefix}_ap_${apKey}`, value[apKey]]);
});
}
if (value?.length) {
keyValues.push([`${prefix}_${key}`, value]);
}
return keyValues;
}
}

export async function fetchContextData() {
const pageUrl = encodeURIComponent(window.location.href);
const requestUrl = `${MOBIAN_URL}?url=${pageUrl}`;
Expand Down Expand Up @@ -140,10 +158,11 @@ export function getConfig(config) {
*/
export function setTargeting(config, contextData) {
logMessage('context', contextData);
const filteredContextData = Object.entries(contextData).filter(([key]) => config.publisherTargeting.includes(key));
const data = Object.fromEntries(filteredContextData);
const keyValues = contextDataToKeyValues(config, data);
Object.entries(keyValues).forEach(([key, value]) => setKeyValue(key, value));
const keyValues = Object.entries(contextData)
.filter(([key]) => config.publisherTargeting.includes(key))
.reduce(makeContextDataToKeyValuesReducer(config), [])

keyValues.forEach(([key, value]) => setKeyValue(key, value));
}

/**
Expand Down Expand Up @@ -176,9 +195,10 @@ export function makeDataFromResponse(contextData) {
export function extendBidRequestConfig(bidReqConfig, contextData, config) {
logMessage('extendBidRequestConfig', bidReqConfig, contextData);
const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global;

const filteredContextData = Object.entries(contextData).filter(([key]) => config.advertiserTargeting.includes(key));
const keyValues = contextDataToKeyValues(config, filteredContextData);
const keyValues = Object.entries(contextData)
.filter(([key]) => config.advertiserTargeting.includes(key))
.reduce(makeContextDataToKeyValuesReducer(config), [])
.reduce(entriesToObjectReducer, {});

ortb2Site.ext = ortb2Site.ext || {};
ortb2Site.ext.data = {
Expand All @@ -195,13 +215,10 @@ export function extendBidRequestConfig(bidReqConfig, contextData, config) {
*/
function init(rawConfig) {
logMessage('init', rawConfig);

const config = getConfig(rawConfig);

if (config.publisherTargeting.length) {
getContextData().then((contextData) => setTargeting(config, contextData));
}

return true;
}

Expand Down

0 comments on commit 4eee43b

Please sign in to comment.