Skip to content

Commit

Permalink
Audience Network: ensure string and array based sizes are handled (#1873
Browse files Browse the repository at this point in the history
)

Also ensures pageurl encoding is consistently applied
  • Loading branch information
lovell authored and Matt Kendall committed Nov 27, 2017
1 parent 440fa77 commit 503c47b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions modules/audienceNetworkBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const isBidRequestValid = bid =>
const flattenSize = size =>
(Array.isArray(size) && size.length === 2) ? `${size[0]}x${size[1]}` : size;

/**
* Expands a 'WxH' string as a 2-element [W, H] array
* @param {String} size
* @returns {Array}
*/
const expandSize = size => size.split('x').map(Number);

/**
* Is this a valid slot size?
* @param {String} size
Expand Down Expand Up @@ -84,6 +91,12 @@ const createAdHtml = (placementId, format, bidId) => {
${nativeContainer}</div></body></html>`;
};

/**
* Get the current window location URL correctly encoded for use in a URL query string.
* @returns {String} URI-encoded URL
*/
const getTopWindowUrlEncoded = () => encodeURIComponent(getTopWindowUrl());

/**
* Convert each bid request to a single URL to fetch those bids.
* @param {Array} bids - list of bids
Expand Down Expand Up @@ -118,7 +131,7 @@ const buildRequests = bids => {

// Build URL
const testmode = isTestmode();
const pageurl = getTopWindowUrl();
const pageurl = getTopWindowUrlEncoded();
const search = {
placementids,
adformats,
Expand Down Expand Up @@ -163,7 +176,7 @@ const interpretResponse = ({ body }, { adformats, requestIds, sizes }) => {
} = bid;

const format = adformats[i];
const [width, height] = sizes[i];
const [width, height] = expandSize(flattenSize(sizes[i]));
const ad = createAdHtml(creativeId, format, fb_bidid);
const requestId = requestIds[i];

Expand All @@ -186,9 +199,9 @@ const interpretResponse = ({ body }, { adformats, requestIds, sizes }) => {
};
// Video attributes
if (isVideo(format)) {
const pageurl = getTopWindowUrl();
const pageurl = getTopWindowUrlEncoded();
bidResponse.mediaType = 'video';
bidResponse.vastUrl = `https://an.facebook.com/v1/instream/vast.xml?placementid=${creativeId}&pageurl=${encodeURIComponent(pageurl)}&playerwidth=${width}&playerheight=${height}&bidid=${fb_bidid}`;
bidResponse.vastUrl = `https://an.facebook.com/v1/instream/vast.xml?placementid=${creativeId}&pageurl=${pageurl}&playerwidth=${width}&playerheight=${height}&bidid=${fb_bidid}`;
}
return bidResponse;
});
Expand Down
2 changes: 1 addition & 1 deletion test/spec/modules/audienceNetworkBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe('AudienceNetwork adapter', () => {
}, {
adformats: ['native', '300x250'],
requestIds: [requestId, requestId],
sizes: [[300, 250], [300, 250]]
sizes: ['300x250', [300, 250]]
});

expect(bidResponseNative.cpm).to.equal(1.23);
Expand Down

0 comments on commit 503c47b

Please sign in to comment.