Skip to content

Commit

Permalink
AppNexus debug auction via cookie (prebid#3314)
Browse files Browse the repository at this point in the history
* updating appnexus debug auction to be cookie-only

* adding getCookie to utils

* remove LGTM alert on getCookie

* fix failed tests
  • Loading branch information
aneuway2 authored and Loic Talon committed Dec 19, 2018
1 parent 37d4ccd commit fd2420c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
52 changes: 52 additions & 0 deletions modules/appnexusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const VIDEO_TARGETING = ['id', 'mimes', 'minduration', 'maxduration',
'startdelay', 'skippable', 'playback_method', 'frameworks'];
const USER_PARAMS = ['age', 'external_uid', 'segments', 'gender', 'dnt', 'language'];
const APP_DEVICE_PARAMS = ['geo', 'device_id']; // appid is collected separately
const DEBUG_PARAMS = ['enabled', 'dongle', 'member_id', 'debug_timeout'];
const NATIVE_MAPPING = {
body: 'description',
body2: 'desc2',
Expand Down Expand Up @@ -79,6 +80,32 @@ export const spec = {
};
}

let debugObj = {};
let debugObjParams = {};
const debugCookieName = 'apn_prebid_debug';
const debugCookie = utils.getCookie(debugCookieName) || null;

if (debugCookie) {
try {
debugObj = JSON.parse(debugCookie);
} catch (e) {
utils.logError('AppNexus Debug Auction Cookie Error:\n\n' + e);
}
} else {
const debugBidRequest = find(bidRequests, hasDebug);
if (debugBidRequest && debugBidRequest.debug) {
debugObj = debugBidRequest.debug;
}
}

if (debugObj && debugObj.enabled) {
Object.keys(debugObj)
.filter(param => includes(DEBUG_PARAMS, param))
.forEach(param => {
debugObjParams[param] = debugObj[param];
});
}

const memberIdBid = find(bidRequests, hasMemberId);
const member = memberIdBid ? parseInt(memberIdBid.params.member, 10) : 0;

Expand All @@ -101,6 +128,11 @@ export const spec = {
payload.app = appIdObj;
}

if (debugObjParams.enabled) {
payload.debug = debugObjParams;
utils.logInfo('AppNexus Debug Auction Settings:\n\n' + JSON.stringify(debugObjParams, null, 4));
}

if (bidderRequest && bidderRequest.gdprConsent) {
// note - objects for impbus use underscore instead of camelCase
payload.gdpr_consent = {
Expand Down Expand Up @@ -156,6 +188,22 @@ export const spec = {
}
});
}

if (serverResponse.debug && serverResponse.debug.debug_info) {
let debugHeader = 'AppNexus Debug Auction for Prebid\n\n'
let debugText = debugHeader + serverResponse.debug.debug_info
debugText = debugText
.replace(/(<td>|<th>)/gm, '\t') // Tables
.replace(/(<\/td>|<\/th>)/gm, '\n') // Tables
.replace(/^<br>/gm, '') // Remove leading <br>
.replace(/(<br>\n|<br>)/gm, '\n') // <br>
.replace(/<h1>(.*)<\/h1>/gm, '\n\n===== $1 =====\n\n') // Header H1
.replace(/<h[2-6]>(.*)<\/h[2-6]>/gm, '\n\n*** $1 ***\n\n') // Headers
.replace(/(<([^>]+)>)/igm, ''); // Remove any other tags
utils.logMessage('https://console.appnexus.com/docs/understanding-the-debug-auction');
utils.logMessage(debugText);
}

return bids;
},

Expand Down Expand Up @@ -458,6 +506,10 @@ function hasAppId(bid) {
return !!bid.params.app
}

function hasDebug(bid) {
return !!bid.debug
}

function getRtbBid(tag) {
return tag && tag.ads && tag.ads.length && find(tag.ads, ad => ad.rtb);
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ export function cookiesAreEnabled() {
return window.document.cookie.indexOf('prebid.cookieTest') != -1;
}

export function getCookie(name) {
let m = window.document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]*)\\s*(;|$)');
return m ? decodeURIComponent(m[2]) : null;
}

/**
* Given a function, return a function which only executes the original after
* it's been called numRequiredCalls times.
Expand Down

0 comments on commit fd2420c

Please sign in to comment.