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

initial update for bidder schain support - feedback wanted #4479

Closed
wants to merge 1 commit into from
Closed
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
169 changes: 169 additions & 0 deletions integrationExamples/gpt/hello_world_schain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<!--
This page calls a single bidder for a single ad slot. It can be considered a "hello world" example for using
Prebid with the Google Publisher Tag.
It also makes a good test page for new adapter PR submissions. Simply set your server's Bid Params object in the
bids array inside the adUnits, and it will use your adapter to load an ad.
NOTE that many ad servers won't send back an ad if the URL is localhost... so you might need to
set an alias in your /etc/hosts file so that you can load this page from a different domain.
-->

<html>

<head>
<script async src="../../build/dev/prebid.js"></script>
<script async src="https://www.googletagservices.com/tag/js/gpt.js"></script>
<script>
var FAILSAFE_TIMEOUT = 3300;
var PREBID_TIMEOUT = 1000;

var adUnits = [{
code: 'div-gpt-ad-1460505748561-0',
mediaTypes: {
banner: {
sizes: [[300, 250], [300, 600]],
}
},
// Replace this object to test a new Adapter!
bids: [{
bidder: 'appnexus',
params: {
placementId: 13144370
}
}, {
bidder: 'districtm',
params: {
placementId: 13144370
}
}, {
bidder: "rubicon",
params: {
accountId: 14062,
siteId: 70608,
zoneId: 498816
}
}]

}];

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];

</script>

<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function () {
googletag.pubads().disableInitialLoad();
});

pbjs.que.push(function () {
pbjs.addAdUnits(adUnits);
pbjs.setConfig({
"schain": {
"validation": "off",
"config": {
"ver": "1.0",
"complete": 1,
"nodes": [
{
"asi": "indirectseller.com",
"sid": "00001",
"hp": 1
},

{
"asi": "indirectseller-2.com",
"sid": "00002",
"hp": 1
}
]
}
}
});

pbjs.setBidderConfig({
bidders: ['appnexus'],
config: {
"schain": {
"validation": "strict",
"config": {
"ver": "1.0",
"complete": 1,
"nodes": [
{
"asi": "myoverride.com",
// "sid": "00001",
"sid": 1,
"hp": 1
}
]
}
}
}
});

pbjs.setBidderConfig({
bidders: ['districtm'],
config: {
"schain": {
"validation": "relaxed",
"config": {
"ver": "1.0",
"complete": 1,
"nodes": [
{
"asi": "myoverride.com",
// "sid": "00001",
"sid": 1,
"hp": 1
}
]
}
}
}
});
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest,
timeout: PREBID_TIMEOUT
});
});

function sendAdserverRequest() {
if (pbjs.adserverRequestSent) return;
pbjs.adserverRequestSent = true;
googletag.cmd.push(function () {
pbjs.que.push(function () {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh();
});
});
}

// setTimeout(function () {
// sendAdserverRequest();
// }, FAILSAFE_TIMEOUT);

</script>

<script>
googletag.cmd.push(function () {
googletag.defineSlot('/19968336/header-bid-tag-0', [[300, 250], [300, 600]], 'div-gpt-ad-1460505748561-0').addService(googletag.pubads());

googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>

<body>
<h2>Prebid.js Test</h2>
<h5>Div-1</h5>
<div id='div-gpt-ad-1460505748561-0'>
<script type='text/javascript'>
googletag.cmd.push(function () { googletag.display('div-gpt-ad-1460505748561-0'); });
</script>
</div>
</body>

</html>
111 changes: 68 additions & 43 deletions modules/schain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {config} from '../src/config';
import {getGlobal} from '../src/prebidGlobal';
import { isNumber, isStr, isArray, isPlainObject, hasOwn, logError, isInteger, _each } from '../src/utils';
import adapterManager from '../src/adapterManager';
import { isNumber, isStr, isArray, isPlainObject, hasOwn, logError, isInteger, _each, logWarn } from '../src/utils';

// https://github.com/InteractiveAdvertisingBureau/openrtb/blob/master/supplychainobject.md

Expand All @@ -19,85 +19,93 @@ _each(MODE, mode => MODES.push(mode));

// validate the supply chain object
export function isSchainObjectValid(schainObject, returnOnError) {
function warnOrError(msg) {
if (returnOnError === true) {
logError(msg);
} else {
logWarn(msg);
}
}

if (!isPlainObject(schainObject)) {
logError(schainErrorPrefix + `schain` + shouldBeAnObject);
warnOrError(schainErrorPrefix + `schain` + shouldBeAnObject);
if (returnOnError) return false;
}

// complete: Integer
if (!isNumber(schainObject.complete) || !isInteger(schainObject.complete)) {
logError(schainErrorPrefix + `schain.complete` + shouldBeAnInteger);
warnOrError(schainErrorPrefix + `schain.complete` + shouldBeAnInteger);
if (returnOnError) return false;
}

// ver: String
if (!isStr(schainObject.ver)) {
logError(schainErrorPrefix + `schain.ver` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.ver` + shouldBeAString);
if (returnOnError) return false;
}

// ext: Object [optional]
if (hasOwn(schainObject, 'ext')) {
if (!isPlainObject(schainObject.ext)) {
logError(schainErrorPrefix + `schain.ext` + shouldBeAnObject);
warnOrError(schainErrorPrefix + `schain.ext` + shouldBeAnObject);
if (returnOnError) return false;
}
}

// nodes: Array of objects
let isEachNodeIsValid = true;
if (!isArray(schainObject.nodes)) {
logError(schainErrorPrefix + `schain.nodes` + shouldBeAnArray);
warnOrError(schainErrorPrefix + `schain.nodes` + shouldBeAnArray);
if (returnOnError) return false;
} else {
schainObject.nodes.forEach(node => {
// asi: String
if (!isStr(node.asi)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].asi` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.nodes[].asi` + shouldBeAString);
}

// sid: String
if (!isStr(node.sid)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].sid` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.nodes[].sid` + shouldBeAString);
}

// hp: Integer
if (!isNumber(node.hp) || !isInteger(node.hp)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].hp` + shouldBeAnInteger);
warnOrError(schainErrorPrefix + `schain.nodes[].hp` + shouldBeAnInteger);
}

// rid: String [Optional]
if (hasOwn(node, 'rid')) {
if (!isStr(node.rid)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].rid` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.nodes[].rid` + shouldBeAString);
}
}

// name: String [Optional]
if (hasOwn(node, 'name')) {
if (!isStr(node.name)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].name` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.nodes[].name` + shouldBeAString);
}
}

