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

Audience Network: correct TTL, allow platform override #2974

Merged
merged 1 commit into from
Aug 20, 2018
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
19 changes: 15 additions & 4 deletions modules/audienceNetworkBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @file AudienceNetwork adapter.
*/
import { registerBidder } from 'src/adapters/bidderFactory';
import { config } from 'src/config';
import { formatQS } from 'src/url';
import { generateUUID, getTopWindowUrl, isSafariBrowser, convertTypes } from 'src/utils';
import findIndex from 'core-js/library/fn/array/find-index';
Expand All @@ -15,9 +14,11 @@ const url = 'https://an.facebook.com/v2/placementbid.json';
const supportedMediaTypes = ['banner', 'video'];
const netRevenue = true;
const hbBidder = 'fan';
const ttl = 600;
const videoTtl = 3600;
const platver = '$prebid.version$';
const platform = '241394079772386';
const adapterver = '1.0.0';
const adapterver = '1.0.1';

/**
* Does this bid request contain valid parameters?
Expand Down Expand Up @@ -93,6 +94,13 @@ const isFullWidth = format => format === 'fullwidth';
*/
const sdkVersion = format => isVideo(format) ? '' : '5.5.web';

/**
* Which platform identifier should be used?
* @param {Array<String>} platforms Possible platform identifiers
* @returns {String} First valid platform found, or default if none found
*/
const findPlatform = platforms => [...platforms.filter(Boolean), platform][0];

/**
* Does the search part of the URL contain "anhb_testmode"
* and therefore indicate testmode should be used?
Expand Down Expand Up @@ -133,6 +141,7 @@ const getTopWindowUrlEncoded = () => encodeURIComponent(getTopWindowUrl());
* @param {String} bids[].placementCode - Prebid placement identifier
* @param {Object} bids[].params
* @param {String} bids[].params.placementId - Audience Network placement identifier
* @param {String} bids[].params.platform - Audience Network platform identifier (optional)
* @param {String} bids[].params.format - Optional format, one of 'video', 'native' or 'fullwidth' if set
* @param {Array} bids[].sizes - list of desired advert sizes
* @param {Array} bids[].sizes[] - Size arrays [h,w]: should include one of [300, 250], [320, 50]: first matched size is used
Expand All @@ -144,6 +153,7 @@ const buildRequests = bids => {
const adformats = [];
const sizes = [];
const sdk = [];
const platforms = [];
const requestIds = [];

bids.forEach(bid => bid.sizes
Expand All @@ -155,13 +165,15 @@ const buildRequests = bids => {
adformats.push(bid.params.format || size);
sizes.push(size);
sdk.push(sdkVersion(bid.params.format));
platforms.push(bid.params.platform);
requestIds.push(bid.bidId);
})
);

// Build URL
const testmode = isTestmode();
const pageurl = getTopWindowUrlEncoded();
const platform = findPlatform(platforms);
const search = {
placementids,
adformats,
Expand Down Expand Up @@ -194,8 +206,6 @@ const buildRequests = bids => {
* @returns {Array<Object>} A list of bid response objects
*/
const interpretResponse = ({ body }, { adformats, requestIds, sizes }) => {
const ttl = Number(config.getConfig().bidderTimeout);

const { bids = {} } = body;
return Object.keys(bids)
// extract Array of bid responses
Expand Down Expand Up @@ -237,6 +247,7 @@ const interpretResponse = ({ body }, { adformats, requestIds, sizes }) => {
const pageurl = getTopWindowUrlEncoded();
bidResponse.mediaType = 'video';
bidResponse.vastUrl = `https://an.facebook.com/v1/instream/vast.xml?placementid=${creativeId}&pageurl=${pageurl}&playerwidth=${width}&playerheight=${height}&bidid=${fbBidid}`;
bidResponse.ttl = videoTtl;
}
return bidResponse;
});
Expand Down
19 changes: 16 additions & 3 deletions test/spec/modules/audienceNetworkBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const placementId = 'test-placement-id';
const playerwidth = 320;
const playerheight = 180;
const requestId = 'test-request-id';
const debug = 'adapterver=1.0.0&platform=241394079772386&platver=$prebid.version$';
const debug = 'adapterver=1.0.1&platform=241394079772386&platver=$prebid.version$';
const pageUrl = encodeURIComponent(utils.getTopWindowUrl());

describe('AudienceNetwork adapter', () => {
Expand Down Expand Up @@ -182,13 +182,17 @@ describe('AudienceNetwork adapter', () => {
}]);
});

