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

Underdog Media 1.0 Compliance #1801

Merged
merged 2 commits into from
Nov 8, 2017
Merged
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
145 changes: 75 additions & 70 deletions modules/underdogmediaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,117 @@
import * as utils from 'src/utils';
import { config } from 'src/config';
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader.js');
var utils = require('src/utils.js');
var adaptermanager = require('src/adaptermanager');

function UnderdogMediaAdapter() {
const UDM_ADAPTER_VERSION = '1.0.0';
var getJsStaticUrl = window.location.protocol + '//udmserve.net/udm/img.fetch?tid=1;dt=9;callback=$$PREBID_GLOBAL$$.handleUnderdogMediaCB;';
var bidParams = {};

function _callBids(params) {
bidParams = params;
import { registerBidder } from 'src/adapters/bidderFactory';
const BIDDER_CODE = 'underdogmedia';
const UDM_ADAPTER_VERSION = '1.0';

export const spec = {
code: BIDDER_CODE,
bidParams: [],

isBidRequestValid: function (bid) {
return !!((bid.params && bid.params.siteId) && (bid.sizes && bid.sizes.length > 0));
},

buildRequests: function (validBidRequests) {
var sizes = [];
var siteId = 0;

bidParams.bids.forEach(bidParam => {
validBidRequests.forEach(bidParam => {
sizes = utils.flatten(sizes, utils.parseSizesInput(bidParam.sizes));
siteId = bidParam.params.siteId;
});
adloader.loadScript(getJsStaticUrl + 'sid=' + siteId + ';sizes=' + sizes.join(','), null, false);
}

function _callback(response) {
var mids = response.mids;
bidParams.bids.forEach(bidParam => {
var filled = false;
mids.forEach(mid => {
return {
method: 'GET',
url: `${window.location.protocol}//udmserve.net/udm/img.fetch`,
data: `tid=1;dt=10;sid=${siteId};sizes=${sizes.join(',')}`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

More params can be added to request object here. So you can add bidParams to bidRequest and access that in interpretResponse method.
By doing this you can remove bidParams line 15.

bidParams: validBidRequests
};
},

interpretResponse: function (serverResponse, bidRequest) {
const bidResponses = [];
bidRequest.bidParams.forEach(bidParam => {
serverResponse.body.mids.forEach(mid => {
if (mid.useCount > 0) {
return;
}

if (!mid.useCount) {
mid.useCount = 0;
}

var size_not_found = true;
utils.parseSizesInput(bidParam.sizes).forEach(size => {
if (size === mid.width + 'x' + mid.height) {
size_not_found = false;
}
});

if (size_not_found) {
return;
}

var bid = bidfactory.createBid(1, bidParam);
bid.bidderCode = bidParam.bidder;
bid.width = mid.width;
bid.height = mid.height;
const bidResponse = {
requestId: bidParam.bidId,
bidderCode: spec.code,
cpm: parseFloat(mid.cpm),
width: mid.width,
height: mid.height,
ad: mid.ad_code_html,
creativeId: mid.mid,
currency: 'USD',
netRevenue: false,
ttl: config.getConfig('_bidderTimeout'),
};

bid.cpm = parseFloat(mid.cpm);
if (bid.cpm <= 0) {
if (bidResponse.cpm <= 0) {
return;
}
mid.useCount++;
bid.ad = mid.ad_code_html;
bid.ad = _makeNotification(bid, mid, bidParam) + bid.ad;
if (!(bid.ad || bid.adUrl)) {
if (bidResponse.ad.length <= 0) {
return;
}
bidmanager.addBidResponse(bidParam.placementCode, bid);
filled = true;

mid.useCount++;

bidResponse.ad += makeNotification(bidResponse, mid, bidParam);

bidResponses.push(bidResponse);
});
if (!filled) {
var nobid = bidfactory.createBid(2, bidParam);
nobid.bidderCode = bidParam.bidder;
bidmanager.addBidResponse(bidParam.placementCode, nobid);
}
});
}

$$PREBID_GLOBAL$$.handleUnderdogMediaCB = _callback;
return bidResponses;
},
};

function _makeNotification(bid, mid, bidParam) {
var url = mid.notification_url;
function makeNotification (bid, mid, bidParam) {
var url = mid.notification_url;

url += UDM_ADAPTER_VERSION;
url += ';cb=' + Math.random();
url += ';qqq=' + (1 / bid.cpm);
url += ';hbt=' + config.getConfig('bidderTimeout');
url += ';style=adapter';
url += ';vis=' + encodeURIComponent(document.visibilityState);
url += UDM_ADAPTER_VERSION;
url += ';cb=' + Math.random();
url += ';qqq=' + (1 / bid.cpm);
url += ';hbt=' + config.getConfig('_bidderTimeout');
url += ';style=adapter';
url += ';vis=' + encodeURIComponent(document.visibilityState);

url += ';traffic_info=' + encodeURIComponent(JSON.stringify(_getUrlVars()));
if (bidParam.params.subId) {
url += ';subid=' + encodeURIComponent(bidParam.params.subId);
}
return '<script async src="' + url + '"></script>';
url += ';traffic_info=' + encodeURIComponent(JSON.stringify(getUrlVars()));
if (bidParam.params.subId) {
url += ';subid=' + encodeURIComponent(bidParam.params.subId);
}
return '<script async src="' + url + '"></script>';
}

function _getUrlVars() {
var vars = {};
var hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
if (!hash[0].match(/^utm/)) {
continue;
}
function getUrlVars() {
var vars = {};
var hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
if (hash[0].match(/^utm_/)) {
vars[hash[0]] = hash[1].substr(0, 150);
}
return vars;
}

return {
callBids: _callBids
};
return vars;
}

adaptermanager.registerBidAdapter(new UnderdogMediaAdapter(), 'underdogmedia');

module.exports = UnderdogMediaAdapter;
registerBidder(spec);
27 changes: 27 additions & 0 deletions modules/underdogmediaBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Overview

**Module Name**: Underdog Media Bidder Adapter
**Module Type**: Bidder Adapter
**Maintainer**: jake@underdogmedia.com

# Description

Module that connects to Underdog Media's servers to fetch bids.

# Test Parameters
```
var adUnits = [
{
code: 'test-div',
sizes: [[300, 250]], // a display size
bids: [
{
bidder: "underdogmedia",
params: {
siteId: '12143'
}
}
]
}
];
```
Loading