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

Lucead Bid Adapter: Add new adapter #11068

Merged
merged 4 commits into from
Feb 14, 2024
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
147 changes: 147 additions & 0 deletions modules/luceadBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import {ortbConverter} from '../libraries/ortbConverter/converter.js';
import {loadExternalScript} from '../src/adloader.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getUniqueIdentifierStr, logInfo} from '../src/utils.js';
import {fetch} from '../src/ajax.js';

const bidderCode = 'lucead';
let baseUrl = 'https://ayads.io';
let staticUrl = 'https://s.ayads.io';
let companionUrl = 'https://cdn.jsdelivr.net/gh/lucead/prebid-js-external-js-lucead@master/dist/prod.min.js';
let endpointUrl = 'https://prebid.ayads.io/go';
const defaultCurrency = 'EUR';
const defaultTtl = 500;
const isDevEnv = location.hostname.endsWith('.ngrok-free.app');

function isBidRequestValid(bidRequest) {
return !!bidRequest?.params?.placementId;
}

export function log(msg, obj) {
logInfo('Lucead - ' + msg, obj);
}

function buildRequests(validBidRequests, bidderRequest) {
if (isDevEnv) {
baseUrl = `https://${location.hostname}`;
staticUrl = baseUrl;
companionUrl = `${staticUrl}/dist/prebid-companion.js`;
endpointUrl = `${baseUrl}/go`;
}

log('buildRequests', {
validBidRequests,
bidderRequest,
});

const companionData = {
base_url: baseUrl,
static_url: staticUrl,
endpoint_url: endpointUrl,
request_id: bidderRequest.bidderRequestId,
validBidRequests,
bidderRequest,
getUniqueIdentifierStr,
ortbConverter,
};

loadExternalScript(companionUrl, bidderCode, () => window.ayads_prebid && window.ayads_prebid(companionData));

return validBidRequests.map(bidRequest => ({
method: 'POST',
url: `${endpointUrl}/prebid/sub`,
data: JSON.stringify({
request_id: bidderRequest.bidderRequestId,
domain: location.hostname,
bid_id: bidRequest.bidId,
sizes: bidRequest.sizes,
media_types: bidRequest.mediaTypes,
fledge_enabled: bidderRequest.fledgeEnabled,
enable_contextual: bidRequest?.params?.enableContextual !== false,
enable_pa: bidRequest?.params?.enablePA !== false,
params: bidRequest.params,
}),
options: {
contentType: 'text/plain',
withCredentials: false
},
}));
}

function interpretResponse(serverResponse, bidRequest) {
// @see required fields https://docs.prebid.org/dev-docs/bidder-adaptor.html
const response = serverResponse.body;
const bidRequestData = JSON.parse(bidRequest.data);

const bids = response.enable_contextual !== false ? [{
requestId: response?.bid_id || '1', // bid request id, the bid id
cpm: response?.cpm || 0,
width: (response?.size && response?.size?.width) || 300,
height: (response?.size && response?.size?.height) || 250,
currency: response?.currency || defaultCurrency,
ttl: response?.ttl || defaultTtl,
creativeId: response?.ad_id || '0',
netRevenue: response?.netRevenue || true,
ad: response?.ad || '',
meta: {
advertiserDomains: response?.advertiserDomains || [],
},
}] : null;

log('interpretResponse', {serverResponse, bidRequest, bidRequestData, bids});

if (response.enable_pa === false) { return bids; }

const fledgeAuctionConfig = {
seller: baseUrl,
decisionLogicUrl: `${baseUrl}/js/ssp.js`,
interestGroupBuyers: [baseUrl],
perBuyerSignals: {},
auctionSignals: {
size: bidRequestData.sizes ? {width: bidRequestData?.sizes[0][0] || 300, height: bidRequestData?.sizes[0][1] || 250} : null,
},
};

const fledgeAuctionConfigs = [{bidId: response.bid_id, config: fledgeAuctionConfig}];

return {bids, fledgeAuctionConfigs};
}

function report(type = 'impression', data = {}) {
// noinspection JSCheckFunctionSignatures
return fetch(`${endpointUrl}/report/${type}`, {
body: JSON.stringify(data),
method: 'POST',
contentType: 'text/plain'
});
}

function onBidWon(bid) {
log('Bid won', bid);

return report(`impression`, {
bid_id: bid?.bidId,
ad_id: bid?.creativeId,
placement_id: bid?.params ? bid?.params[0]?.placementId : 0,
spent: bid?.cpm,
currency: bid?.currency,
});
}

function onTimeout(timeoutData) {
log('Timeout from adapter', timeoutData);
}

export const spec = {
code: bidderCode,
// gvlid: BIDDER_GVLID,
aliases: [],
isBidRequestValid,
buildRequests,
interpretResponse,
onBidWon,
onTimeout,
};

// noinspection JSCheckFunctionSignatures
registerBidder(spec);
27 changes: 27 additions & 0 deletions modules/luceadBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Overview

Module Name: Lucead Bidder Adapter
Module Type: Bidder Adapter
Maintainer: prebid@lucead.com

