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

Populate adtypes array #1862

Merged
merged 4 commits into from
Nov 28, 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
44 changes: 28 additions & 16 deletions modules/appnexusAstBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Renderer } from 'src/Renderer';
import * as utils from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';
import { NATIVE, VIDEO } from 'src/mediaTypes';
import { BANNER, NATIVE, VIDEO } from 'src/mediaTypes';

const BIDDER_CODE = 'appnexusAst';
const URL = '//ib.adnxs.com/ut/v3/prebid';
const SUPPORTED_AD_TYPES = ['banner', 'video', 'native'];
const VIDEO_TARGETING = ['id', 'mimes', 'minduration', 'maxduration',
'startdelay', 'skippable', 'playback_method', 'frameworks'];
const USER_PARAMS = ['age', 'external_uid', 'segments', 'gender', 'dnt', 'language'];
Expand All @@ -28,7 +27,7 @@ const SOURCE = 'pbjs';

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [VIDEO, NATIVE],
supportedMediaTypes: [BANNER, VIDEO, NATIVE],

/**
* Determines whether or not the given bid request is valid.
Expand Down Expand Up @@ -100,7 +99,7 @@ export const spec = {
serverResponse.tags.forEach(serverBid => {
const rtbBid = getRtbBid(serverBid);
if (rtbBid) {
if (rtbBid.cpm !== 0 && SUPPORTED_AD_TYPES.includes(rtbBid.ad_type)) {
if (rtbBid.cpm !== 0 && this.supportedMediaTypes.includes(rtbBid.ad_type)) {
const bid = newBid(serverBid, rtbBid);
bid.mediaType = parseMediaType(rtbBid);
bids.push(bid);
Expand Down Expand Up @@ -206,9 +205,9 @@ function newBid(serverBid, rtbBid) {
bid.adResponse.ad = bid.adResponse.ads[0];
bid.adResponse.ad.video = bid.adResponse.ad.rtb.video;
}
} else if (rtbBid.rtb['native']) {
const nativeAd = rtbBid.rtb['native'];
bid['native'] = {
} else if (rtbBid.rtb[NATIVE]) {
const nativeAd = rtbBid.rtb[NATIVE];
bid[NATIVE] = {
title: nativeAd.title,
body: nativeAd.desc,
cta: nativeAd.ctatext,
Expand Down Expand Up @@ -241,6 +240,7 @@ function bidToTag(bid) {
const tag = {};
tag.sizes = transformSizes(bid.sizes);
tag.primary_size = tag.sizes[0];
tag.ad_types = [];
tag.uuid = bid.bidId;
if (bid.params.placementId) {
tag.id = parseInt(bid.params.placementId, 10);
Expand Down Expand Up @@ -279,19 +279,24 @@ function bidToTag(bid) {
tag.keywords = getKeywords(bid.params.keywords);
}

if (bid.mediaType === 'native' || utils.deepAccess(bid, 'mediaTypes.native')) {
tag.ad_types = ['native'];
if (bid.mediaType === NATIVE || utils.deepAccess(bid, 'mediaTypes.native')) {
tag.ad_types.push(NATIVE);

if (bid.nativeParams) {
const nativeRequest = buildNativeRequest(bid.nativeParams);
tag['native'] = {layouts: [nativeRequest]};
tag[NATIVE] = {layouts: [nativeRequest]};
}
}

const videoMediaType = utils.deepAccess(bid, 'mediaTypes.video');
const context = utils.deepAccess(bid, 'mediaTypes.video.context');

if (bid.mediaType === 'video' || (videoMediaType && context !== 'outstream')) {
if (bid.mediaType === VIDEO || videoMediaType) {
tag.ad_types.push(VIDEO);
}

// instream gets vastUrl, outstream gets vastXml
if (bid.mediaType === VIDEO || (videoMediaType && context !== 'outstream')) {
tag.require_asset_url = true;
}

Expand All @@ -303,6 +308,13 @@ function bidToTag(bid) {
.forEach(param => tag.video[param] = bid.params.video[param]);
}

if (
(utils.isEmpty(bid.mediaType) && utils.isEmpty(bid.mediaTypes)) ||
(bid.mediaType === BANNER || (bid.mediaTypes && bid.mediaTypes[BANNER]))
) {
tag.ad_types.push(BANNER);
}

return tag;
}

Expand Down Expand Up @@ -399,12 +411,12 @@ function handleOutstreamRendererEvents(bid, id, eventName) {

function parseMediaType(rtbBid) {
const adType = rtbBid.ad_type;
if (adType === 'video') {
return 'video';
} else if (adType === 'native') {
return 'native';
if (adType === VIDEO) {
return VIDEO;
} else if (adType === NATIVE) {
return NATIVE;
} else {
return 'banner';
return BANNER;
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/spec/modules/appnexusAstBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ describe('AppNexusAdapter', () => {
});
});

it('should populate the ad_types array on all requests', () => {
['banner', 'video', 'native'].forEach(type => {
const bidRequest = Object.assign({}, bidRequests[0]);
bidRequest.mediaTypes = {};
bidRequest.mediaTypes[type] = {};

const request = spec.buildRequests([bidRequest]);
const payload = JSON.parse(request.data);

expect(payload.tags[0].ad_types).to.deep.equal([type]);
});
});

it('should populate the ad_types array on outstream requests', () => {
const bidRequest = Object.assign({}, bidRequests[0]);
bidRequest.mediaTypes = {};
bidRequest.mediaTypes.video = {context: 'outstream'};

const request = spec.buildRequests([bidRequest]);
const payload = JSON.parse(request.data);

expect(payload.tags[0].ad_types).to.deep.equal(['video']);
});

it('sends bid request to ENDPOINT via POST', () => {
const request = spec.buildRequests(bidRequests);
expect(request.url).to.equal(ENDPOINT);
Expand Down