diff --git a/integrationExamples/gpt/pbjs_example_gpt.html b/integrationExamples/gpt/pbjs_example_gpt.html index 536bc5a655d..88d4839d984 100644 --- a/integrationExamples/gpt/pbjs_example_gpt.html +++ b/integrationExamples/gpt/pbjs_example_gpt.html @@ -302,6 +302,16 @@ channelCode: 2264002816, //REQUIRED dimId: 9 //REQUIRED } + }, + { + bidder: 'openxoutstream', + params: { + unit: '53943996499', + delDomain: 'se-demo-d.openx.net', + publisher_page_url: 'yieldmo.com', + width: '300', + height: '250', + } } ] diff --git a/modules/openxoutstreamBidAdapter.js b/modules/openxoutstreamBidAdapter.js new file mode 100644 index 00000000000..6da234284c6 --- /dev/null +++ b/modules/openxoutstreamBidAdapter.js @@ -0,0 +1,214 @@ +import { config } from 'src/config'; +import { registerBidder } from 'src/adapters/bidderFactory'; +import * as utils from 'src/utils'; +import { BANNER } from 'src/mediaTypes'; + +const SUPPORTED_AD_TYPES = [BANNER]; +const BIDDER_CODE = 'openxoutstream'; +const BIDDER_CONFIG = 'hb_pb_ym'; +const BIDDER_VERSION = '1.0.0'; +const CURRENCY = 'USD'; +const NET_REVENUE = true; +const TIME_TO_LIVE = 300; +const YM_SCRIPT = `!function(e,t){if(void 0===t._ym){var a=Math.round(5*Math.random()/3)+'';t._ym='';var m=e.createElement('script');m.type='text/javascript',m.async=!0,m.src='//static.yieldmo.com/ym.'+a+'.js',(e.getElementsByTagName('head')[0]||e.getElementsByTagName('body')[0]).appendChild(m)}else t._ym instanceof String||void 0===t._ym.chkPls||t._ym.chkPls()}(document,window);`; +const PLACEMENT_ID = '1986307928000988495'; +const PUBLISHER_ID = '1986307525700126029'; +const CR_ID = '2052941939925262540'; +const AD_ID = '1991358644725162800'; + +export const spec = { + code: BIDDER_CODE, + supportedMediaTypes: SUPPORTED_AD_TYPES, + isBidRequestValid: function(bidRequest) { + if (bidRequest.params.delDomain) { + return !!bidRequest.params.unit || utils.deepAccess(bidRequest, 'mediaTypes.banner.sizes.length') > 0; + } + return false; + }, + buildRequests: function(bidRequests, bidderRequest) { + if (bidRequests.length === 0) { + return []; + } + let requests = []; + requests.push(buildOXBannerRequest(bidRequests, bidderRequest)); + return requests; + }, + interpretResponse: function(serverResponse, serverRequest) { + return handleVastResponse(serverResponse, serverRequest.payload) + }, + + transformBidParams: function(params, isOpenRtb) { + return utils.convertTypes({ + 'unit': 'string', + }, params); + } +}; + +function getViewportDimensions(isIfr) { + let width; + let height; + let tWin = window; + let body; + + if (isIfr) { + let tDoc; + try { + tWin = window.top; + tDoc = window.top.document; + } catch (e) { + return; + } + body = tDoc.body; + + width = tWin.innerWidth || docEl.clientWidth || body.clientWidth; + height = tWin.innerHeight || docEl.clientHeight || body.clientHeight; + } else { + width = tWin.innerWidth || docEl.clientWidth; + height = tWin.innerHeight || docEl.clientHeight; + } + + return `${width}x${height}`; +} + +function buildCommonQueryParamsFromBids(bids, bidderRequest) { + const isInIframe = utils.inIframe(); + let defaultParams; + defaultParams = { + ju: config.getConfig('pageUrl') || utils.getTopWindowUrl(), + jr: utils.getTopWindowReferrer(), + ch: document.charSet || document.characterSet, + res: `${screen.width}x${screen.height}x${screen.colorDepth}`, + ifr: isInIframe, + tz: new Date().getTimezoneOffset(), + tws: getViewportDimensions(isInIframe), + be: 1, + bc: bids[0].params.bc || `${BIDDER_CONFIG}_${BIDDER_VERSION}`, + auid: '540141567', + dddid: utils._map(bids, bid => bid.transactionId).join(','), + openrtb: '%7B%22mimes%22%3A%5B%22video%2Fmp4%22%5D%7D', + nocache: new Date().getTime() + }; + + if (utils.deepAccess(bidderRequest, 'gdprConsent')) { + let gdprConsentConfig = bidderRequest.gdprConsent; + + if (gdprConsentConfig.consentString !== undefined) { + defaultParams.gdpr_consent = gdprConsentConfig.consentString; + } + + if (gdprConsentConfig.gdprApplies !== undefined) { + defaultParams.gdpr = gdprConsentConfig.gdprApplies ? 1 : 0; + } + + if (config.getConfig('consentManagement.cmpApi') === 'iab') { + defaultParams.x_gdpr_f = 1; + } + } + + return defaultParams; +} + +function buildOXBannerRequest(bids, bidderRequest) { + let queryParams = buildCommonQueryParamsFromBids(bids, bidderRequest); + queryParams.aus = utils._map(bids, bid => utils.parseSizesInput(bid.sizes).join(',')).join('|'); + + if (bids.some(bid => bid.params.doNotTrack)) { + queryParams.ns = 1; + } + + if (bids.some(bid => bid.params.coppa)) { + queryParams.tfcd = 1; + } + + let url = `https://${bids[0].params.delDomain}/v/1.0/avjp` + return { + method: 'GET', + url: url, + data: queryParams, + payload: {'bids': bids} + }; +} + +function handleVastResponse(response, serverResponse) { + const body = response.body + let bidResponses = []; + if (response !== undefined && body.vastUrl !== '' && body.pub_rev && body.pub_rev > 0) { + const openHtmlTag = ''; + const closeHtmlTag = ''; + const sdkScript = createSdkScript().outerHTML; + const placementDiv = createPlacementDiv(); + placementDiv.dataset.pId = PUBLISHER_ID; + const placementDivString = placementDiv.outerHTML; + const adResponse = getTemplateAdResponse(body.vastUrl); + const adResponseString = JSON.stringify(adResponse); + const ymAdsScript = ''; + + let bidResponse = {}; + bidResponse.requestId = serverResponse.bids[0].bidId; + bidResponse.bidderCode = BIDDER_CODE; + bidResponse.netRevenue = NET_REVENUE; + bidResponse.currency = CURRENCY; + bidResponse.cpm = Number(body.pub_rev) / 1000; + bidResponse.creativeId = body.adid; + bidResponse.height = body.height; + bidResponse.width = body.width; + bidResponse.vastUrl = body.vastUrl; + bidResponse.ttl = TIME_TO_LIVE; + bidResponse.mediaType = BANNER; + bidResponse.ad = openHtmlTag + placementDivString + ymAdsScript + sdkScript + closeHtmlTag; + + bidResponses.push(bidResponse); + } + return bidResponses; +} +registerBidder(spec); + +// HELPER FUNCTIONS +function createSdkScript() { + const script = document.createElement('script'); + script.innerHTML = YM_SCRIPT; + return script; +} +function createPlacementDiv() { + const div = document.createElement('div'); + div.id = `ym_${PLACEMENT_ID}`; + div.classList.add('ym'); + div.dataset.lfId = CR_ID; + return div +} + +/** + * Create a nativeplay template with the placement id and vastURL. + * @param vastUrl + */ +const getTemplateAdResponse = (vastUrl) => { + return { + availability_zone: 'us-east-1a', + data: [ + { + ads: [ + { + actions: {}, + adv_id: AD_ID, + configurables: { + cta_button_copy: 'Learn More', + vast_click_tracking: 'true', + vast_url: vastUrl, + }, + cr_id: CR_ID, + } + ], + column_count: 1, + configs: { + allowable_height: '248', + header_copy: 'You May Like', + ping: 'true', + }, + creative_format_id: 40, + css: '', + placement_id: PLACEMENT_ID, + } + ], + nc: 0, + }; +}; diff --git a/modules/openxoutstreamBidAdapter.md b/modules/openxoutstreamBidAdapter.md new file mode 100644 index 00000000000..a77b4560f97 --- /dev/null +++ b/modules/openxoutstreamBidAdapter.md @@ -0,0 +1,41 @@ +# Overview + +``` +Module Name: OpenX Outstream Bidder Adapter +Module Type: Bidder Adapter +Maintainer: opensource@yieldmo.com, jimmy.tu@openx.com +Note: Ads will only render in mobile +``` + +# Description + +Module that connects to OpenX's demand sources for outstream to Yieldmo. + +This bid adapter supports Banner. + +# Example +```javascript +var adUnits = [ + { + code: 'test-div', + sizes: [[300, 250]], // a display size + mediaTypes: {'banner': {}}, + bids: [ + { + bidder: 'openxoutstream', + params: { + unit: '53943996499', + delDomain: 'se-demo-d.openx.net', + publisher_page_url: 'yieldmo.com', + width: '300', + height: '250', + } + } + ] + } +]; +``` + +# Additional Details +[Banner Ads](https://docs.openx.com/Content/developers/containers/prebid-adapter.html) + diff --git a/test/spec/modules/openxoutstreamBidAdapter_spec.js b/test/spec/modules/openxoutstreamBidAdapter_spec.js new file mode 100644 index 00000000000..a1d4f4aa2d4 --- /dev/null +++ b/test/spec/modules/openxoutstreamBidAdapter_spec.js @@ -0,0 +1,238 @@ +import {expect} from 'chai'; +import {spec} from 'modules/openxoutstreamBidAdapter'; +import {newBidder} from 'src/adapters/bidderFactory'; + +describe('OpenXOutstreamAdapter', function () { + const adapter = newBidder(spec); + const URLBASE = '/v/1.0/avjp'; + const BIDDER = 'openxoutstream'; + const div = document.createElement('div'); + const PLACEMENT_ID = '1986307928000988495'; + const YM_SCRIPT = `!function(e,t){if(void 0===t._ym){var a=Math.round(5*Math.random()/3)+'';t._ym='';var m=e.createElement('script');m.type='text/javascript',m.async=!0,m.src='//static.yieldmo.com/ym.'+a+'.js',(e.getElementsByTagName('head')[0]||e.getElementsByTagName('body')[0]).appendChild(m)}else t._ym instanceof String||void 0===t._ym.chkPls||t._ym.chkPls()}(document,window);`; + const PUBLISHER_ID = '1986307525700126029'; + const CR_ID = '2052941939925262540'; + const AD_ID = '1991358644725162800'; + + describe('inherited functions', function () { + it('exists and is a function', function () { + expect(adapter.callBids).to.exist.and.to.be.a('function'); + }); + }); + + describe('isBidRequestValid', function () { + describe('when request is for a banner ad', function () { + let bannerBid; + beforeEach(function () { + bannerBid = { + bidder: BIDDER, + params: {}, + adUnitCode: 'adunit-code', + mediaTypes: {banner: {}}, + sizes: [[300, 250], [300, 600]], + bidId: '30b31c1838de1e', + bidderRequestId: '22edbae2733bf6', + auctionId: '1d1a030790a475' + }; + }); + it('should return false when there is no delivery domain', function () { + bannerBid.params = {'unit': '12345678'}; + expect(spec.isBidRequestValid(bannerBid)).to.equal(false); + }); + + describe('when there is a delivery domain', function () { + beforeEach(function () { + bannerBid.params = {delDomain: 'test-delivery-domain'} + }); + + it('should return false when there is no ad unit id and size', function () { + expect(spec.isBidRequestValid(bannerBid)).to.equal(false); + }); + + it('should return true if there is an adunit id ', function () { + bannerBid.params.unit = '12345678'; + expect(spec.isBidRequestValid(bannerBid)).to.equal(true); + }); + + it('should return true if there is no adunit id and sizes are defined', function () { + bannerBid.mediaTypes.banner.sizes = [720, 90]; + expect(spec.isBidRequestValid(bannerBid)).to.equal(true); + }); + + it('should return false if no sizes are defined ', function () { + expect(spec.isBidRequestValid(bannerBid)).to.equal(false); + }); + + it('should return false if sizes empty ', function () { + bannerBid.mediaTypes.banner.sizes = []; + expect(spec.isBidRequestValid(bannerBid)).to.equal(false); + }); + }); + }); + }); + + describe('buildRequests for banner ads', function () { + const bidRequestsWithMediaType = [{ + 'bidder': BIDDER, + 'params': { + 'unit': '12345678', + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + 'mediaType': 'banner', + 'sizes': [[300, 250], [300, 600]], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475' + }]; + + it('should send bid request to openx url via GET, with mediaType specified as banner', function () { + const request = spec.buildRequests(bidRequestsWithMediaType); + const params = bidRequestsWithMediaType[0].params; + expect(request[0].url).to.equal(`https://` + params.delDomain + URLBASE); + expect(request[0].method).to.equal('GET'); + }); + + it('should send ad unit ids when any are defined', function () { + const bidRequestsWithUnitIds = [{ + 'bidder': BIDDER, + 'params': { + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + mediaTypes: { + banner: { + sizes: [[300, 250], [300, 600]] + } + }, + 'bidId': 'test-bid-id-1', + 'bidderRequestId': 'test-bid-request-1', + 'auctionId': 'test-auction-1' + }, { + 'bidder': BIDDER, + 'params': { + 'unit': '540141567', + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + mediaTypes: { + banner: { + sizes: [[728, 90]] + } + }, + 'bidId': 'test-bid-id-2', + 'bidderRequestId': 'test-bid-request-2', + 'auctionId': 'test-auction-2' + }]; + const request = spec.buildRequests(bidRequestsWithUnitIds); + expect(request[0].data.auid).to.equal(`${bidRequestsWithUnitIds[1].params.unit}`); + }); + + describe('interpretResponse', function () { + let serverResponse; + let serverRequest; + + beforeEach(function () { + serverResponse = { + body: { + width: 300, + height: 250, + pub_rev: 3000, + bidderCode: 'openxoutstream', + vastUrl: 'test.vast.url', + mediaType: 'banner', + adid: '9874652394875' + }, + header: 'header?', + }; + serverRequest = { + payload: { + bids: [{ + bidId: '2d36ac90d654af', + }], + } + }; + }); + + it('should correctly reorder the server response', function () { + const newResponse = spec.interpretResponse(serverResponse, serverRequest); + const openHtmlTag = ''; + const closeHtmlTag = ''; + const sdkScript = createSdkScript().outerHTML; + const placementDiv = createPlacementDiv(); + placementDiv.dataset.pId = PUBLISHER_ID; + const placementDivString = placementDiv.outerHTML; + const adResponse = getTemplateAdResponse(serverResponse.body.vastUrl, PLACEMENT_ID); + const adResponseString = JSON.stringify(adResponse); + const ymAdsScript = ''; + expect(newResponse.length).to.be.equal(1); + expect(newResponse[0]).to.deep.equal({ + requestId: '2d36ac90d654af', + bidderCode: 'openxoutstream', + vastUrl: 'test.vast.url', + mediaType: 'banner', + cpm: 3, + width: 300, + height: 250, + creativeId: '9874652394875', + currency: 'USD', + netRevenue: true, + ttl: 300, + ad: openHtmlTag + placementDivString + ymAdsScript + sdkScript + closeHtmlTag + }); + }); + + it('should not add responses if the cpm is 0 or null', function () { + serverResponse.body.pub_rev = 0; + let response = spec.interpretResponse(serverResponse, serverRequest); + expect(response).to.deep.equal([]); + + serverResponse.body.pub_rev = null; + response = spec.interpretResponse(serverResponse, serverRequest); + expect(response).to.deep.equal([]) + }); + }); + }) + + function createSdkScript() { + const script = document.createElement('script'); + script.innerHTML = YM_SCRIPT; + return script; + } + function createPlacementDiv() { + div.id = `ym_${PLACEMENT_ID}`; + div.classList.add('ym'); + div.dataset.lfId = CR_ID; + return div + } + const getTemplateAdResponse = (vastUrl) => { + return { + availability_zone: 'us-east-1a', + data: [ + { + ads: [ + { + actions: {}, + adv_id: AD_ID, + configurables: { + cta_button_copy: 'Learn More', + vast_click_tracking: 'true', + vast_url: vastUrl, + }, + cr_id: CR_ID, + } + ], + column_count: 1, + configs: { + allowable_height: '248', + header_copy: 'You May Like', + ping: 'true', + }, + creative_format_id: 40, + css: '', + placement_id: PLACEMENT_ID, + } + ], + nc: 0, + }; + }; +});