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

AMX bid adapter: fix timeout handler, bump version #10744

Merged
merged 8 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
114 changes: 92 additions & 22 deletions modules/amxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,31 @@ import {
} from '../src/utils.js';
import { config } from '../src/config.js';
import { getStorageManager } from '../src/storageManager.js';
import { ajax } from '../src/ajax.js';

const BIDDER_CODE = 'amx';
const storage = getStorageManager({ bidderCode: BIDDER_CODE });
const SIMPLE_TLD_TEST = /\.com?\.\w{2,4}$/;
const DEFAULT_ENDPOINT = 'https://prebid.a-mo.net/a/c';
const VERSION = 'pba1.3.3';
const VERSION = 'pba1.3.4';
const VAST_RXP = /^\s*<\??(?:vast|xml)/i;
const TRACKING_ENDPOINT = 'https://1x1.a-mo.net/hbx/';
const TRACKING_BASE = 'https://1x1.a-mo.net/';
const TRACKING_ENDPOINT = TRACKING_BASE + 'hbx/';
const POST_TRACKING_ENDPOINT = TRACKING_BASE + 'e';
const AMUID_KEY = '__amuidpb';

function getLocation(request) {
return parseUrl(request.refererInfo?.topmostLocation || window.location.href);
}

function getTimeoutSize(timeoutData) {
if (timeoutData.sizes == null || timeoutData.sizes.length === 0) {
return [0, 0];
}

return timeoutData.sizes[0];
}

