Skip to content

Commit

Permalink
Add AdgenerationBidAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
takaaki.kojima authored and takaaki.kojima committed Jan 18, 2018
1 parent 4418fec commit d8e8ae6
Show file tree
Hide file tree
Showing 3 changed files with 556 additions and 0 deletions.
189 changes: 189 additions & 0 deletions modules/adgenerationBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import * as utils from 'src/utils';
// import {config} from 'src/config';
import {registerBidder} from 'src/adapters/bidderFactory';
import { NATIVE } from 'src/mediaTypes';
const ADG_BIDDER_CODE = 'adgeneration';

export const spec = {
code: ADG_BIDDER_CODE,
aliases: ['adg'], // short code
supportedMediaTypes: [NATIVE],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
return !!(bid.params.id && (bid.params.width && bid.params.height));
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests) {
let serverRequests = [];
for (let i = 0, len = validBidRequests.length; i < len; i++) {
const validReq = validBidRequests[i];
const url = validReq.params.debug ? 'http://api-test.scaleout.jp/adsv/v1' : 'https://d.socdm.com/adsv/v1';
let data = ``;
data = utils.tryAppendQueryString(data, 'posall', 'SSPLOC');
const id = utils.getBidIdParameter('id', validReq.params);
data = utils.tryAppendQueryString(data, 'id', id);
data = utils.tryAppendQueryString(data, 'sdktype', '0');
data = utils.tryAppendQueryString(data, 'hb', 'true');
data = utils.tryAppendQueryString(data, 't', 'json3');
data = utils.tryAppendQueryString(data, 'transactionid', validReq.transactionId);

// remove the trailing "&"
if (data.lastIndexOf('&') === data.length - 1) {
data = data.substring(0, data.length - 1);
}
serverRequests.push({
method: 'GET',
url: url,
data: data,
bidRequest: validBidRequests[i]
});
}
return serverRequests;
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {BidRequest} bidRequest
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequests) {
const body = serverResponse.body;
const bidResponses = [];
let bidRequest = {};
if (body.results && body.results.length > 0) {
bidRequest = bidRequests.bidRequest;
const bidResponse = {
requestId: bidRequest.bidId,
cpm: body.cpm || 0,
width: bidRequest.params.width,
height: bidRequest.params.height,
creativeId: body.creativeid || '',
dealId: body.dealid || '',
currency: 'JPY',
netRevenue: true,
ttl: body.ttl || 10,
referrer: utils.getTopWindowUrl(),
};
if (bidRequest.mediaTypes && bidRequest.mediaTypes.native) {
bidResponse.native = createNativeAd(body);
bidResponse.mediaType = NATIVE;
} else {
// banner
bidResponse.ad = createAd(body, bidRequest);
}
bidResponses.push(bidResponse);
}
return bidResponses; // noAdは空を返す
},

/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = [];
return syncs;
}
};

function createAd(body, bidRequest) {
let ad = body.ad;
if (body.vastxml && body.vastxml.length > 0) {
ad = `<div id="apvad-${bidRequest.bidId}"></div>` +
appendChildToBody(ad, createAPVTag() + insertVASTMethod(bidRequest.bidId, body.vastxml));
}
ad = appendChildToBody(ad, body.beacon);
if (removeWrapper(ad)) return removeWrapper(ad);
return ad;
}

function createNativeAd(body) {
let native = {};
if (body.native_ad && body.native_ad.assets.length > 0) {
const assets = body.native_ad.assets;
for (let i = 0, len = assets.length; i < len; i++) {
switch (assets[i].id) {
case 1:
native.title = assets[i].title.text;
break;
case 2:
native.image = {
url: assets[i].img.url,
height: assets[i].img.h,
width: assets[i].img.w,
};
break;
case 3:
native.icon = {
url: assets[i].img.url,
height: assets[i].img.h,
width: assets[i].img.w,
};
break;
case 4:
native.sponsoredBy = assets[i].data.value;
break;
case 5:
native.body = assets[i].data.value;
break;
case 6:
native.cta = assets[i].data.value;
break;
}
}
native.clickUrl = body.native_ad.link.url;
native.clickTrackers = body.native_ad.link.clicktrackers || [];
native.impressionTrackers = body.native_ad.imptrackers || [];
}
return native;
}

function appendChildToBody(ad, data) {
return ad.replace(/<\/\s?body>/, `${data}</body>`);
}

function createAPVTag() {
const APVURL = 'https://cdn.apvdr.com/js/VideoAd.min.js';
let apvScript = document.createElement('script');
apvScript.type = 'text/javascript';
apvScript.id = 'apv';
apvScript.src = APVURL;
return apvScript.outerHTML;
}

function insertVASTMethod(targetId, vastXml) {
let apvVideoAdParam = {
s: targetId
};
let script = document.createElement(`script`);
script.type = 'text/javascript';
script.innerHTML = `(function(){ new APV.VideoAd(${JSON.stringify(apvVideoAdParam)}).load('${vastXml.replace(/\r?\n/g, '')}'); })();`;
return script.outerHTML;
}

/**
*
* @param ad
*/
function removeWrapper(ad) {
const bodyIndex = ad.indexOf('<body>');
const lastBodyIndex = ad.lastIndexOf('</body>');
if (bodyIndex === -1 || lastBodyIndex === -1) return false;
return ad.substr(bodyIndex, lastBodyIndex).replace('<body>', '').replace('</body>', '');
}

registerBidder(spec);
72 changes: 72 additions & 0 deletions modules/adgenerationBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Overview

```
Module Name: Adgeneration Bid Adapter
Module Type: Bidder Adapter
Maintainer: ssp-ope@supership.jp
```

# Description

Connects to Adgeneration exchange for bids.

Adgeneration bid adapter supports Banner and Native.

# Test Parameters
```
var adUnits = [
// Banner adUnit
{
code: 'banner-div', // banner
sizes: [[300, 250]],
bids: [
{
bidder: 'adg',
params: {
id: '58278', // banner
width: '300',
height: '250'
}
},
]
},
// Native adUnit
{
code: 'native-div',
sizes: [[1,1]],
mediaTypes: {
native: {
image: {
required: true
},
title: {
required: true,
len: 80
},
sponsoredBy: {
required: true
},
clickUrl: {
required: true
},
body: {
required: true
},
icon: {
required: true
}
},
},
bids: [
{
bidder: 'adg',
params: {
id: '58279', //native
width: '1',
height: '1'
}
},
]
},
];
```
Loading

0 comments on commit d8e8ae6

Please sign in to comment.