it('can build URL for fullwidth 300x250 unit', () => {
it('can build URL for fullwidth 300x250 unit, overriding platform', () => {
const platform = 'test-platform';
const debugPlatform = debug.replace('241394079772386', platform);

expect(buildRequests([{
bidder,
bidId: requestId,
sizes: [[300, 250]],
params: {
placementId,
platform,
format: 'fullwidth'
}
}])).to.deep.equal([{
Expand All @@ -197,7 +201,7 @@ describe('AudienceNetwork adapter', () => {
requestIds: [requestId],
sizes: ['300x250'],
url: 'https://an.facebook.com/v2/placementbid.json',
data: `placementids[]=test-placement-id&adformats[]=fullwidth&testmode=false&pageurl=${pageUrl}&sdk[]=5.5.web&${debug}`
data: `placementids[]=test-placement-id&adformats[]=fullwidth&testmode=false&pageurl=${pageUrl}&sdk[]=5.5.web&${debugPlatform}`
}]);
});

Expand Down Expand Up @@ -249,6 +253,7 @@ describe('AudienceNetwork adapter', () => {
.to.contain(`placementid:'${placementId}',format:'native',bidid:'test-bid-id'`, 'ad missing parameters')
.and.to.contain('getElementsByTagName("style")', 'ad missing native styles')
.and.to.contain('<div class="thirdPartyRoot"><a class="fbAdLink">', 'ad missing native container');
expect(bidResponse.ttl).to.equal(600);
expect(bidResponse.creativeId).to.equal(placementId);
expect(bidResponse.netRevenue).to.equal(true);
expect(bidResponse.currency).to.equal('USD');
Expand Down Expand Up @@ -287,6 +292,7 @@ describe('AudienceNetwork adapter', () => {
.to.contain(`placementid:'${placementId}',format:'300x250',bidid:'test-bid-id'`, 'ad missing parameters')
.and.not.to.contain('getElementsByTagName("style")', 'ad should not contain native styles')
.and.not.to.contain('<div class="thirdPartyRoot"><a class="fbAdLink">', 'ad should not contain native container');
expect(bidResponse.ttl).to.equal(600);
expect(bidResponse.creativeId).to.equal(placementId);
expect(bidResponse.netRevenue).to.equal(true);
expect(bidResponse.currency).to.equal('USD');
Expand Down Expand Up @@ -320,6 +326,7 @@ describe('AudienceNetwork adapter', () => {
expect(bidResponse.requestId).to.equal(requestId);
expect(bidResponse.width).to.equal(300);
expect(bidResponse.height).to.equal(250);
expect(bidResponse.ttl).to.equal(600);
expect(bidResponse.creativeId).to.equal(placementId);
expect(bidResponse.netRevenue).to.equal(true);
expect(bidResponse.currency).to.equal('USD');
Expand Down Expand Up @@ -364,6 +371,7 @@ describe('AudienceNetwork adapter', () => {
expect(bidResponseNative.width).to.equal(300);
expect(bidResponseNative.height).to.equal(250);
expect(bidResponseNative.ad).to.contain(`placementid:'${placementIdNative}',format:'native',bidid:'test-bid-id-native'`, 'ad missing parameters');
expect(bidResponseNative.ttl).to.equal(600);
expect(bidResponseNative.creativeId).to.equal(placementIdNative);
expect(bidResponseNative.netRevenue).to.equal(true);
expect(bidResponseNative.currency).to.equal('USD');
Expand All @@ -377,6 +385,7 @@ describe('AudienceNetwork adapter', () => {
expect(bidResponseIab.width).to.equal(300);
expect(bidResponseIab.height).to.equal(250);
expect(bidResponseIab.ad).to.contain(`placementid:'${placementIdIab}',format:'300x250',bidid:'test-bid-id-iab'`, 'ad missing parameters');
expect(bidResponseIab.ttl).to.equal(600);
expect(bidResponseIab.creativeId).to.equal(placementIdIab);
expect(bidResponseIab.netRevenue).to.equal(true);
expect(bidResponseIab.currency).to.equal('USD');
Expand Down Expand Up @@ -410,6 +419,7 @@ describe('AudienceNetwork adapter', () => {

expect(bidResponse.cpm).to.equal(1.23);
expect(bidResponse.requestId).to.equal(requestId);
expect(bidResponse.ttl).to.equal(3600);
expect(bidResponse.mediaType).to.equal('video');
expect(bidResponse.vastUrl).to.equal(`https://an.facebook.com/v1/instream/vast.xml?placementid=${placementId}&pageurl=${pageUrl}&playerwidth=${playerwidth}&playerheight=${playerheight}&bidid=${bidId}`);
expect(bidResponse.width).to.equal(playerwidth);
Expand Down Expand Up @@ -450,13 +460,15 @@ describe('AudienceNetwork adapter', () => {

expect(bidResponseVideo.cpm).to.equal(1.23);
expect(bidResponseVideo.requestId).to.equal(requestId);
expect(bidResponseVideo.ttl).to.equal(3600);
expect(bidResponseVideo.mediaType).to.equal('video');
expect(bidResponseVideo.vastUrl).to.equal(`https://an.facebook.com/v1/instream/vast.xml?placementid=${videoPlacementId}&pageurl=${pageUrl}&playerwidth=${playerwidth}&playerheight=${playerheight}&bidid=${videoBidId}`);
expect(bidResponseVideo.width).to.equal(playerwidth);
expect(bidResponseVideo.height).to.equal(playerheight);

expect(bidResponseNative.cpm).to.equal(4.56);
expect(bidResponseNative.requestId).to.equal(requestId);
expect(bidResponseNative.ttl).to.equal(600);
expect(bidResponseNative.width).to.equal(300);
expect(bidResponseNative.height).to.equal(250);
expect(bidResponseNative.ad).to.contain(`placementid:'${nativePlacementId}',format:'native',bidid:'${nativeBidId}'`);
Expand Down Expand Up @@ -490,6 +502,7 @@ describe('AudienceNetwork adapter', () => {
.to.contain(`placementid:'${placementId}',format:'native',bidid:'test-bid-id'`, 'ad missing parameters')
.and.to.contain('getElementsByTagName("style")', 'ad missing native styles')
.and.to.contain('<div class="thirdPartyRoot"><a class="fbAdLink">', 'ad missing native container');
expect(bidResponse.ttl).to.equal(600);
expect(bidResponse.creativeId).to.equal(placementId);
expect(bidResponse.netRevenue).to.equal(true);
expect(bidResponse.currency).to.equal('USD');
Expand Down