Skip to content

AdPlus Analytics Adapter : initial release #13493

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

Merged
merged 3 commits into from
Jul 29, 2025
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
154 changes: 154 additions & 0 deletions modules/adplusAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { logInfo, logError } from '../src/utils.js';
import { EVENTS } from '../src/constants.js';
import { ajax } from '../src/ajax.js';

const { AUCTION_END, BID_WON } = EVENTS;
const ANALYTICS_CODE = 'adplus';
const SERVER_URL = 'https://ssp-dev.ad-plus.com.tr/server/analytics/bids';
const SEND_DELAY_MS = 200;
const MAX_RETRIES = 3;

let auctionBids = {};
let sendQueue = [];
let isSending = false;

const adplusAnalyticsAdapter = Object.assign(adapter({ SERVER_URL, analyticsType: 'endpoint' }), {
track({ eventType, args }) {
try {
switch (eventType) {
case AUCTION_END:
auctionBids[args.auctionId] = auctionBids[args.auctionId] || {};
(args.bidsReceived || []).forEach(bid => {
const adUnit = bid.adUnitCode;
auctionBids[args.auctionId][adUnit] = auctionBids[args.auctionId][adUnit] || [];
auctionBids[args.auctionId][adUnit].push({
type: 'bid',
bidder: bid.bidderCode,
auctionId: bid.auctionId,
adUnitCode: bid.adUnitCode,
cpm: bid.cpm,
currency: bid.currency,
size: bid.size,
width: bid.width,
height: bid.height,
creativeId: bid.creativeId,
timeToRespond: bid.timeToRespond,
netRevenue: bid.netRevenue,
dealId: bid.dealId || null,
});
});
break;

case BID_WON:
const bid = args;
const adUnitBids = (auctionBids[bid.auctionId] || {})[bid.adUnitCode];
if (!adUnitBids) {
logInfo(`[adplusAnalyticsAdapter] No bid data for auction ${bid.auctionId}, ad unit ${bid.adUnitCode}`);
return;
}

const winningBidData = {
type: BID_WON,
bidder: bid.bidderCode,
auctionId: bid.auctionId,
adUnitCode: bid.adUnitCode,
cpm: bid.cpm,
currency: bid.currency,
size: bid.size,
width: bid.width,
height: bid.height,
creativeId: bid.creativeId,
timeToRespond: bid.timeToRespond,
netRevenue: bid.netRevenue,
dealId: bid.dealId || null,
};

const payload = {
auctionId: bid.auctionId,
adUnitCode: bid.adUnitCode,
winningBid: winningBidData,
allBids: adUnitBids
};

sendQueue.push(payload);
if (!isSending) {
processQueue();
}
break;

default:
break;
}
} catch (err) {
logError(`[adplusAnalyticsAdapter] Error processing event ${eventType}`, err);
}
}
});

function processQueue() {
if (sendQueue.length === 0) {
isSending = false;
return;
}

isSending = true;
const nextPayload = sendQueue.shift();
sendWithRetries(nextPayload, 0);
}

function sendWithRetries(payload, attempt) {
const payloadStr = JSON.stringify(payload);

ajax(
SERVER_URL,
{
success: () => {
logInfo(`[adplusAnalyticsAdapter] Sent BID_WON payload (attempt ${attempt + 1})`);
setTimeout(() => {
processQueue();
}, SEND_DELAY_MS);
},
error: () => {
if (attempt < MAX_RETRIES - 1) {
logError(`[adplusAnalyticsAdapter] Send failed (attempt ${attempt + 1}), retrying...`);
setTimeout(() => {
sendWithRetries(payload, attempt + 1);
}, SEND_DELAY_MS);
} else {
logError(`[adplusAnalyticsAdapter] Failed to send after ${MAX_RETRIES} attempts`);
setTimeout(() => {
processQueue();
}, SEND_DELAY_MS);
}
}
},
payloadStr,
{
method: 'POST',
contentType: 'application/json',

Check warning

Code scanning / CodeQL

Application/json request type in bidder Warning

application/json request type triggers preflight requests and may increase bidder timeouts
},
);
}

adplusAnalyticsAdapter.originEnableAnalytics = adplusAnalyticsAdapter.enableAnalytics;

adplusAnalyticsAdapter.enableAnalytics = function (config) {
adplusAnalyticsAdapter.originEnableAnalytics(config);
logInfo('[adplusAnalyticsAdapter] Analytics enabled with config:', config);
};

adapterManager.registerAnalyticsAdapter({
adapter: adplusAnalyticsAdapter,
code: ANALYTICS_CODE
});

adplusAnalyticsAdapter.auctionBids = auctionBids;

adplusAnalyticsAdapter.reset = function () {
auctionBids = {};
adplusAnalyticsAdapter.auctionBids = auctionBids;
};

export default adplusAnalyticsAdapter;
23 changes: 23 additions & 0 deletions modules/adplusAnalyticsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Overview