# Description

Module that connects to Lucead demand source to fetch bids.

# Test Parameters
```
const adUnits = [
{
code: 'test-div',
sizes: [[300, 250]],
bids: [
{
bidder: "lucead",
params: {
placementId: '1',
}
}
]
}
];
```
3 changes: 2 additions & 1 deletion src/adloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const _approvedLoadExternalJSList = [
'qortex',
'dynamicAdBoost',
'contxtful',
'id5'
'id5',
'lucead',
]

/**
Expand Down
165 changes: 165 additions & 0 deletions test/spec/modules/luceadBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable prebid/validate-imports,no-undef */
import { expect } from 'chai';
import { spec } from 'modules/luceadBidAdapter.js';
import sinon from 'sinon';
import { newBidder } from 'src/adapters/bidderFactory.js';
import {deepClone} from 'src/utils.js';
import * as ajax from 'src/ajax.js';

describe('Lucead Adapter', () => {
describe('inherited functions', function () {
it('exists and is a function', function () {
// noinspection JSCheckFunctionSignatures
const adapter = newBidder(spec);
expect(adapter.callBids).to.exist.and.to.be.a('function');
});
});

describe('isBidRequestValid', function () {
let bid;
beforeEach(function () {
bid = {
bidder: 'lucead',
params: {
placementId: '1',
},
};
});

it('should return true when required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});
});

describe('onBidWon', function () {
let sandbox;
const bid = { foo: 'bar' };

beforeEach(function () {
sandbox = sinon.sandbox.create();
});

it('should trigger impression pixel', function () {
sandbox.spy(ajax, 'fetch');
spec.onBidWon(bid);
expect(ajax.fetch.args[0][0]).to.match(/report\/impression$/);
});

afterEach(function () {
sandbox.restore();
});
});

describe('buildRequests', function () {
const bidRequests = [
{
bidder: 'lucead',
adUnitCode: 'lucead_code',
bidId: 'abc1234',
sizes: [[1800, 1000], [640, 300]],
requestId: 'xyz654',
params: {
placementId: '123',
}
}
];

const bidderRequest = {
bidderRequestId: '13aaa3df18bfe4',
bids: {}
};

it('should have a post method', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
expect(request[0].method).to.equal('POST');
});

it('should contains a request id equals to the bid id', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
expect(JSON.parse(request[0].data).bid_id).to.equal(bidRequests[0].bidId);
});

it('should have an url that contains sub keyword', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
expect(request[0].url).to.match(/sub/);
});
});

describe('interpretResponse', function () {
const serverResponse = {
body: {
'bid_id': '2daf899fbe4c52',
'request_id': '13aaa3df18bfe4',
'ad': 'Ad',
'ad_id': '3890677904',
'cpm': 3.02,
'currency': 'USD',
'time': 1707257712095,
'size': {'width': 300, 'height': 250},
}
};

const bidRequest = {data: JSON.stringify({
'request_id': '13aaa3df18bfe4',
'domain': '7cdb-2a02-8429-e4a0-1701-bc69-d51c-86e-b279.ngrok-free.app',
'bid_id': '2daf899fbe4c52',
'sizes': [[300, 250]],
'media_types': {'banner': {'sizes': [[300, 250]]}},
'fledge_enabled': true,
'enable_contextual': true,
'enable_pa': true,
'params': {'placementId': '1'},
})};

it('should get correct bid response', function () {
const result = spec.interpretResponse(serverResponse, bidRequest);

expect(Object.keys(result.bids[0])).to.have.members([
'requestId',
'cpm',
'width',
'height',
'currency',
'ttl',
'creativeId',
'netRevenue',
'ad',
'meta',
]);
});

it('should return bid empty response', function () {
const serverResponse = {body: {cpm: 0}};
const bidRequest = {data: '{}'};
const result = spec.interpretResponse(serverResponse, bidRequest);
expect(result.bids[0].ad).to.be.equal('');
expect(result.bids[0].cpm).to.be.equal(0);
});

it('should add advertiserDomains', function () {
const bidRequest = {data: JSON.stringify({
bidder: 'lucead',
params: {
placementId: '1',
}
})};

const result = spec.interpretResponse(serverResponse, bidRequest);
expect(Object.keys(result.bids[0].meta)).to.include.members(['advertiserDomains']);
});

it('should support disabled contextual bids', function () {
const serverResponseWithDisabledContectual = deepClone(serverResponse);
serverResponseWithDisabledContectual.body.enable_contextual = false;
const result = spec.interpretResponse(serverResponseWithDisabledContectual, bidRequest);
expect(result.bids).to.be.null;
});

it('should support disabled Protected Audience', function () {
const serverResponseWithEnablePaFalse = deepClone(serverResponse);
serverResponseWithEnablePaFalse.body.enable_pa = false;
const result = spec.interpretResponse(serverResponseWithEnablePaFalse, bidRequest);
expect(result.fledgeAuctionConfigs).to.be.undefined;
});
});
});