From 14e4a941c0514090de4d27bceb3ca91a616c0d7b Mon Sep 17 00:00:00 2001 From: Konduit <55142865+konduit-dev@users.noreply.github.com> Date: Thu, 7 May 2020 20:38:50 +0300 Subject: [PATCH] New version of Konduit Accelerate module (#5164) * Adding Konduit module * Removed superfluous arguments passed to obtainVastUrl function * Removed superfluous arguments passed to obtainVastUrl function. * Build trigger (empty commit) * Module documentation updated according to the comments * Logic in obtainVastUrl function updated according to the review comment. * Removed hook, enabled eslint * Merged recent prebid changes * New method is introduced to process a bid and return dynamic CPM data * New Konduit Analytics adapter responsible for client auction stats collection * Updated konduit analytics adapter .md file * Fixed linter issue with more than 1 blank line used * Use '$prebid.version$' instead of the $$PREBID_GLOBAL$$.version * Updated unit tests Co-authored-by: Max Shevchenko Co-authored-by: Alexander Kislitsyn --- modules/konduitAnalyticsAdapter.js | 225 ++++++++++++++++++ modules/konduitAnalyticsAdapter.md | 32 +++ modules/konduitWrapper.js | 203 ++++++++++++---- modules/konduitWrapper.md | 137 ++++++++--- .../modules/konduitAnalyticsAdapter_spec.js | 126 ++++++++++ test/spec/modules/konduitWrapper_spec.js | 143 +++++++---- 6 files changed, 740 insertions(+), 126 deletions(-) create mode 100644 modules/konduitAnalyticsAdapter.js create mode 100644 modules/konduitAnalyticsAdapter.md create mode 100644 test/spec/modules/konduitAnalyticsAdapter_spec.js diff --git a/modules/konduitAnalyticsAdapter.js b/modules/konduitAnalyticsAdapter.js new file mode 100644 index 00000000000..00df790b18a --- /dev/null +++ b/modules/konduitAnalyticsAdapter.js @@ -0,0 +1,225 @@ +import { ajax } from '../src/ajax.js'; +import adapter from '../src/AnalyticsAdapter.js'; +import adapterManager from '../src/adapterManager.js'; +import * as utils from '../src/utils.js'; +import { targeting } from '../src/targeting.js'; +import { config } from '../src/config.js'; +import CONSTANTS from '../src/constants.json'; + +const TRACKER_HOST = 'tracker.konduit.me'; + +const analyticsType = 'endpoint'; + +const eventDataComposerMap = { + [CONSTANTS.EVENTS.AUCTION_INIT]: obtainAuctionInfo, + [CONSTANTS.EVENTS.AUCTION_END]: obtainAuctionInfo, + [CONSTANTS.EVENTS.BID_REQUESTED]: obtainBidRequestsInfo, + [CONSTANTS.EVENTS.BID_TIMEOUT]: obtainBidTimeoutInfo, + [CONSTANTS.EVENTS.BID_RESPONSE]: obtainBidResponseInfo, + [CONSTANTS.EVENTS.BID_WON]: obtainWinnerBidInfo, + [CONSTANTS.EVENTS.NO_BID]: obtainNoBidInfo, +}; + +// This function is copy from prebid core +function formatQS(query) { + return Object + .keys(query) + .map(k => Array.isArray(query[k]) + ? query[k].map(v => `${k}[]=${v}`).join('&') + : `${k}=${query[k]}`) + .join('&'); +} + +// This function is copy from prebid core +function buildUrl(obj) { + return (obj.protocol || 'http') + '://' + + (obj.host || + obj.hostname + (obj.port ? `:${obj.port}` : '')) + + (obj.pathname || '') + + (obj.search ? `?${formatQS(obj.search || '')}` : '') + + (obj.hash ? `#${obj.hash}` : ''); +} + +const getWinnerBidFromAggregatedEvents = () => { + return konduitAnalyticsAdapter.context.aggregatedEvents + .filter(evt => evt.eventType === CONSTANTS.EVENTS.BID_WON)[0]; +}; + +const isWinnerBidDetected = () => { + return !!getWinnerBidFromAggregatedEvents(); +}; +const isWinnerBidExist = () => { + return !!targeting.getWinningBids()[0]; +}; + +const konduitAnalyticsAdapter = Object.assign( + adapter({ analyticsType }), + { + track ({ eventType, args }) { + if (CONSTANTS.EVENTS.AUCTION_INIT === eventType) { + konduitAnalyticsAdapter.context.aggregatedEvents.splice(0); + } + + if (eventDataComposerMap[eventType]) { + konduitAnalyticsAdapter.context.aggregatedEvents.push({ + eventType, + data: eventDataComposerMap[eventType](args), + }); + } + + if (eventType === CONSTANTS.EVENTS.AUCTION_END) { + if (!isWinnerBidDetected() && isWinnerBidExist()) { + const bidWonData = eventDataComposerMap[CONSTANTS.EVENTS.BID_WON](targeting.getWinningBids()[0]); + + konduitAnalyticsAdapter.context.aggregatedEvents.push({ + eventType: CONSTANTS.EVENTS.BID_WON, + data: bidWonData, + }); + } + sendRequest({ method: 'POST', path: '/analytics-initial-event', payload: composeRequestPayload() }); + } + } + } +); + +function obtainBidTimeoutInfo (args) { + return args.map(item => item.bidder).filter(utils.uniques); +} + +function obtainAuctionInfo (auction) { + return { + auctionId: auction.auctionId, + timestamp: auction.timestamp, + auctionEnd: auction.auctionEnd, + auctionStatus: auction.auctionStatus, + adUnitCodes: auction.adUnitCodes, + labels: auction.labels, + timeout: auction.timeout + }; +} + +function obtainBidRequestsInfo (bidRequests) { + return { + bidderCode: bidRequests.bidderCode, + time: bidRequests.start, + bids: bidRequests.bids.map(function (bid) { + return { + transactionId: bid.transactionId, + adUnitCode: bid.adUnitCode, + bidId: bid.bidId, + startTime: bid.startTime, + sizes: utils.parseSizesInput(bid.sizes).toString(), + params: bid.params + }; + }), + }; +} + +function obtainBidResponseInfo (bidResponse) { + return { + bidderCode: bidResponse.bidder, + transactionId: bidResponse.transactionId, + adUnitCode: bidResponse.adUnitCode, + statusMessage: bidResponse.statusMessage, + mediaType: bidResponse.mediaType, + renderedSize: bidResponse.size, + cpm: bidResponse.cpm, + currency: bidResponse.currency, + netRevenue: bidResponse.netRevenue, + timeToRespond: bidResponse.timeToRespond, + bidId: bidResponse.bidId, + requestId: bidResponse.requestId, + creativeId: bidResponse.creativeId + }; +} + +function obtainNoBidInfo (bidResponse) { + return { + bidderCode: bidResponse.bidder, + transactionId: bidResponse.transactionId, + adUnitCode: bidResponse.adUnitCode, + bidId: bidResponse.bidId, + }; +} + +function obtainWinnerBidInfo (bidResponse) { + return { + adId: bidResponse.adId, + bidderCode: bidResponse.bidder, + adUnitCode: bidResponse.adUnitCode, + statusMessage: bidResponse.statusMessage, + mediaType: bidResponse.mediaType, + renderedSize: bidResponse.size, + cpm: bidResponse.cpm, + currency: bidResponse.currency, + netRevenue: bidResponse.netRevenue, + timeToRespond: bidResponse.timeToRespond, + bidId: bidResponse.requestId, + dealId: bidResponse.dealId, + status: bidResponse.status, + creativeId: bidResponse.creativeId + }; +} + +function composeRequestPayload () { + const konduitId = config.getConfig('konduit.konduitId'); + const { width, height } = window.screen; + + return { + konduitId, + prebidVersion: '$prebid.version$', + environment: { + screen: { width, height }, + language: navigator.language, + }, + events: konduitAnalyticsAdapter.context.aggregatedEvents, + }; +} + +function sendRequest ({ host = TRACKER_HOST, method, path, payload }) { + const formattedUrlOptions = { + protocol: 'https', + hostname: host, + pathname: path, + }; + if (method === 'GET') { + formattedUrlOptions.search = payload; + } + + let konduitAnalyticsRequestUrl = buildUrl(formattedUrlOptions); + + ajax( + konduitAnalyticsRequestUrl, + undefined, + method === 'POST' ? JSON.stringify(payload) : null, + { + contentType: 'application/json', + method, + withCredentials: true + } + ); +} + +konduitAnalyticsAdapter.originEnableAnalytics = konduitAnalyticsAdapter.enableAnalytics; + +konduitAnalyticsAdapter.enableAnalytics = function (analyticsConfig) { + const konduitId = config.getConfig('konduit.konduitId'); + + if (!konduitId) { + utils.logError('A konduitId in config is required to use konduitAnalyticsAdapter'); + return; + } + + konduitAnalyticsAdapter.context = { + aggregatedEvents: [], + }; + + konduitAnalyticsAdapter.originEnableAnalytics(analyticsConfig); +}; + +adapterManager.registerAnalyticsAdapter({ + adapter: konduitAnalyticsAdapter, + code: 'konduit' +}); + +export default konduitAnalyticsAdapter; diff --git a/modules/konduitAnalyticsAdapter.md b/modules/konduitAnalyticsAdapter.md new file mode 100644 index 00000000000..c5854b77ccd --- /dev/null +++ b/modules/konduitAnalyticsAdapter.md @@ -0,0 +1,32 @@ +# Overview +​ +``` +Module Name: Konduit Analytics Adapter +Module Type: Analytics Adapter +Maintainer: support@konduit.me +``` +​ +​ +# Description +​ +Konduit Analytics adapter pushes Prebid events into Konduit platform, which is then organizes the data and presents it to a client in different insightful views. +​ +For more information, visit the [official Konduit website](https://konduitvideo.com/). +​ +​ +# Usage +​ +Konduit Analytics can be enabled with a standard `enableAnalytics` call. +Note it is also important to provide a valid Konduit identifier as a config parameter. +​ +```javascript +pbjs.setConfig({ + konduit: { + konduitId: your_konduit_id, + } +}); +​ +pbjs.enableAnalytics({ + provider: 'konduit' +}) +``` diff --git a/modules/konduitWrapper.js b/modules/konduitWrapper.js index 33bc45f7566..e015fb93616 100644 --- a/modules/konduitWrapper.js +++ b/modules/konduitWrapper.js @@ -2,87 +2,192 @@ import { registerVideoSupport } from '../src/adServerManager.js'; import { targeting } from '../src/targeting.js'; import * as utils from '../src/utils.js'; import { config } from '../src/config.js'; +import { ajaxBuilder } from '../src/ajax.js'; +import { getPriceBucketString } from '../src/cpmBucketManager.js'; +import { getPriceByGranularity } from '../src/auction.js'; + +const SERVER_PROTOCOL = 'https'; +const SERVER_HOST = 'p.konduit.me'; const MODULE_NAME = 'Konduit'; +const KONDUIT_ID_CONFIG = 'konduit.konduitId'; + +export const errorMessages = { + NO_KONDUIT_ID: 'A konduitId param is required to be in configs', + NO_BID: 'A bid was not found', + CACHE_FAILURE: 'A bid was not cached', +}; + +// This function is copy from prebid core +function formatQS(query) { + return Object + .keys(query) + .map(k => Array.isArray(query[k]) + ? query[k].map(v => `${k}[]=${v}`).join('&') + : `${k}=${query[k]}`) + .join('&'); +} + +// This function is copy from prebid core +function buildUrl(obj) { + return (obj.protocol || 'http') + '://' + + (obj.host || + obj.hostname + (obj.port ? `:${obj.port}` : '')) + + (obj.pathname || '') + + (obj.search ? `?${formatQS(obj.search || '')}` : '') + + (obj.hash ? `#${obj.hash}` : ''); +} + function addLogLabel(args) { args = [].slice.call(args); args.unshift(`${MODULE_NAME}: `); return args; } -export function logInfo() { +function logInfo() { utils.logInfo(...addLogLabel(arguments)); } -export function logError() { +function logError() { utils.logError(...addLogLabel(arguments)); } -export function buildVastUrl(options) { - if (!options.params || !options.params.konduit_id) { - logError(`'konduit_id' parameter is required for $$PREBID_GLOBAL$$.adServers.konduit.buildVastUrl function`); - - return null; +function sendRequest ({ host = SERVER_HOST, protocol = SERVER_PROTOCOL, method = 'GET', path, payload, callbacks, timeout }) { + const formattedUrlOptions = { + protocol: protocol, + hostname: host, + pathname: path, + }; + if (method === 'GET') { + formattedUrlOptions.search = payload; } - const bid = options.bid || targeting.getWinningBids()[0]; + let konduitAnalyticsRequestUrl = buildUrl(formattedUrlOptions); + const ajax = ajaxBuilder(timeout); + + ajax( + konduitAnalyticsRequestUrl, + callbacks, + method === 'POST' ? JSON.stringify(payload) : null, + { + contentType: 'application/json', + method, + withCredentials: true + } + ); +} - if (!bid) { - logError('Bid is not provided or not found'); +/** + * This function accepts an object with bid and tries to cache it while generating konduit_cache_key for it. + * In addition, it returns a list with updated bid objects where k_cpm key is added + * @param {Object} options + * @param {Object} [options.bid] - winner bid from publisher + * @param {string} [options.adUnitCode] - to look for winner bid + * @param {string} [options.timeout] - timeout for bidsProcessor request + * @param {function} [options.callback] - callback will be called in the end of the request + */ +export function processBids(options = {}) { + const konduitId = config.getConfig(KONDUIT_ID_CONFIG); + options = options || {}; + + if (!konduitId) { + logError(errorMessages.NO_KONDUIT_ID); + + if (options.callback) { + options.callback(new Error(errorMessages.NO_KONDUIT_ID)); + } return null; } - logInfo('The following bid will be wrapped: ', bid); - - const queryParams = {}; + const bid = options.bid || targeting.getWinningBids(options.adUnitCode)[0]; - const vastUrl = obtainVastUrl(bid); - - if (vastUrl) { - queryParams.konduit_id = options.params.konduit_id; - queryParams.konduit_header_bidding = 1; - queryParams.konduit_url = vastUrl; - } else { - logError('No VAST url found in the bid'); - } - - let resultingUrl = null; + if (!bid) { + logError(errorMessages.NO_BID); - if (queryParams.konduit_url) { - resultingUrl = utils.buildUrl({ - protocol: 'https', - host: 'p.konduit.me', - pathname: '/api/vastProxy', - search: queryParams - }); + if (options.callback) { + options.callback(new Error(errorMessages.NO_BID)); + } - logInfo(`Konduit wrapped VAST url: ${resultingUrl}`); + return null; } - return resultingUrl; -} - -function obtainVastUrl(bid) { - const vastUrl = bid && bid.vastUrl; + const priceGranularity = config.getConfig('priceGranularity'); - if (vastUrl) { - logInfo(`VAST url found in the bid - ${vastUrl}`); + bid.kCpm = bid.cpm; - return encodeURIComponent(vastUrl); + if (!bid.adserverTargeting) { + bid.adserverTargeting = {}; } - const cacheUrl = config.getConfig('cache.url'); - if (cacheUrl) { - const composedCacheUrl = `${cacheUrl}?uuid=${bid.videoCacheKey}`; - - logInfo(`VAST url is taken from cache.url: ${composedCacheUrl}`); - - return encodeURIComponent(composedCacheUrl); - } + bid.adserverTargeting.k_cpm = getPriceByGranularity(priceGranularity)(bid); + + const bidsToProcess = [{ + auctionId: bid.auctionId, + vastUrl: bid.vastUrl, + bidderCode: bid.bidderCode, + creativeId: bid.creativeId, + adUnitCode: bid.adUnitCode, + cpm: bid.cpm, + currency: bid.currency, + }]; + + sendRequest({ + method: 'POST', + path: '/api/bidsProcessor', + timeout: options.timeout || 1000, + payload: { + clientId: konduitId, + bids: bidsToProcess, + }, + callbacks: { + success: (data) => { + let error = null; + logInfo('Bids processed successfully ', data); + try { + const { kCpmData, cacheData } = JSON.parse(data); + const processedBidKey = `${bid.bidderCode}:${bid.creativeId}`; + + if (!utils.isEmpty(cacheData)) { + bid.adserverTargeting.konduit_id = konduitId; + } else { + error = new Error(errorMessages.CACHE_FAILURE); + } + + if (utils.isNumber(kCpmData[processedBidKey])) { + bid.kCpm = kCpmData[processedBidKey]; + const priceStringsObj = getPriceBucketString( + bid.kCpm, + config.getConfig('customPriceBucket'), + config.getConfig('currency.granularityMultiplier') + ); + bid.adserverTargeting.k_cpm = priceStringsObj.custom || priceStringsObj[priceGranularity] || priceStringsObj.med; + } + + if (utils.isStr(cacheData[processedBidKey])) { + bid.konduitCacheKey = cacheData[processedBidKey]; + bid.adserverTargeting.konduit_cache_key = cacheData[processedBidKey]; + } + } catch (err) { + error = err; + logError('Error parsing JSON response for bidsProcessor data: ', err) + } + + if (options.callback) { + options.callback(error, [bid]); + } + }, + error: (error) => { + logError('Bid was not processed successfully ', error); + if (options.callback) { + options.callback(utils.isStr(error) ? new Error(error) : error, [bid]); + } + } + } + }); } registerVideoSupport('konduit', { - buildVastUrl: buildVastUrl, + processBids: processBids, }); diff --git a/modules/konduitWrapper.md b/modules/konduitWrapper.md index adbb50487da..3097801ffab 100644 --- a/modules/konduitWrapper.md +++ b/modules/konduitWrapper.md @@ -1,11 +1,84 @@ -## Konduit video tags wrapper +# Overview + +``` +Module Name: Konduit Accelerate +Module Type: Video Module +Maintainer: support@konduit.me +``` + +# Description + +Konduit Wrapper is a prebid module that allows +- wrapping a bid response so that it is processed through Konduit platform +- obtaining a historical performance indicator for a bid + + +# Configuration + +## Building Prebid with the Konduit wrapper function + +Your Prebid build must include the **konduitWrapper** module. Follow the build instructions for Prebid as explained in the top level README.md file of the Prebid source tree. + +ex: $ gulp build --modules=konduitWrapper -Konduit Wrapper is a prebid module to generate Konduit wrapped VAST tag URLs for a provided bid or a winning bid. +## Prebid related configuration -### Setup +Konduit module should be used with a valid Konduit identifier. +```javascript +pbjs.setConfig({ + konduit: { + konduitId: your_konduit_id, + } +}); +``` + +Please contact support@konduit.me for assistance. + + +## GAM related configuration + +It is important to configure your GAM line items. +Please contact support@konduit.me for assistance. + +In most cases it would require only Creative VAST URL update with the following URL: +``` +https://p.konduit.me/api/vastProxy?konduit_hb=1&konduit_hb_awarded=1&konduit_cache_key=%%PATTERN:konduit_cache_key%%&konduit_id=%%PATTERN:konduit_id%% ``` + + +# Usage + +Konduit module contains a single function that accepts an `options` parameter. + +The `options` parameter can include: +* `bid` - prebid object with VAST url that should be cached (if not passed first winning bid from `auctionManager.getWinningBids()` will be used) +* `adUnitCode` - adUnitCode where a winner bid can be found +* `timeout` - max time to wait for Konduit response with cache key and kCpm data +* `callback` - callback function is called once Konduit cache data for the bid. Arguments of this function are - `error` and `bids` (error should be `null` if Konduit request is successful) + +The function adds two parameters into the passed bid - kCpm and konduitCacheKey. Additionally `processBids` updates bid's `adserverTargeting` with `k_cpm`, `konduti_cache_key` and `konduit_id` fields. + + +```javascript +pbjs.requestBids({ + bidsBackHandler: function (bids) { + pbjs.adServers.konduit.processBids({ + callback: function (error, bids) { + var videoUrl = pbjs.adServers.dfp.buildVideoUrl({ + ... + }); + } + }); + } +}) +``` + + +# Sample code + +```javascript var videoAdUnit = [{ code: 'videoAd', mediaTypes: { @@ -28,46 +101,48 @@ var videoAdUnit = [{ pbjs.que.push(function(){ pbjs.addAdUnits(videoAdUnit); + pbjs.setConfig({ + konduit: { + konduitId: 'your_konduit_id', + }, + }); + pbjs.requestBids({ - timeout : 700, bidsBackHandler : function(bids) { var winnerBid = pbjs.getHighestCpmBids('videoAd')[0]; - var vastTagUrl = pbjs.adServers.konduit.buildVastUrl({ - bid: winnerBid, // just in case if you want to pass your bid - params: { - konduit_id: 'your_konduit_id' + pbjs.adServers.konduit.processBids({ + bid: winnerBid, + adUnitCode: videoAdUnit[0].code, + timeout: 2000, + callback: function (error, processedBids) { + var vastTagUrl = pbjs.adServers.dfp.buildVideoUrl({ + adUnit: videoAdUnit, + params: { + iu: '', + output: 'vast', + }, + }); + + invokeVideoPlayer(vastTagUrl); } }); - - invokeVideoPlayer(vastTagUrl); } }); }); function invokeVideoPlayer(vastTagUrl) { - videojs("video_player_id").ready(function() { - this.vastClient({ - adTagUrl: vastTagUrl, - playAdAlways: true, - verbosity: 4, - autoplay: true - }); - - this.play(); + videojs("video_player_id").ready(function() { + this.vastClient({ + adTagUrl: vastTagUrl, + playAdAlways: true, + verbosity: 4, + autoplay: true }); - } -``` - -Function parameters: -* `bid` - prebid object with VAST url that should be wrapped (if not passed first winning bid from `auctionManager.getWinningBids()` is used) -* `konduit_id` - your personal unique Konduit identifier (required) - -The function returns a Konduit wrapped VAST url if valid parameters are passed in. If some of the parameters are not passed or are invalid the function returns 'null' along with related error logs providing more details. + this.play(); + }); +} +``` -### Building Prebid with the Konduit wrapper function -Your Prebid build must include the **konduitWrapper** module. Follow the build instructions for Prebid as explained in the top level README.md file of the Prebid source tree. - -ex: $ gulp build --modules=konduitWrapper diff --git a/test/spec/modules/konduitAnalyticsAdapter_spec.js b/test/spec/modules/konduitAnalyticsAdapter_spec.js new file mode 100644 index 00000000000..ac557d27f90 --- /dev/null +++ b/test/spec/modules/konduitAnalyticsAdapter_spec.js @@ -0,0 +1,126 @@ +import konduitAnalyticsAdapter from 'modules/konduitAnalyticsAdapter'; +import { expect } from 'chai'; +import { config } from '../../../src/config.js'; +import { server } from 'test/mocks/xhr.js'; +let events = require('src/events'); +let adapterManager = require('src/adapterManager').default; +let CONSTANTS = require('src/constants.json'); + +const eventsData = { + [CONSTANTS.EVENTS.AUCTION_INIT]: { + 'auctionId': 'test_auction_id', + 'timestamp': Date.now(), + 'auctionStatus': 'inProgress', + 'adUnitCodes': ['video-test'], + 'timeout': 700 + }, + [CONSTANTS.EVENTS.BID_REQUESTED]: { + 'bidderCode': 'test_bidder_code', + 'time': Date.now(), + 'bids': [{ + 'transactionId': 'test_transaction_id', + 'adUnitCode': 'video-test', + 'bidId': 'test_bid_id', + 'sizes': '640x480', + 'params': { 'testParam': 'test_param' } + }] + }, + [CONSTANTS.EVENTS.NO_BID]: { + 'bidderCode': 'test_bidder_code2', + 'transactionId': 'test_transaction_id', + 'adUnitCode': 'video-test', + 'bidId': 'test_bid_id' + }, + [CONSTANTS.EVENTS.BID_RESPONSE]: { + 'bidderCode': 'test_bidder_code', + 'adUnitCode': 'video-test', + 'statusMessage': 'Bid available', + 'mediaType': 'video', + 'renderedSize': '640x480', + 'cpm': 0.5, + 'currency': 'USD', + 'netRevenue': true, + 'timeToRespond': 124, + 'requestId': 'test_request_id', + 'creativeId': 144876543 + }, + [CONSTANTS.EVENTS.AUCTION_END]: { + 'auctionId': 'test_auction_id', + 'timestamp': Date.now(), + 'auctionEnd': Date.now() + 400, + 'auctionStatus': 'completed', + 'adUnitCodes': ['video-test'], + 'timeout': 700 + }, + [CONSTANTS.EVENTS.BID_WON]: { + 'bidderCode': 'test_bidder_code', + 'adUnitCode': 'video-test', + 'statusMessage': 'Bid available', + 'mediaType': 'video', + 'renderedSize': '640x480', + 'cpm': 0.5, + 'currency': 'USD', + 'netRevenue': true, + 'timeToRespond': 124, + 'requestId': 'test_request_id', + 'creativeId': 144876543 + }, +}; + +describe(`Konduit Analytics Adapter`, () => { + const konduitId = 'test'; + + beforeEach(function () { + sinon.spy(konduitAnalyticsAdapter, 'track'); + sinon.stub(events, 'getEvents').returns([]); + config.setConfig({ konduit: { konduitId } }); + }); + + afterEach(function () { + events.getEvents.restore(); + konduitAnalyticsAdapter.track.restore(); + konduitAnalyticsAdapter.disableAnalytics(); + }); + + it(`should add all events to an aggregatedEvents queue + inside konduitAnalyticsAdapter.context and send a request with correct data`, function () { + server.respondWith(JSON.stringify({ key: 'test' })); + + adapterManager.registerAnalyticsAdapter({ + code: 'konduit', + adapter: konduitAnalyticsAdapter + }); + + adapterManager.enableAnalytics({ + provider: 'konduit', + }); + + expect(konduitAnalyticsAdapter.context).to.be.an('object'); + expect(konduitAnalyticsAdapter.context.aggregatedEvents).to.be.an('array'); + + const eventTypes = [ + CONSTANTS.EVENTS.AUCTION_INIT, + CONSTANTS.EVENTS.BID_REQUESTED, + CONSTANTS.EVENTS.NO_BID, + CONSTANTS.EVENTS.BID_RESPONSE, + CONSTANTS.EVENTS.BID_WON, + CONSTANTS.EVENTS.AUCTION_END, + ]; + const args = eventTypes.map(eventType => eventsData[eventType]); + + eventTypes.forEach((eventType, i) => { + events.emit(eventType, args[i]); + }); + + server.respond(); + + expect(konduitAnalyticsAdapter.context.aggregatedEvents.length).to.be.equal(6); + expect(server.requests[0].url).to.match(/http(s):\/\/\w*\.konduit\.me\/analytics-initial-event/); + + const requestBody = JSON.parse(server.requests[0].requestBody); + expect(requestBody.konduitId).to.be.equal(konduitId); + expect(requestBody.prebidVersion).to.be.equal('$prebid.version$'); + expect(requestBody.environment).to.be.an('object'); + sinon.assert.callCount(konduitAnalyticsAdapter.track, 6); + }); +}); diff --git a/test/spec/modules/konduitWrapper_spec.js b/test/spec/modules/konduitWrapper_spec.js index 4a0c627e885..d70cb7a6c60 100644 --- a/test/spec/modules/konduitWrapper_spec.js +++ b/test/spec/modules/konduitWrapper_spec.js @@ -1,72 +1,123 @@ import { expect } from 'chai'; -import parse from 'url-parse'; -import { buildVastUrl } from 'modules/konduitWrapper.js'; -import { parseQS } from 'src/utils.js'; +import { processBids, errorMessages } from 'modules/konduitWrapper.js'; import { config } from 'src/config.js'; +import { server } from 'test/mocks/xhr.js'; describe('The Konduit vast wrapper module', function () { - it('should make a wrapped request url when `bid` passed', function () { - const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + const konduitId = 'test'; + beforeEach(function() { + config.setConfig({ konduit: { konduitId } }); + }); - const url = parse(buildVastUrl({ - bid, - params: { 'konduit_id': 'testId' }, - })); + describe('processBids function', () => { + it(`should make a correct processBids request and add kCpm and konduitCacheKey + to the passed bids and to the adserverTargeting object`, function () { + const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); - expect(url.protocol).to.equal('https:'); - expect(url.host).to.equal('p.konduit.me'); + server.respondWith(JSON.stringify({ + kCpmData: { [`${bid.bidderCode}:${bid.creativeId}`]: bid.cpm }, + cacheData: { [`${bid.bidderCode}:${bid.creativeId}`]: 'test_cache_key' }, + })); - const queryParams = parseQS(url.query); - expect(queryParams).to.have.property('konduit_url', encodeURIComponent('http://some-vast-url.com')); - expect(queryParams).to.have.property('konduit_header_bidding', '1'); - expect(queryParams).to.have.property('konduit_id', 'testId'); - }); + processBids({ bid }); + server.respond(); - it('should return null when no `konduit_id` (required param) passed', function () { - const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + expect(server.requests.length).to.equal(1); - const url = buildVastUrl({ bid }); + const requestBody = JSON.parse(server.requests[0].requestBody); - expect(url).to.equal(null); - }); + expect(requestBody.clientId).to.equal(konduitId); - it('should return null when either bid or adUnit is not passed', function () { - const url = buildVastUrl({ params: { 'konduit_id': 'testId' } }); + expect(bid.konduitCacheKey).to.equal('test_cache_key'); + expect(bid.kCpm).to.equal(bid.cpm); - expect(url).to.equal(null); - }); + expect(bid.adserverTargeting).to.be.an('object'); + + expect(bid.adserverTargeting.k_cpm).to.equal(bid.pbCg || bid.pbAg); + expect(bid.adserverTargeting.konduit_cache_key).to.equal('test_cache_key'); + expect(bid.adserverTargeting.konduit_id).to.equal(konduitId); + }); + + it(`should call callback with error object in arguments if cacheData is empty in the response`, function () { + const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + + server.respondWith(JSON.stringify({ + kCpmData: { [`${bid.bidderCode}:${bid.creativeId}`]: bid.cpm }, + cacheData: {}, + })); + const callback = sinon.spy(); + processBids({ bid, callback }); + server.respond(); - it('should return null when bid does not contain vastUrl', function () { - const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + expect(server.requests.length).to.equal(1); - delete bid.vastUrl; + const requestBody = JSON.parse(server.requests[0].requestBody); - const url = buildVastUrl({ - bid, - params: { 'konduit_id': 'testId' }, + expect(requestBody.clientId).to.equal(konduitId); + + expect(bid.konduitCacheKey).to.be.undefined; + expect(bid.kCpm).to.equal(bid.cpm); + + expect(bid.adserverTargeting.k_cpm).to.equal(bid.pbCg || bid.pbAg); + expect(bid.adserverTargeting.konduit_cache_key).to.be.undefined; + expect(bid.adserverTargeting.konduit_id).to.be.undefined; + + expect(callback.firstCall.args[0]).to.be.an('error'); }); - expect(url).to.equal(null); - }); + it('should call callback if processBids request is sent successfully', function () { + const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + server.respondWith(JSON.stringify({ key: 'test' })); + const callback = sinon.spy(); + processBids({ + bid, + callback + }); + server.respond(); + + expect(callback.calledOnce).to.be.true; + }); - it('should return wrapped vastUrl based on cached url in params', function () { - config.setConfig({ cache: { url: 'https://cached.url.com' } }); - const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + it('should call callback with error object in arguments if processBids request is failed', function () { + const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + const callback = sinon.spy(); + processBids({ + bid, + callback + }); + server.respond(); + + expect(callback.calledOnce).to.be.true; + expect(callback.firstCall.args[0]).to.be.an('error'); + }); - delete bid.vastUrl; + it('should call callback with error object in arguments if no konduitId in configs', function () { + config.setConfig({ konduit: { konduitId: null } }); - const expectedUrl = encodeURIComponent(`https://cached.url.com?uuid=${bid.videoCacheKey}`); + const bid = createBid(10, 'video1', 15, '10.00_15s', '123', '395'); + const callback = sinon.spy(); + processBids({ + bid, + callback + }); - const url = parse(buildVastUrl({ - bid, - params: { 'konduit_id': 'testId' }, - })); - const queryParams = parseQS(url.query); + expect(callback.calledOnce).to.be.true; + expect(callback.firstCall.args[0]).to.be.an('error'); + expect(callback.firstCall.args[0].message).to.equal(errorMessages.NO_KONDUIT_ID); + }); - expect(queryParams).to.have.property('konduit_url', expectedUrl); + it('should call callback with error object in arguments if no bids found', function () { + const callback = sinon.spy(); + processBids({ + bid: null, + callback + }); - config.resetConfig(); + expect(callback.calledOnce).to.be.true; + expect(callback.firstCall.args[0]).to.be.an('error'); + expect(callback.firstCall.args[0].message).to.equal(errorMessages.NO_BID); + }); }); }); @@ -103,7 +154,7 @@ function createBid(cpm, adUnitCode, durationBucket, priceIndustryDuration, uuid, 'pbLg': '5.00', 'pbMg': '5.00', 'pbHg': '5.00', - 'pbAg': '5.00', + 'pbAg': `${cpm}.00`, 'pbDg': '5.00', 'pbCg': '', 'size': '640x360',