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

gptPreAuction: pass publisher provided signals to GPT #11946

Merged
merged 10 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 14 additions & 28 deletions modules/dfpAdServerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
* This module adds [DFP support]{@link https://www.doubleclickbygoogle.com/} for Video to Prebid.
*/

import {registerVideoSupport} from '../src/adServerManager.js';
import {targeting} from '../src/targeting.js';
import { DEFAULT_DFP_PARAMS, DFP_ENDPOINT } from '../libraries/dfpUtils/dfpUtils.js';
import { registerVideoSupport } from '../src/adServerManager.js';
import { gdprDataHandler } from '../src/adapterManager.js';
import { getPPID } from '../src/adserver.js';
import { auctionManager } from '../src/auctionManager.js';
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 {
buildUrl,
deepAccess,
Expand All @@ -12,19 +22,8 @@ import {
isNumber,
logError,
parseSizesInput,
parseUrl,
uniques
parseUrl
} from '../src/utils.js';
import {config} from '../src/config.js';
import {getHook} from '../src/hook.js';
import {auctionManager} from '../src/auctionManager.js';
import {gdprDataHandler} from '../src/adapterManager.js';
import * as events from '../src/events.js';
import {EVENTS} from '../src/constants.js';
import {getPPID} from '../src/adserver.js';
import {getRefererInfo} from '../src/refererDetection.js';
import {CLIENT_SECTIONS} from '../src/fpd/oneClient.js';
import {DEFAULT_DFP_PARAMS, DFP_ENDPOINT} from '../libraries/dfpUtils/dfpUtils.js';
/**
* @typedef {Object} DfpVideoParams
*
Expand Down Expand Up @@ -171,20 +170,7 @@ export function buildDfpVideoUrl(options) {
const fpd = auctionManager.index.getBidRequest(options.bid || {})?.ortb2 ??
auctionManager.index.getAuction(options.bid || {})?.getFPD()?.global;

function getSegments(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)
}

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

if (signals.length) {
queryParams.ppsj = btoa(JSON.stringify({
Expand Down
58 changes: 58 additions & 0 deletions src/pps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { auctionManager } from './auctionManager.js';
patmmccann marked this conversation as resolved.
Show resolved Hide resolved
import { TARGETING_KEYS } from './constants.js';
import { CLIENT_SECTIONS } from './fpd/oneClient.js';
import { deepAccess, uniques } from './utils.js';

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)
}

export function getSignals(fpd) {
const signals = Object.entries({
IAB_AUDIENCE_1_1: getSegments(fpd, ['user.data'], 4),
IAB_CONTENT_2_2: 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 getSignalsArrayByAuctionsIds(auctionIds) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(regarding the test failure) my preferred method of stubbing is dependency injection. E.g. if this did something like getSignalsArrayByAuctionIds(auctionIds, index = auctionManager.index), tests can swap in a mock version of index, and I find it a bit easier to reason about compared to sinon's stubs. To do the same with sinon I think you need an extra layer of indirection, basically instead of making index a parameter make it a property of some global object pointing to auctionManager.index, which you can then stub.

const signals = auctionIds
.map(auctionId => auctionManager.index.getAuction({ auctionId })?.getFPD()?.global)
.map(getSignals)
.filter(fpd => fpd);

return signals;
}

export function getSignalsIntersection(signals) {
const taxonomies = ['IAB_AUDIENCE_1_1', 'IAB_CONTENT_2_2'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these can be extracted to be reused with getSignals above

const result = {};
taxonomies.forEach((taxonomy) => {
const allValues = signals
.flatMap(x => x)
.filter(x => x.taxonomy === taxonomy)
.map(x => x.values);
result[taxonomy] = allValues.length ? (
allValues.reduce((commonElements, subArray) => {
return commonElements.filter(element => subArray.includes(element));
})
) : []
result[taxonomy] = { values: result[taxonomy] };
})
return result;
}

export function getAuctionsIdsFromTargeting(targeting) {
return Object.values(targeting)
.flatMap(x => Object.entries(x))
.filter((entry) => (entry[0] || '').includes(TARGETING_KEYS.AD_ID))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be entry[0] === TARGETING_KEYS.AD_ID || entry[0].startsWith(TARGETING_KEYS.AD_ID + '_')

That's because

  1. I don't think object keys can be falsy (afaik the language requires them to be either strings or symbols), so you don't need the fallback to ''
  2. You are interested in either hb_adid or hb_adid_${bidder} (reference). It's unlikely, but possible, to define custom targeting keys like custom_hb_adid that would be a false positive for this.

.flatMap(entry => entry[1])
.filter(uniques);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that these are ad ids, not auction IDs. they should be passed through findBidByAdId.

}
44 changes: 25 additions & 19 deletions src/targeting.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import { auctionManager } from './auctionManager.js';
import { getTTL } from './bidTTL.js';
import { bidderSettings } from './bidderSettings.js';
import { config } from './config.js';
import {
BID_STATUS,
DEFAULT_TARGETING_KEYS,
EVENTS,
JSON_MAPPING,
NATIVE_KEYS,
STATUS,
TARGETING_KEYS
} from './constants.js';
import * as events from './events.js';
import { hook } from './hook.js';
import { ADPOD } from './mediaTypes.js';
import { NATIVE_TARGETING_KEYS } from './native.js';
import { find, includes } from './polyfill.js';
import { getAuctionsIdsFromTargeting, getSignalsArrayByAuctionsIds, getSignalsIntersection } from './pps.js';
import {
deepAccess,
deepClone,
Expand All @@ -14,25 +33,7 @@ import {
timestamp,
uniques,
} from './utils.js';
import {config} from './config.js';
import {NATIVE_TARGETING_KEYS} from './native.js';
import {auctionManager} from './auctionManager.js';
import {ADPOD} from './mediaTypes.js';
import {hook} from './hook.js';
import {bidderSettings} from './bidderSettings.js';
import {find, includes} from './polyfill.js';
import {
BID_STATUS,
DEFAULT_TARGETING_KEYS,
EVENTS,
JSON_MAPPING,
NATIVE_KEYS,
STATUS,
TARGETING_KEYS
} from './constants.js';
import {getHighestCpm, getOldestHighestCpmBid} from './utils/reducers.js';
import {getTTL} from './bidTTL.js';
import * as events from './events.js';
import { getHighestCpm, getOldestHighestCpmBid } from './utils/reducers.js';

var pbTargetingKeys = [];

Expand Down Expand Up @@ -461,6 +462,11 @@ export function newTargeting(auctionManager) {
});
});

// set gpt config
const auctionsIds = getAuctionsIdsFromTargeting(targetingSet);
const signals = getSignalsIntersection(getSignalsArrayByAuctionsIds(auctionsIds));
window.googletag.setConfig && window.googletag.setConfig({pps: { taxonomies: signals }});

// emit event
events.emit(EVENTS.SET_TARGETING, targetingSet);
}, 'setTargetingForGPT');
Expand Down
116 changes: 116 additions & 0 deletions test/spec/unit/core/pps_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { auctionManager } from '../../../../src/auctionManager';
import { getAuctionsIdsFromTargeting, getSegments, getSignals, getSignalsArrayByAuctionsIds, getSignalsIntersection } from '../../../../src/pps';

describe('pps', () => {
let getAuctionStub;
const mockTargeting = {'/123456/header-bid-tag-0': {'hb_deal_rubicon': '1234', 'hb_deal': '1234', 'hb_pb': '0.53', 'hb_adid': '148018fe5e', 'hb_bidder': 'rubicon', 'foobar': '300x250', 'hb_pb_rubicon': '0.53', 'hb_adid_rubicon': '148018fe5e', 'hb_bidder_rubicon': 'rubicon', 'hb_deal_appnexus': '4321', 'hb_pb_appnexus': '0.1', 'hb_adid_appnexus': '567891011', 'hb_bidder_appnexus': 'appnexus'}}

const mocksAuctions = [
{
auctionId: '1111',
getFPD: () => ({
global: {
user: {
data: [{
name: 'dataprovider.com',
ext: {
segtax: 4
},
segment: [{
id: '1'
}, {
id: '2'
}]
}],
}
}
})
},
{
auctionId: '234234',
getFPD: () => ({
global: {
user: {
data: [{
name: 'dataprovider.com',
ext: {
segtax: 4
},
segment: [{
id: '2'
}]
}]
}
}
}),
}, {
auctionId: '234324234',
getFPD: () => ({
global: {
user: {
data: [{
name: 'dataprovider.com',
ext: {
segtax: 4
},
segment: [{
id: '2'
}, {
id: '3'
}]
}]
}
}
})
},
]

it('should parse segments from fpd', () => {
const twoSegments = getSegments(mocksAuctions[0].getFPD().global, ['user.data'], 4);
expect(JSON.stringify(twoSegments)).to.equal(JSON.stringify(['1', '2']))
const zeroSegments = getSegments(mocksAuctions[0].getFPD().global, ['user.data'], 6);
expect(zeroSegments).to.length(0);
})

it('should return signals from fpd', () => {
const signals = getSignals(mocksAuctions[0].getFPD().global);
const expectedSignals = [{ taxonomy: 'IAB_AUDIENCE_1_1', values: ['1', '2'] }];
expect(JSON.stringify(signals)).to.equal(JSON.stringify(expectedSignals))
})

it('should properly get auctions ids from targeting', () => {
const auctionsIds = getAuctionsIdsFromTargeting(mockTargeting);
expect(JSON.stringify(auctionsIds)).to.equal(JSON.stringify(['148018fe5e', '567891011']))
})

it('should properly return empty array of auction ids for invalid targeting', () => {
let auctionsIds = getAuctionsIdsFromTargeting({});
expect(Array.isArray(auctionsIds)).to.equal(true);
expect(auctionsIds).to.length(0);
auctionsIds = getAuctionsIdsFromTargeting({'/123456/header-bid-tag-0/bg': {'invalidContent': '123'}});
expect(Array.isArray(auctionsIds)).to.equal(true);
expect(auctionsIds).to.length(0);
})

it('should properly get signals from auctions', () => {
getAuctionStub = sinon.stub(auctionManager.index, 'getAuction').callsFake(({ auctionId }) => {
return mocksAuctions.find(auction => auction.auctionId === auctionId)
});
const signals = getSignalsArrayByAuctionsIds(['1111', '234234', '234324234']);
const intersection = getSignalsIntersection(signals)
const expectedResult = { IAB_AUDIENCE_1_1: { values: ['2'] }, IAB_CONTENT_2_2: { values: [] } };
expect(JSON.stringify(intersection)).to.be.equal(JSON.stringify(expectedResult));
getAuctionStub.restore();
})

it('should return empty signals array for empty auctions ids array', () => {
const signals = getSignalsArrayByAuctionsIds([]);
expect(Array.isArray(signals)).to.equal(true);
expect(signals).to.length(0);
})

it('should return properly formatted object for getSignalsIntersection invoked with empty array', () => {
const signals = getSignalsIntersection([]);
expect(Object.keys(signals)).to.contain.members(['IAB_AUDIENCE_1_1', 'IAB_CONTENT_2_2']);
})
})
Loading