Skip to content

Commit

Permalink
Merge branch 'master' into prebid-8
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed May 4, 2023
2 parents a48caf0 + bc665aa commit ec7681f
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 131 deletions.
39 changes: 32 additions & 7 deletions modules/freewheel-sspBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logWarn, isArray, isFn, deepAccess } from '../src/utils.js';
import { logWarn, isArray, isFn, deepAccess, formatQS } from '../src/utils.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
Expand Down Expand Up @@ -372,6 +372,15 @@ export const spec = {
requestParams._fw_us_privacy = bidderRequest.uspConsent;
}

// Add GPP consent
if (bidderRequest && bidderRequest.gppConsent) {
requestParams.gpp = bidderRequest.gppConsent.gppString;
requestParams.gpp_sid = bidderRequest.gppConsent.applicableSections;
} else if (bidderRequest && bidderRequest.ortb2 && bidderRequest.ortb2.regs && bidderRequest.ortb2.regs.gpp) {
requestParams.gpp = bidderRequest.ortb2.regs.gpp;
requestParams.gpp_sid = bidderRequest.ortb2.regs.gpp_sid;
}

// Add schain object
var schain = currentBidRequest.schain;
if (schain) {
Expand Down Expand Up @@ -526,26 +535,42 @@ export const spec = {
return bidResponses;
},

getUserSyncs: function(syncOptions, responses, gdprConsent, usPrivacy) {
var gdprParams = '';
getUserSyncs: function(syncOptions, responses, gdprConsent, usPrivacy, gppConsent) {
const params = {};

if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
gdprParams = `?gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
params.gdpr = Number(gdprConsent.gdprApplies);
params.gdpr_consent = gdprConsent.consentString;
} else {
gdprParams = `?gdpr_consent=${gdprConsent.consentString}`;
params.gdpr_consent = gdprConsent.consentString;
}
}

if (gppConsent) {
if (typeof gppConsent.gppString === 'string') {
params.gpp = gppConsent.gppString;
}
if (gppConsent.applicableSections) {
params.gpp_sid = gppConsent.applicableSections;
}
}

var queryString = '';
if (params) {
queryString = '?' + `${formatQS(params)}`;
}

const syncs = [];
if (syncOptions && syncOptions.pixelEnabled) {
syncs.push({
type: 'image',
url: USER_SYNC_URL + gdprParams
url: USER_SYNC_URL + queryString
});
} else if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: USER_SYNC_URL + gdprParams
url: USER_SYNC_URL + queryString
});
}

Expand Down
110 changes: 110 additions & 0 deletions modules/greenbidsRtdProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';

const MODULE_NAME = 'greenbidsRtdProvider';
const MODULE_VERSION = '1.0.0';
const ENDPOINT = 'https://europe-west1-greenbids-357713.cloudfunctions.net/partner-selection';

const auctionInfo = {};
const rtdOptions = {};

function init(moduleConfig) {
let params = moduleConfig?.params;
if (!params?.pbuid) {
logError('Greenbids pbuid is not set!');
return false;
} else {
rtdOptions.pbuid = params?.pbuid;
rtdOptions.targetTPR = params?.targetTPR || 0.99;
rtdOptions.timeout = params?.timeout || 200;
return true;
}
}

function onAuctionInitEvent(auctionDetails) {
auctionInfo.auctionId = auctionDetails.auctionId;
}

function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) {
let promise = createPromise(reqBidsConfigObj);
promise.then(callback);
}

function createPromise(reqBidsConfigObj) {
return new Promise((resolve) => {
const timeoutId = setTimeout(() => {
resolve(reqBidsConfigObj);
}, rtdOptions.timeout);
ajax(
ENDPOINT,
{
success: (response) => {
processSuccessResponse(response, timeoutId, reqBidsConfigObj);
resolve(reqBidsConfigObj);
},
error: () => {
clearTimeout(timeoutId);
resolve(reqBidsConfigObj);
},
},
createPayload(reqBidsConfigObj),
{ contentType: 'application/json' }
);
});
}

function processSuccessResponse(response, timeoutId, reqBidsConfigObj) {
clearTimeout(timeoutId);
const responseAdUnits = JSON.parse(response);

updateAdUnitsBasedOnResponse(reqBidsConfigObj.adUnits, responseAdUnits);
}

function updateAdUnitsBasedOnResponse(adUnits, responseAdUnits) {
adUnits.forEach((adUnit) => {
const matchingAdUnit = findMatchingAdUnit(responseAdUnits, adUnit.code);
if (matchingAdUnit) {
removeFalseBidders(adUnit, matchingAdUnit);
}
});
}

function findMatchingAdUnit(responseAdUnits, adUnitCode) {
return responseAdUnits.find((responseAdUnit) => responseAdUnit.code === adUnitCode);
}

function removeFalseBidders(adUnit, matchingAdUnit) {
const falseBidders = getFalseBidders(matchingAdUnit.bidders);
adUnit.bids = adUnit.bids.filter((bidRequest) => !falseBidders.includes(bidRequest.bidder));
}

function getFalseBidders(bidders) {
return Object.entries(bidders)
.filter(([bidder, shouldKeep]) => !shouldKeep)
.map(([bidder]) => bidder);
}

function createPayload(reqBidsConfigObj) {
return JSON.stringify({
auctionId: auctionInfo.auctionId,
version: MODULE_VERSION,
referrer: window.location.href,
prebid: '$prebid.version$',
rtdOptions: rtdOptions,
adUnits: reqBidsConfigObj.adUnits,
});
}

export const greenbidsSubmodule = {
name: MODULE_NAME,
init: init,
onAuctionInitEvent: onAuctionInitEvent,
getBidRequestData: getBidRequestData,
updateAdUnitsBasedOnResponse: updateAdUnitsBasedOnResponse,
findMatchingAdUnit: findMatchingAdUnit,
removeFalseBidders: removeFalseBidders,
getFalseBidders: getFalseBidders,
};

submodule('realTimeData', greenbidsSubmodule);
65 changes: 65 additions & 0 deletions modules/greenbidsRtdProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Overview

```
Module Name: Greenbids RTD Provider
Module Type: RTD Provider
Maintainer: [email protected]
```

# Description

The Greenbids RTD adapter allows to dynamically filter calls to SSP to reduce outgoing call to the programmatics chain, reducing ad serving carbon impact

## Configuration

This module is configured as part of the `realTimeData.dataProviders` object.

{: .table .table-bordered .table-striped }
| Name | Scope | Description | Example | Type |
|------------|----------|----------------------------------------|---------------|----------|
| `name ` | required | Real time data module name | `'greenbidsRtdProvider'` | `string` |
| `waitForIt ` | required (mandatory true value) | Tells prebid auction to wait for the result of this module | `'true'` | `boolean` |
| `params` | required | | | `Object` |
| `params.pbuid` | required | The client site id provided by Greenbids. | `'TEST_FROM_GREENBIDS'` | `string` |
| `params.targetTPR` | optional (default 0.95) | Target True positive rate for the throttling model | `0.99` | `[0-1]` |
| `params.timeout` | optional (default 200) | Maximum amount of milliseconds allowed for module to finish working (has to be <= to the realTimeData.auctionDelay property) | `200` | `number` |

#### Example

```javascript
const greenbidsDataProvider = {
name: 'greenbidsRtdProvider',
waitForIt: true,
params: {
pbuid: 'TEST_FROM_GREENBIDS',
timeout: 200
}
};

pbjs.setConfig({
realTimeData: {
auctionDelay: 200,
dataProviders: [greenbidsDataProvider]
}
});
```

## Integration
To install the module, follow these instructions:

#### Step 1: Contact Greenbids to get a pbuid and account

#### Step 2: Integrate the Greenbids Analytics Adapter

Greenbids RTD module works hand in hand with Greenbids Analytics module
See prebid Analytics modules -> Greenbids Analytics module

#### Step 3: Prepare the base Prebid file

- Option 1: Use Prebid [Download](/download.html) page to build the prebid package. Ensure that you do check *Greenbids RTD Provider* module

- Option 2: From the command line, run `gulp build --modules=greenbidsRtdProvider,...`

#### Step 4: Set configuration

Enable Greenbids Real Time Module using `pbjs.setConfig`. Example is provided in Configuration section.
Loading

0 comments on commit ec7681f

Please sign in to comment.