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

Updated a4g adapter #5688

Merged
merged 2 commits into from
Sep 7, 2020
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
90 changes: 90 additions & 0 deletions modules/a4gBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import * as utils from '../src/utils.js';

const A4G_BIDDER_CODE = 'a4g';
const A4G_CURRENCY = 'USD';
const A4G_DEFAULT_BID_URL = 'https://ads.ad4game.com/v1/bid';
const A4G_TTL = 120;

const LOCATION_PARAM_NAME = 'siteurl';
const ID_PARAM_NAME = 'id';
const IFRAME_PARAM_NAME = 'if';
const ZONE_ID_PARAM_NAME = 'zoneId';
const SIZE_PARAM_NAME = 'size';

const ARRAY_PARAM_SEPARATOR = ';';
const ARRAY_SIZE_SEPARATOR = ',';
const SIZE_SEPARATOR = 'x';

export const spec = {
code: A4G_BIDDER_CODE,
isBidRequestValid: function(bid) {
return bid.params && !!bid.params.zoneId;
},

buildRequests: function(validBidRequests, bidderRequest) {
let deliveryUrl = '';
const idParams = [];
const sizeParams = [];
const zoneIds = [];

utils._each(validBidRequests, function(bid) {
if (!deliveryUrl && typeof bid.params.deliveryUrl === 'string') {
deliveryUrl = bid.params.deliveryUrl;
}
idParams.push(bid.bidId);
let bidSizes = (bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes) || bid.sizes;
sizeParams.push(bidSizes.map(size => size.join(SIZE_SEPARATOR)).join(ARRAY_SIZE_SEPARATOR));
zoneIds.push(bid.params.zoneId);
});

if (!deliveryUrl) {
deliveryUrl = A4G_DEFAULT_BID_URL;
}

let data = {
[IFRAME_PARAM_NAME]: 0,
[LOCATION_PARAM_NAME]: (bidderRequest.refererInfo && bidderRequest.refererInfo.referer) ? bidderRequest.refererInfo.referer : window.location.href,
[SIZE_PARAM_NAME]: sizeParams.join(ARRAY_PARAM_SEPARATOR),
[ID_PARAM_NAME]: idParams.join(ARRAY_PARAM_SEPARATOR),
[ZONE_ID_PARAM_NAME]: zoneIds.join(ARRAY_PARAM_SEPARATOR)
};

if (bidderRequest && bidderRequest.gdprConsent) {
data.gdpr = {
applies: bidderRequest.gdprConsent.gdprApplies,
consent: bidderRequest.gdprConsent.consentString
};
}

return {
method: 'GET',
url: deliveryUrl,
data: data
};
},

interpretResponse: function(serverResponses, request) {
const bidResponses = [];
utils._each(serverResponses.body, function(response) {
if (response.cpm > 0) {
const bidResponse = {
requestId: response.id,
creativeId: response.id,
adId: response.id,
cpm: response.cpm,
width: response.width,
height: response.height,
currency: A4G_CURRENCY,
netRevenue: true,
ttl: A4G_TTL,
ad: response.ad
};
bidResponses.push(bidResponse);
}
});
return bidResponses;
}
};

registerBidder(spec);
18 changes: 13 additions & 5 deletions modules/a4gBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ Maintainer: devops@ad4game.com

# Description

Ad4Game Bidder Adapter for Prebid.js. It should be tested on real domain. `localhost` should be rewritten (ex. example.com).
Ad4Game Bidder Adapter for Prebid.js. It should be tested on real domain. `localhost` should be rewritten (ex. example.com).