const largestSize = (sizes, mediaTypes) => {
const allSizes = sizes
.concat(deepAccess(mediaTypes, `${BANNER}.sizes`, []) || [])
Expand Down Expand Up @@ -149,7 +160,9 @@ function convertRequest(bid) {
const tid = deepAccess(bid, 'params.tagId');

const au =
bid.params != null && typeof bid.params.adUnitId === 'string' && bid.params.adUnitId !== ''
bid.params != null &&
typeof bid.params.adUnitId === 'string' &&
bid.params.adUnitId !== ''
? bid.params.adUnitId
: bid.adUnitCode;

Expand Down Expand Up @@ -202,7 +215,10 @@ function isSyncEnabled(syncConfigP, syncType) {
return false;
}

if (syncConfig.bidders === '*' || (isArray(syncConfig.bidders) && syncConfig.bidders.indexOf('amx') !== -1)) {
if (
syncConfig.bidders === '*' ||
(isArray(syncConfig.bidders) && syncConfig.bidders.indexOf('amx') !== -1)
) {
return syncConfig.filter == null || syncConfig.filter === 'include';
}

Expand All @@ -219,12 +235,17 @@ function getSyncSettings() {
d: 0,
l: 0,
t: 0,
e: true
e: true,
};
}

const settings = { d: syncConfig.syncDelay, l: syncConfig.syncsPerBidder, t: 0, e: syncConfig.syncEnabled }
const all = isSyncEnabled(syncConfig.filterSettings, 'all')
const settings = {
d: syncConfig.syncDelay,
l: syncConfig.syncsPerBidder,
t: 0,
e: syncConfig.syncEnabled,
};
const all = isSyncEnabled(syncConfig.filterSettings, 'all');

if (all) {
settings.t = SYNC_IMAGE & SYNC_IFRAME;
Expand Down Expand Up @@ -256,12 +277,14 @@ function getGpp(bidderRequest) {
return bidderRequest.gppConsent;
}

return bidderRequest?.ortb2?.regs?.gpp ?? { gppString: '', applicableSections: '' };
return (
bidderRequest?.ortb2?.regs?.gpp ?? { gppString: '', applicableSections: '' }
);
}

function buildReferrerInfo(bidderRequest) {
if (bidderRequest.refererInfo == null) {
return { r: '', t: false, c: '', l: 0, s: [] }
return { r: '', t: false, c: '', l: 0, s: [] };
}

const re = bidderRequest.refererInfo;
Expand All @@ -272,7 +295,7 @@ function buildReferrerInfo(bidderRequest) {
l: re.numIframes,
s: re.stack,
c: re.canonicalUrl,
}
};
}

const isTrue = (boolValue) =>
Expand Down Expand Up @@ -358,28 +381,35 @@ export const spec = {
return {
data: payload,
method: 'POST',
browsingTopics: true,
url: deepAccess(bidRequests[0], 'params.endpoint', DEFAULT_ENDPOINT),
withCredentials: true,
};
},

getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent, gppConsent) {
getUserSyncs(
syncOptions,
serverResponses,
gdprConsent,
uspConsent,
gppConsent
) {
const qp = {
gdpr_consent: enc(gdprConsent?.consentString || ''),
gdpr: enc(gdprConsent?.gdprApplies ? 1 : 0),
us_privacy: enc(uspConsent || ''),
gpp: enc(gppConsent?.gppString || ''),
gpp_sid: enc(gppConsent?.applicableSections || '')
gpp_sid: enc(gppConsent?.applicableSections || ''),
};

const iframeSync = {
url: `https://prebid.a-mo.net/isyn?${formatQS(qp)}`,
type: 'iframe'
type: 'iframe',
};

if (serverResponses == null || serverResponses.length === 0) {
if (syncOptions.iframeEnabled) {
return [iframeSync]
return [iframeSync];
}

return [];
Expand All @@ -394,7 +424,10 @@ export const spec = {
const pixelType =
syncPixel.indexOf('__st=iframe') !== -1 ? 'iframe' : 'image';
if (syncOptions.iframeEnabled || pixelType === 'image') {
hasFrame = hasFrame || (pixelType === 'iframe') || (syncPixel.indexOf('cchain') !== -1)
hasFrame =
hasFrame ||
pixelType === 'iframe' ||
syncPixel.indexOf('cchain') !== -1;
output.push({
url: syncPixel,
type: pixelType,
Expand All @@ -405,7 +438,7 @@ export const spec = {
});

if (!hasFrame && output.length < 2) {
output.push(iframeSync)
output.push(iframeSync);
}

return output;
Expand Down Expand Up @@ -470,19 +503,56 @@ export const spec = {
aud: targetingData.requestId,
a: targetingData.adUnitCode,
c2: nestedQs(targetingData.adserverTargeting),
cn3: targetingData.timeToRespond,
});
},

onTimeout(timeoutData) {
if (timeoutData == null) {
if (timeoutData == null || !timeoutData.length) {
return;
}

trackEvent('pbto', {
A: timeoutData.bidder,
bid: timeoutData.bidId,
a: timeoutData.adUnitCode,
cn: timeoutData.timeout,
let common = null;
const events = timeoutData.map((timeout) => {
const params = timeout.params || {};
const size = getTimeoutSize(timeout);
const { domain, page, ref } =
timeout.ortb2 != null && timeout.ortb2.site != null
? timeout.ortb2.site
: {};

if (common == null) {
common = {
do: domain,
u: page,
U: getUIDSafe(),
re: ref,
V: '$prebid.version$',
vg: '$$PREBID_GLOBAL$$',
};
}

return {
A: timeout.bidder,
mid: params.tagId,
a: params.adunitId || timeout.adUnitCode,
bid: timeout.bidId,
aud: timeout.transactionId,
w: size[0],
h: size[1],
cn: timeout.timeout,
cn2: timeout.bidderRequestsCount,
cn3: timeout.bidderWinsCount,
};
});

const payload = JSON.stringify({ c: common, e: events });
const url = POST_TRACKING_ENDPOINT + '/g_pbto';

ajax(url, null, payload, {
method: 'POST',
keepalive: true,
withCredentials: true,
});
},

Expand Down
5 changes: 5 additions & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export function toFetchRequest(url, data, options = {}) {
// but we're not in a secure context
rqOpts.browsingTopics = true;
}

if (options.keepalive) {
rqOpts.keepalive = true;
}

return dep.makeRequest(url, rqOpts);
}

Expand Down
1 change: 1 addition & 0 deletions test/mocks/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function mockFetchServer() {
}
}),
withCredentials: request.credentials === 'include',
keepalive: request.keepalive,
Copy link
Collaborator

@dgirardi dgirardi Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came to my attention only now and I'm sorry that I'm suggesting this so late, but:

  • this should be available under request.fetch.request.keepalive (see lines 42-46 in this file);
  • I think it'd be better for your keepalive request to use the fetch API directly (ajax.js exports it as fetch); since it's not a "normal" bid request, it doesn't need the XHR shim (which is there because the rest of core was written to use XHR), plus keepalive doesn't really make sense for "normal" bid requests so in my view it's a bonus if that remains unavailable to them.

setStatus(status) {
// nise replaces invalid status with 200
status = typeof status === 'number' ? status : 200;
Expand Down
Loading