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

Fledge Module: Initial Commit #7189

Closed
wants to merge 3 commits 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
3 changes: 2 additions & 1 deletion allowedModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
...sharedWhiteList,
'criteo-direct-rsa-validate',
'crypto-js',
'live-connect' // Maintained by LiveIntent : https://github.com/liveintent-berlin/live-connect/
'live-connect', // Maintained by LiveIntent : https://github.com/liveintent-berlin/live-connect/
'@magnite/fledge.polyfill'
],
'src': [
...sharedWhiteList,
Expand Down
75 changes: 75 additions & 0 deletions modules/fledge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Fledge } from '@magnite/fledge.polyfill/dist/api/cjs/index.js';
import { auctionManager } from '../src/auctionManager.js';
import { config } from '../src/config.js';
import CONSTANTS from '../src/constants.json';
import { getGlobal } from '../src/prebidGlobal.js';
import * as utils from '../src/utils.js';

/**
* @summary This Module is intended to provide users with the ability to run a Fledge-based auction against a PreBid winning bid
*/
const MODULE_NAME = 'Fledge';

/**
* @summary The config to be used. Can be updated via: setConfig
*/
let _fledgeConfig = {};

/**
*
* @param {Object} reqBidsConfigObj required; This is the same param that's used in pbjs.requestBids.
* @param {function} fn required; The next function in the chain, used by hook.js
*/
export function renderAdHook(fn, doc, id, options) {
const bid = auctionManager.findBidByAdId(id);

if (_fledgeConfig.auctionConfig) {
const fledge = new Fledge('https://magniteengineering.github.io/fledge.polyfill/iframe.html');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is external code needed here? Also, please lock https://magniteengineering.github.io/fledge.polyfill/scripts/frame/esm/index.js to a version if external code is actually required. Also, since it requires promises / async await, please make sure the browser can handle these features before adding these requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why is external code needed here?

The external code is a polyfill that is required to work with Google's new Fledge proposal.

Also, please lock https://magniteengineering.github.io/fledge.polyfill/scripts/frame/esm/index.js to a version if external code is actually required.

This is locked down in the package.json to a version (https://github.com/prebid/Prebid.js/pull/7189/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R105)

Also, since it requires promises / async await, please make sure the browser can handle these features before adding these requests.

Do you have any suggestions for how to handle this? Do you guys have/use some sort of agent/browser sniffing that I could work with in order to prevent the code from running in older browsers? Technically this piece of code will only work with Chrome at version 91 or later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am confused, if you don't need the runtime code bc you included it at build time, why are you invoking it.

I would suggest not sniffing for user agents or browser versions but just checking for the existence of the required api or functionality

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am confused, if you don't need the runtime code bc you included it at build time, why are you invoking it.

I'm also confused by the concern and questions here.

I included the @magnite/fledge.polyfill library because it's a polyfill that is required to work with Fledge.

I would suggest not sniffing for user agents or browser versions but just checking for the existence of the required api or functionality

okay, thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@iamnewton I was looking at the polyfill and looks like it only supports chrome versions 91 and greater? Not sure if it's a problem but that is a pretty recent version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, this is new stuff that doesn't exist yet

Copy link
Member

Choose a reason for hiding this comment

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

That is intentional @ChrisHuie Chrome 91 will be the version of Chrome that supports Fledge. We have to match this "module" with requests from the right Chrome browser version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well Chrome 94 is out now, so it will be much later at this point

Copy link
Member

Choose a reason for hiding this comment

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

right I guess the pt is that this module is intended to only work on a very specific version of Chrome, TBD on which version.

const auctionConfig = _fledgeConfig.auctionConfig;
auctionConfig.auctionSignals.winningContextualBid = bid;
fledge
.runAdAuction(auctionConfig)
.then(results => {
if (results) {
// create an iframe within the Fledge auction iframe, render the winning ad
const ad = document.createElement('iframe');
ad.src = results;
document.getElementById(bid.adUnitCode).appendChild(ad);
} else {
return fn.call(this, doc, id, options);
}
});
} else {
return fn.call(this, doc, id, options);
}
}

/**
* @summary This is the function which controls what happens during a pbjs.setConfig({...floors: {}}) is called
*/
export function handleSetFledgeConfig(config) {
_fledgeConfig = utils.pick(config, [
'enabled', enabled => enabled !== false, // defaults to true
'auctionConfig',
]);

// if enabled then do some stuff
if (_fledgeConfig.enabled) {
getGlobal()
.renderAd
.before(renderAdHook, CONSTANTS.HOOK_PRIORITY.RENDER_AD);
} else {
utils.logInfo(`${MODULE_NAME}: Turning off module`);

_fledgeConfig = {};

getGlobal()
.renderAd
.getHooks({
hook: renderAdHook
})
.remove();
}
}

config.getConfig('fledge', config => handleSetFledgeConfig(config.fledge));
Loading