Skip to content

Commit

Permalink
code sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Komorski authored and Marcin Komorski committed Jul 10, 2024
1 parent 23af08e commit 42aefd7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
24 changes: 23 additions & 1 deletion libraries/gptUtils/gptUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CLIENT_SECTIONS } from '../../src/fpd/oneClient.js';
import {find} from '../../src/polyfill.js';
import {compareCodeAndSlot, isGptPubadsDefined} from '../../src/utils.js';
import {compareCodeAndSlot, deepAccess, isGptPubadsDefined, uniques} from '../../src/utils.js';

/**
* Returns filter function to match adUnitCode in slot
Expand Down Expand Up @@ -35,3 +36,24 @@ export function getGptSlotInfoForAdUnitCode(adUnitCode) {
}
return {};
}

export const taxonomies = ['IAB_AUDIENCE_1_1', 'IAB_CONTENT_2_2'];

export function getSignals(fpd) {
const signals = Object.entries({
[taxonomies[0]]: getSegments(fpd, ['user.data'], 4),
[taxonomies[1]]: getSegments(fpd, CLIENT_SECTIONS.map(section => `${section}.content.data`), 6)
}).map(([taxonomy, values]) => values.length ? {taxonomy, values} : null)
.filter(ob => ob);

return signals;
}

export function getSegments(fpd, sections, segtax) {
return sections
.flatMap(section => deepAccess(fpd, section) || [])
.filter(datum => datum.ext?.segtax === segtax)
.flatMap(datum => datum.segment?.map(seg => seg.id))
.filter(ob => ob)
.filter(uniques)
}
2 changes: 1 addition & 1 deletion modules/dfpAdServerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { DEFAULT_DFP_PARAMS, DFP_ENDPOINT } from '../libraries/dfpUtils/dfpUtils.js';
import { getSignals } from '../libraries/gptUtils/gptUtils.js';
import { registerVideoSupport } from '../src/adServerManager.js';
import { gdprDataHandler } from '../src/adapterManager.js';
import { getPPID } from '../src/adserver.js';
Expand All @@ -11,7 +12,6 @@ import { config } from '../src/config.js';
import { EVENTS } from '../src/constants.js';
import * as events from '../src/events.js';
import { getHook } from '../src/hook.js';
import { getSignals } from '../src/pps.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { targeting } from '../src/targeting.js';
import {
Expand Down
39 changes: 14 additions & 25 deletions modules/gptPreAuction.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
import { getSignals as getSignalsFn, getSegments as getSegmentsFn, taxonomies } from '../libraries/gptUtils/gptUtils.js';
import { auctionManager } from '../src/auctionManager.js';
import { config } from '../src/config.js';
import { TARGETING_KEYS } from '../src/constants.js';
import { getHook } from '../src/hook.js';
import { find } from '../src/polyfill.js';
import {
deepAccess,
deepSetValue,
isAdUnitCodeMatchingSlot,
isGptPubadsDefined,
logInfo,
logWarn,
pick,
deepSetValue, logWarn, uniques
uniques
} from '../src/utils.js';
import {config} from '../src/config.js';
import {getHook} from '../src/hook.js';
import {find} from '../src/polyfill.js';
import { auctionManager } from '../src/auctionManager.js';
import { CLIENT_SECTIONS } from '../src/fpd/oneClient.js';
import { TARGETING_KEYS } from '../src/constants.js';

const MODULE_NAME = 'GPT Pre-Auction';
export let _currentConfig = {};
let hooksAdded = false;

export const taxonomies = ['IAB_AUDIENCE_1_1', 'IAB_CONTENT_2_2'];

export function getSegments(fpd, sections, segtax) {
return sections
.flatMap(section => deepAccess(fpd, section) || [])
.filter(datum => datum.ext?.segtax === segtax)
.flatMap(datum => datum.segment?.map(seg => seg.id))
.filter(ob => ob)
.filter(uniques)
return getSegmentsFn(fpd, sections, segtax);
}

export function getSignals(fpd) {
const signals = Object.entries({
[taxonomies[0]]: getSegments(fpd, ['user.data'], 4),
[taxonomies[1]]: getSegments(fpd, CLIENT_SECTIONS.map(section => `${section}.content.data`), 6)
}).map(([taxonomy, values]) => values.length ? {taxonomy, values} : null)
.filter(ob => ob);

return signals;
return getSignalsFn(fpd);
}

export function getSignalsArrayByAuctionsIds(auctionIds, index = auctionManager.index) {
Expand Down Expand Up @@ -212,7 +201,7 @@ export const makeBidRequestsHook = (fn, adUnits, ...args) => {
return fn.call(this, adUnits, ...args);
};

const setPPSConfigForGPT = (next, targetingSet) => {
const getPpsConfigFromTargetingSet = (next, targetingSet) => {
// set gpt config
const auctionsIds = getAuctionsIdsFromTargeting(targetingSet);
const signals = getSignalsIntersection(getSignalsArrayByAuctionsIds(auctionsIds));
Expand All @@ -233,14 +222,14 @@ const handleSetGptConfig = moduleConfig => {
if (_currentConfig.enabled) {
if (!hooksAdded) {
getHook('makeBidRequests').before(makeBidRequestsHook);
getHook('getReadyTargetingSetForGPT').after(setPPSConfigForGPT)
getHook('targetingDone').after(getPpsConfigFromTargetingSet)
hooksAdded = true;
}
} else {
logInfo(`${MODULE_NAME}: Turning off module`);
_currentConfig = {};
getHook('makeBidRequests').getHooks({hook: makeBidRequestsHook}).remove();
getHook('getReadyTargetingSetForGPT').getHooks({hook: setPPSConfigForGPT}).remove();
getHook('targetingDone').getHooks({hook: getPpsConfigFromTargetingSet}).remove();
hooksAdded = false;
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ export function newTargeting(auctionManager) {
});
});

targeting.getReadyTargetingSetForGPT(targetingSet);
targeting.targetingDone(targetingSet);

// emit event
events.emit(EVENTS.SET_TARGETING, targetingSet);
}, 'setTargetingForGPT');

targeting.getReadyTargetingSetForGPT = hook('sync', function (targetingSet) {
targeting.targetingDone = hook('sync', function (targetingSet) {
return targetingSet;
}, 'getReadyTargetingSetForGPT');
}, 'targetingDone');

/**
* normlizes input to a `adUnit.code` array
Expand Down
2 changes: 1 addition & 1 deletion test/spec/modules/gptPreAuction_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'modules/gptPreAuction.js';
import { config } from 'src/config.js';
import { makeSlot } from '../integration/faker/googletag.js';
import { taxonomies } from '../../../modules/gptPreAuction.js';
import { taxonomies } from '../../../libraries/gptUtils/gptUtils.js';

describe('GPT pre-auction module', () => {
let sandbox;
Expand Down

0 comments on commit 42aefd7

Please sign in to comment.