diff --git a/integrationExamples/gpt/airgridRtdProvider_example.html b/integrationExamples/gpt/airgridRtdProvider_example.html new file mode 100644 index 00000000000..a8fd989f682 --- /dev/null +++ b/integrationExamples/gpt/airgridRtdProvider_example.html @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + +

AirGrid RTD Prebid

+ +
+ +
+ + AirGrid Audiences: +
+ + diff --git a/modules/airgridRtdProvider.js b/modules/airgridRtdProvider.js new file mode 100644 index 00000000000..8d212204da8 --- /dev/null +++ b/modules/airgridRtdProvider.js @@ -0,0 +1,138 @@ +/** + * This module adds the AirGrid provider to the real time data module + * The {@link module:modules/realTimeData} module is required + * The module will fetch real-time audience data from AirGrid + * @module modules/airgridRtdProvider + * @requires module:modules/realTimeData + */ +import {config} from '../src/config.js'; +import {submodule} from '../src/hook.js'; +import {mergeDeep, isPlainObject, deepSetValue, deepAccess} from '../src/utils.js'; +import {getGlobal} from '../src/prebidGlobal.js'; +import {getStorageManager} from '../src/storageManager.js'; + +const MODULE_NAME = 'realTimeData'; +const SUBMODULE_NAME = 'airgrid'; +const AG_TCF_ID = 782; +export const AG_AUDIENCE_IDS_KEY = 'edkt_matched_audience_ids' + +export const storage = getStorageManager(AG_TCF_ID, SUBMODULE_NAME); + +/** + * Attach script tag to DOM + * @param {Object} rtdConfig + * @return {void} + */ +export function attachScriptTagToDOM(rtdConfig) { + var edktInitializor = window.edktInitializor = window.edktInitializor || {}; + if (!edktInitializor.invoked) { + edktInitializor.invoked = true; + edktInitializor.accountId = rtdConfig.params.accountId; + edktInitializor.publisherId = rtdConfig.params.publisherId; + edktInitializor.apiKey = rtdConfig.params.apiKey; + edktInitializor.load = function(e) { + var p = e || 'sdk'; + var n = document.createElement('script'); + n.type = 'text/javascript'; + n.async = true; + n.src = 'https://cdn.edkt.io/' + p + '/edgekit.min.js'; + document.getElementsByTagName('head')[0].appendChild(n); + }; + edktInitializor.load(edktInitializor.accountId); + } +} + +/** + * Fetch audiences from localStorage + * @return {Array} + */ +export function getMatchedAudiencesFromStorage() { + const audiences = storage.getDataFromLocalStorage(AG_AUDIENCE_IDS_KEY); + if (!audiences) return [] + try { + return JSON.parse(audiences); + } catch (e) { + return []; + } +} + +/** + * Mutates the adUnits object + * @param {Object} adUnits + * @param {Array} audiences + * @return {void} + */ +function setAudiencesToAppNexusAdUnits(adUnits, audiences) { + adUnits.forEach((adUnit) => { + adUnit.bids.forEach((bid) => { + if (bid.bidder && bid.bidder === 'appnexus') { + deepSetValue(bid, 'params.keywords.perid', audiences || []); + } + }) + }) +} + +/** + * Pass audience data to configured bidders, using ORTB2 + * @param {Object} rtdConfig + * @param {Array} audiences + * @return {void} + */ +export function setAudiencesUsingBidderOrtb2(rtdConfig, audiences) { + const bidders = deepAccess(rtdConfig, 'params.bidders'); + if (!bidders || bidders.length === 0) return; + const allBiddersConfig = config.getBidderConfig(); + const agOrtb2 = {} + deepSetValue(agOrtb2, 'ortb2.user.ext.data.airgrid', audiences || []); + + bidders.forEach((bidder) => { + let bidderConfig = {}; + if (isPlainObject(allBiddersConfig[bidder])) { + bidderConfig = allBiddersConfig[bidder]; + } + config.setBidderConfig({ + bidders: [bidder], + config: mergeDeep(bidderConfig, agOrtb2) + }); + }); +} + +/** + * Module init + * @param {Object} rtdConfig + * @param {Object} userConsent + * @return {boolean} + */ +function init(rtdConfig, userConsent) { + attachScriptTagToDOM(rtdConfig); + return true; +} + +/** + * Real-time data retrieval from AirGrid + * @param {Object} reqBidsConfigObj + * @param {function} onDone + * @param {Object} rtdConfig + * @param {Object} userConsent + * @return {void} + */ +export function passAudiencesToBidders(bidConfig, onDone, rtdConfig, userConsent) { + const adUnits = bidConfig.adUnits || getGlobal().adUnits; + const audiences = getMatchedAudiencesFromStorage(); + if (audiences.length > 0) { + setAudiencesUsingBidderOrtb2(rtdConfig, audiences); + if (adUnits) { + setAudiencesToAppNexusAdUnits(adUnits, audiences); + } + } + onDone(); +}; + +/** @type {RtdSubmodule} */ +export const airgridSubmodule = { + name: SUBMODULE_NAME, + init: init, + getBidRequestData: passAudiencesToBidders +}; + +submodule(MODULE_NAME, airgridSubmodule); diff --git a/modules/airgridRtdProvider.md b/modules/airgridRtdProvider.md new file mode 100644 index 00000000000..7ee502b4c10 --- /dev/null +++ b/modules/airgridRtdProvider.md @@ -0,0 +1,95 @@ + --- + layout: page_v2 + title: AirGrid RTD SubModule + description: Client-side, cookieless and privacy-first audiences. + page_type: module + module_type: rtd + module_code : example + enable_download : true + sidebarType : 1 + --- + +# AirGrid + +AirGrid is a privacy-first, cookie-less audience platform. Designed to help publishers increase inventory yield, +whilst providing audience signal to buyers in the bid request, without exposing raw user level data to any party. + +This real-time data module provides quality first-party data, contextual data, site-level data and more that is +injected into bid request objects destined for different bidders in order to optimize targeting. + +## Usage + +Compile the Halo RTD module into your Prebid build: + +`gulp build --modules=rtdModule,airgridRtdProvider,appnexusBidAdapter` + +Add the AirGrid RTD provider to your Prebid config. In this example we will configure publisher 1234 to retrieve segments from Audigent. See the "Parameter Descriptions" below for more detailed information of the configuration parameters. + +```js +pbjs.setConfig( + ... + realTimeData: { + auctionDelay: 1000, + dataProviders: [ + { + name: 'airgrid', + waitForIt: true, + params: { + // These are unique values for each account. + apiKey: 'apiKey', + accountId: 'accountId', + publisherId: 'publisherId', + bidders: ['appnexus', 'pubmatic'] + } + } + ] + } + ... +} +``` + +### Parameter Descriptions + +| Name |Type | Description | Notes | +| :------------ | :------------ | :------------ |:------------ | +| name | `String` | RTD sub module name | Always 'airgrid' | +| waitForIt | `Boolean` | Wether to delay auction for module response | Optional. Defaults to false | +| params.apiKey | `Boolean` | Publisher partner specific API key | Required | +| params.accountId | `String` | Publisher partner specific account ID | Required | +| params.publisherId | `String` | Publisher partner specific publisher ID | Required | +| params.bidders | `Array` | Bidders with which to share segment information | Optional | + +_Note: Although the module supports passing segment data to any bidder using the ORTB2 spec, there is no way for this to be currently monetised. Please reach out to support, to discuss using bidders other than Xandr/AppNexus._ + +If you do not have your own `apiKey`, `accountId` & `publisherId` please reach out to [support@airgrid.io](mailto:support@airgrid.io) + +## Testing + +To view an example of the on page setup required: + +```bash +gulp serve-fast --modules=rtdModule,airgridRtdProvider,appnexusBidAdapter +``` + +Then in your browser access: + +``` +http://localhost:9999/integrationExamples/gpt/airgridRtdProvider_example.html +``` + +Run the unit tests, just on the AirGrid RTD module test file: + +```bash +gulp test --file "test/spec/modules/airgridRtdProvider_spec.js" +``` + +## Support + +If you require further assistance or are interested in discussing the module functionality please reach out to: +- [hello@airgrid.io](mailto:hello@airgrid.io) for general questions. +- [support@airgrid.io](mailto:support@airgrid.io) for technical questions. + +You are also able to find more examples and other integration routes on the [AirGrid docs site](docs.airgrid.io). + +Happy Coding! 😊 +The AirGrid Team. diff --git a/test/spec/modules/airgridRtdProvider_spec.js b/test/spec/modules/airgridRtdProvider_spec.js new file mode 100644 index 00000000000..cc10dda4ad1 --- /dev/null +++ b/test/spec/modules/airgridRtdProvider_spec.js @@ -0,0 +1,97 @@ +import {config} from 'src/config.js'; +import {deepAccess} from 'src/utils.js' +import {getAdUnits} from '../../fixtures/fixtures.js'; +import * as agRTD from 'modules/airgridRtdProvider.js'; + +const MATCHED_AUDIENCES = ['travel', 'sport']; +const RTD_CONFIG = { + auctionDelay: 250, + dataProviders: [{ + name: 'airgrid', + waitForIt: true, + params: { + apiKey: 'key123', + accountId: 'sdk', + publisherId: 'pub123', + bidders: ['pubmatic'] + } + }] +}; + +describe('airgrid RTD Submodule', function() { + let getDataFromLocalStorageStub; + + beforeEach(function() { + config.resetConfig(); + getDataFromLocalStorageStub = sinon.stub(agRTD.storage, 'getDataFromLocalStorage'); + }); + + afterEach(function () { + getDataFromLocalStorageStub.restore(); + }); + + describe('Initialise module', function() { + it('should initalise and return true', function () { + expect(agRTD.airgridSubmodule.init(RTD_CONFIG.dataProviders[0])).to.equal(true); + }); + it('should attach script to DOM with correct config', function() { + agRTD.attachScriptTagToDOM(RTD_CONFIG); + expect(window.edktInitializor.invoked).to.be.true; + expect(window.edktInitializor.apiKey).to.equal(RTD_CONFIG.dataProviders[0].params.apiKey); + expect(window.edktInitializor.accountId).to.equal(RTD_CONFIG.dataProviders[0].params.accountId); + expect(window.edktInitializor.publisherId).to.equal(RTD_CONFIG.dataProviders[0].params.publisherId); + }); + }); + + describe('Get matched audiences', function() { + it('gets matched audiences from local storage', function() { + getDataFromLocalStorageStub.withArgs(agRTD.AG_AUDIENCE_IDS_KEY).returns(JSON.stringify(MATCHED_AUDIENCES)); + + const audiences = agRTD.getMatchedAudiencesFromStorage(); + expect(audiences).to.have.members(MATCHED_AUDIENCES); + }); + }); + + describe('Add matched audiences', function() { + it('merges matched audiences on appnexus AdUnits', function() { + const adUnits = getAdUnits(); + getDataFromLocalStorageStub.withArgs(agRTD.AG_AUDIENCE_IDS_KEY).returns(JSON.stringify(MATCHED_AUDIENCES)); + agRTD.passAudiencesToBidders({ adUnits }, () => {}, {}, {}); + + adUnits.forEach(adUnit => { + adUnit.bids.forEach(bid => { + const { bidder, params } = bid; + if (bidder === 'appnexus') { + expect(deepAccess(params, 'keywords.perid')).to.eql(MATCHED_AUDIENCES); + } + }); + }); + }); + it('does not merge audiences on appnexus adunits, since none are matched', function() { + const adUnits = getAdUnits(); + getDataFromLocalStorageStub.withArgs(agRTD.AG_AUDIENCE_IDS_KEY).returns(undefined); + agRTD.passAudiencesToBidders({ adUnits }, () => {}, {}, {}); + + adUnits.forEach(adUnit => { + adUnit.bids.forEach(bid => { + const { bidder, params } = bid; + if (bidder === 'appnexus') { + expect(deepAccess(params, 'keywords.perid')).to.be.undefined; + } + }); + }); + }); + it('sets bidder specific ORTB2 config', function() { + getDataFromLocalStorageStub.withArgs(agRTD.AG_AUDIENCE_IDS_KEY).returns(JSON.stringify(MATCHED_AUDIENCES)); + const audiences = agRTD.getMatchedAudiencesFromStorage(); + agRTD.setAudiencesUsingBidderOrtb2(RTD_CONFIG.dataProviders[0], audiences); + + const allBiddersConfig = config.getBidderConfig(); + const bidders = RTD_CONFIG.dataProviders[0].params.bidders; + Object.keys(allBiddersConfig).forEach((bidder) => { + if (bidders.indexOf(bidder) === -1) return; + expect(deepAccess(allBiddersConfig[bidder], 'ortb2.user.ext.data.airgrid')).to.eql(MATCHED_AUDIENCES); + }); + }); + }); +});