// domain: String [Optional]
if (hasOwn(node, 'domain')) {
if (!isStr(node.domain)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].domain` + shouldBeAString);
warnOrError(schainErrorPrefix + `schain.nodes[].domain` + shouldBeAString);
}
}

// ext: Object [Optional]
if (hasOwn(node, 'ext')) {
if (!isPlainObject(node.ext)) {
isEachNodeIsValid = isEachNodeIsValid && false;
logError(schainErrorPrefix + `schain.nodes[].ext` + shouldBeAnObject);
warnOrError(schainErrorPrefix + `schain.nodes[].ext` + shouldBeAnObject);
}
}
});
Expand All @@ -110,48 +118,65 @@ export function isSchainObjectValid(schainObject, returnOnError) {
return true;
}

export function copySchainObjectInAdunits(adUnits, schainObject) {
// copy schain object in all adUnits as adUnits[].bid.schain
adUnits.forEach(adUnit => {
adUnit.bids.forEach(bid => {
bid.schain = schainObject;
});
});
}

export function isValidSchainConfig(schainObject) {
if (schainObject === undefined) {
return false;
}
if (!isPlainObject(schainObject)) {
logError(schainErrorPrefix + 'schain config will not be passed to bidders as schain is not an object.');
logError(schainErrorPrefix + 'the following schain config will not be used as schain is not an object.', schainObject);
return false;
}
return true;
}

export function init(config) {
function checkSchainConfig(schainObject, bidder) {
let mode = MODE.STRICT;
getGlobal().requestBids.before(function(fn, reqBidsConfigObj) {
let schainObject = config.getConfig('schain');
if (isValidSchainConfig(schainObject)) {
if (isStr(schainObject.validation) && MODES.indexOf(schainObject.validation) != -1) {
mode = schainObject.validation;
}
if (mode === MODE.OFF) {
// no need to validate
copySchainObjectInAdunits(reqBidsConfigObj.adUnits || getGlobal().adUnits, schainObject.config);

if (isValidSchainConfig(schainObject)) {
if (isStr(schainObject.validation) && MODES.indexOf(schainObject.validation) != -1) {
mode = schainObject.validation;
}
if (mode === MODE.OFF) {
// no need to validate
return schainObject.config;
} else {
// if strict mode and config is invalid, reject config + throw error; otherwise allow config to go through
if (isSchainObjectValid(schainObject.config, !!(mode === MODE.STRICT))) {
return schainObject.config;
} else {
if (isSchainObjectValid(schainObject.config, mode === MODE.STRICT)) {
copySchainObjectInAdunits(reqBidsConfigObj.adUnits || getGlobal().adUnits, schainObject.config);
} else {
logError(schainErrorPrefix + 'schain config will not be passed to bidders as it is not valid.');
}
logError(schainErrorPrefix + `the following schain config will not be passed to bidder '${bidder}' as it is not valid.`, schainObject);
}
}
// calling fn allows prebid to continue processing
return fn.call(this, reqBidsConfigObj);
}, 40);
}
return null;
}

function makeBidRequestsHook(fn, bidderRequests) {
function getSchainForBidder(bidder) {
let bidderSchain = bidderConfigs[bidder] && bidderConfigs[bidder].schain;
return bidderSchain || globalSchainConfig;
}

const globalSchainConfig = config.getConfig('schain');
const bidderConfigs = config.getBidderConfig();

bidderRequests.forEach(bidderRequest => {
let bidder = bidderRequest.bidderCode;
let schainConfig = getSchainForBidder(bidder);

bidderRequest.bids.forEach(bid => {
let result = checkSchainConfig(schainConfig, bidder);
if (result) {
bid.schain = result;
}
});
});

fn(bidderRequests);
}

export function init() {
adapterManager.makeBidRequests.after(makeBidRequestsHook);
}

init(config)
init()
Loading