Module Name: AdPlus Analytics Adapter

Module Type: Analytics Adapter

Maintainer: [email protected]

---

# Description

Analytics adapter for AdPlus platform. Contact [[email protected]]() if you have any questions about integration.

---

# Example Configuration

```javascript
pbjs.enableAnalytics({
provider: 'adplus',
});
```
176 changes: 176 additions & 0 deletions test/spec/modules/adplusAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import adplusAnalyticsAdapter from 'modules/adplusAnalyticsAdapter.js';
import { expect } from 'chai';
import adapterManager from 'src/adapterManager.js';
import { server } from 'test/mocks/xhr.js';
import { EVENTS } from 'src/constants.js';
import sinon from 'sinon';

let events = require('src/events');

describe('AdPlus analytics adapter', function () {
let sandbox, clock;

beforeEach(function () {
sandbox = sinon.createSandbox();
sandbox.spy(console, 'log');

clock = sandbox.useFakeTimers();
sandbox.stub(events, 'getEvents').returns([]);
adapterManager.enableAnalytics({ provider: 'adplus' });
});

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

const auctionId = 'test-auction-123';

const bidsReceived = [
{
bidderCode: 'adplus',
auctionId,
adUnitCode: 'adunit-1',
cpm: 5,
currency: 'USD',
size: '300x250',
width: 300,
height: 250,
creativeId: 'crea-1',
timeToRespond: 120,
netRevenue: true,
dealId: null
},
{
bidderCode: 'adplus',
auctionId,
adUnitCode: 'adunit-2',
cpm: 7,
currency: 'USD',
size: '728x90',
width: 728,
height: 90,
creativeId: 'crea-2',
timeToRespond: 110,
netRevenue: true,
dealId: 'deal123'
}
];

const bidWon1 = {
auctionId,
adUnitCode: 'adunit-1',
bidderCode: 'adplus',
cpm: 5,
currency: 'USD',
size: '300x250',
width: 300,
height: 250,
creativeId: 'crea-1',
timeToRespond: 120,
netRevenue: true,
dealId: null
};

const bidWon2 = {
auctionId,
adUnitCode: 'adunit-2',
bidderCode: 'adplus',
cpm: 7,
currency: 'USD',
size: '728x90',
width: 728,
height: 90,
creativeId: 'crea-2',
timeToRespond: 110,
netRevenue: true,
dealId: 'deal123'
};

it('should store bids on AUCTION_END and not send immediately', function () {
events.emit(EVENTS.AUCTION_END, {
auctionId,
bidsReceived
});

expect(server.requests.length).to.equal(0);

const storedData = adplusAnalyticsAdapter.auctionBids[auctionId];
expect(storedData).to.exist;
expect(Object.keys(storedData)).to.have.length(2);
expect(storedData['adunit-1'][0]).to.include({
auctionId,
adUnitCode: 'adunit-1',
bidder: 'adplus',
cpm: 5,
currency: 'USD'
});
});

it('should batch BID_WON events and send after delay with retries', function (done) {
// First, send AUCTION_END to prepare data
events.emit(EVENTS.AUCTION_END, { auctionId, bidsReceived });

// Emit first BID_WON - should send immediately
events.emit(EVENTS.BID_WON, bidWon1);

clock.tick(0);
expect(server.requests.length).to.equal(1);

// Fail first request, triggers retry after 200ms
server.requests[0].respond(500, {}, 'Internal Server Error');
clock.tick(200);

expect(server.requests.length).to.equal(2);

// Fail second (retry) request, triggers next retry
server.requests[1].respond(500, {}, 'Internal Server Error');
clock.tick(200);

expect(server.requests.length).to.equal(3);

// Succeed on third retry
server.requests[2].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' }));

// Now emit second BID_WON - queue is empty, should send immediately
events.emit(EVENTS.BID_WON, bidWon2);

// Should wait 200ms after emit.
expect(server.requests.length).to.equal(3);

// Sends the second BID_WON data after 200ms
clock.tick(200);
expect(server.requests.length).to.equal(4);

// Succeed second BID_WON send
server.requests[3].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' }));

// Validate payloads
const payload1 = JSON.parse(server.requests[0].requestBody);
const payload2 = JSON.parse(server.requests[3].requestBody);

expect(payload1.winningBid).to.include({
auctionId,
adUnitCode: 'adunit-1',
bidder: 'adplus',
cpm: 5,
});

expect(payload2.winningBid).to.include({
auctionId,
adUnitCode: 'adunit-2',
bidder: 'adplus',
cpm: 7,
});

done();
});

it('should skip BID_WON if no auction data available', function () {
// Emit BID_WON without AUCTION_END first
expect(() => events.emit(EVENTS.BID_WON, bidWon1)).to.not.throw();

// No ajax call since no auctionData
expect(server.requests.length).to.equal(0);
});
});