# Test Parameters
```
var adUnits = [
{
code: 'test-div',
sizes: [[300, 250]], // a display size
mediaTypes: {
banner: {
sizes: [[300, 250]], // a display size
}
},
bids: [
{
bidder: 'a4g',
params: {
zoneId: 59304,
deliveryUrl: 'http://dev01.ad4game.com/v1/bid'
deliveryUrl: 'https://dev01.ad4game.com/v1/bid'
}
}
]
},{
code: 'test-div',
sizes: [[300, 50]], // a mobile size
mediaTypes: {
banner: {
sizes: [[300, 50]], // a mobile size
}
},
bids: [
{
bidder: 'a4g',
params: {
zoneId: 59354,
deliveryUrl: 'http://dev01.ad4game.com/v1/bid'
deliveryUrl: 'https://dev01.ad4game.com/v1/bid'
}
}
]
Expand Down
173 changes: 173 additions & 0 deletions test/spec/modules/a4gBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { expect } from 'chai';
import { spec } from 'modules/a4gBidAdapter.js';

describe('a4gAdapterTests', function () {
describe('bidRequestValidity', function () {
it('bidRequest with zoneId and deliveryUrl params', function () {
expect(spec.isBidRequestValid({
bidder: 'a4g',
params: {
zoneId: 59304,
deliveryUrl: 'http://dev01.ad4game.com/v1/bid'
}
})).to.equal(true);
});

it('bidRequest with only zoneId', function () {
expect(spec.isBidRequestValid({
bidder: 'a4g',
params: {
zoneId: 59304
}
})).to.equal(true);
});

it('bidRequest with only deliveryUrl', function () {
expect(spec.isBidRequestValid({
bidder: 'a4g',
params: {
deliveryUrl: 'http://dev01.ad4game.com/v1/bid'
}
})).to.equal(false);
});
});

describe('bidRequest', function () {
const DEFAULT_OPTION = {
refererInfo: {
referer: 'https://www.prebid.org',
canonicalUrl: 'https://www.prebid.org/the/link/to/the/page'
}
};

const bidRequests = [{
'bidder': 'a4g',
'bidId': '51ef8751f9aead',
'params': {
'zoneId': 59304,
},
'adUnitCode': 'div-gpt-ad-1460505748561-0',
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'mediaTypes': {
'banner': {
'sizes': [[300, 50], [300, 250], [300, 600]]
}
},
'sizes': [[320, 50], [300, 250], [300, 600]],
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757'
}, {
'bidder': 'a4g',
'bidId': '51ef8751f9aead',
'params': {
'zoneId': 59354,
'deliveryUrl': '//dev01.ad4game.com/v1/bid'
},
'adUnitCode': 'div-gpt-ad-1460505748561-0',
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'mediaTypes': {
'banner': {
'sizes': [[300, 50], [300, 250], [300, 600]]
}
},
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757'
}];

it('bidRequest method', function () {
const request = spec.buildRequests(bidRequests, DEFAULT_OPTION);
expect(request.method).to.equal('GET');
});

it('bidRequest url', function () {
const request = spec.buildRequests(bidRequests, DEFAULT_OPTION);
expect(request.url).to.match(new RegExp(`${bidRequests[1].params.deliveryUrl}`));
});

it('bidRequest data', function () {
const request = spec.buildRequests(bidRequests, DEFAULT_OPTION);
expect(request.data).to.exist;
});

it('bidRequest zoneIds', function () {
const request = spec.buildRequests(bidRequests, DEFAULT_OPTION);
expect(request.data.zoneId).to.equal('59304;59354');
});

it('bidRequest gdpr consent', function () {
const consentString = 'consentString';
const bidderRequest = {
bidderCode: 'a4g',
auctionId: '18fd8b8b0bd757',
bidderRequestId: '418b37f85e772c',
timeout: 3000,
gdprConsent: {
consentString: consentString,
gdprApplies: true
},
refererInfo: {
referer: 'https://www.prebid.org',
canonicalUrl: 'https://www.prebid.org/the/link/to/the/page'
}
};

const request = spec.buildRequests(bidRequests, bidderRequest);

expect(request.data.gdpr).to.exist;
expect(request.data.gdpr.applies).to.exist.and.to.be.true;
expect(request.data.gdpr.consent).to.exist.and.to.equal(consentString);
});
});

describe('interpretResponse', function () {
const bidRequest = [{
'bidder': 'a4g',
'bidId': '51ef8751f9aead',
'params': {
'zoneId': 59304,
},
'adUnitCode': 'div-gpt-ad-1460505748561-0',
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'mediaTypes': {
'banner': {
'sizes': [[300, 50], [300, 250], [300, 600]]
}
},
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757'
}];

const bidResponse = {
body: [{
'id': 'div-gpt-ad-1460505748561-0',
'ad': 'test ad',
'width': 320,
'height': 250,
'cpm': 5.2
}],
headers: {}
};

it('required keys', function () {
const result = spec.interpretResponse(bidResponse, bidRequest);

let requiredKeys = [
'requestId',
'creativeId',
'adId',
'cpm',
'width',
'height',
'currency',
'netRevenue',
'ttl',
'ad'
];

let resultKeys = Object.keys(result[0]);
resultKeys.forEach(function(key) {
expect(requiredKeys.indexOf(key) !== -1).to.equal(true);
});
})
});
});