Skip to content

Commit

Permalink
pubxaiAnalyticsAdapter : collect rejected and nobid cases' data in a …
Browse files Browse the repository at this point in the history
…better way. (#12263)

* send BidRejected Events to capture floored bids

* fix tests

* send pubx_id as query param

* added extraData in analytics adapter to be sent in beacon data

* added extraData in analytics adapter to be sent in beacon data

* moved data read to session storage

* bumped version

* moving all data to localStorage again

* updated test cases for pubxaiAA.js

* fixing the missing logging of invalid bids

---------

Co-authored-by: tej656 <[email protected]>
Co-authored-by: Tej <[email protected]>
Co-authored-by: NikhilX <[email protected]>
Co-authored-by: Nathan Oliver <[email protected]>
  • Loading branch information
5 people authored Sep 23, 2024
1 parent 1536afc commit 0804978
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/pubxaiAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const track = ({ eventType, args }) => {
timestamp: args.timestamp,
});
if (
auctionCache[args.auctionId].bids.every((bid) => bid.bidType === 3)
auctionCache[args.auctionId].bids.every((bid) => [1, 3].includes(bid.bidType))
) {
prepareSend(args.auctionId);
}
Expand Down
107 changes: 103 additions & 4 deletions test/spec/modules/pubxaiAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const readBlobSafariCompat = (blob) => {

describe('pubxai analytics adapter', () => {
beforeEach(() => {
getGlobal().refreshUserIds()
sinon.stub(events, 'getEvents').returns([]);
getGlobal().refreshUserIds?.()
});

afterEach(() => {
Expand Down Expand Up @@ -776,15 +776,66 @@ describe('pubxai analytics adapter', () => {
}
});

it('auction with no bids', async () => {
it('auction data with only rejected bids', async () => {
// Step 1: Send auction init event
events.emit(EVENTS.AUCTION_INIT, prebidEvent['auctionInit']);

// Step 2: Send bid requested event
events.emit(EVENTS.BID_REQUESTED, prebidEvent['bidRequested']);

// Step 3: Send bid time out event
events.emit(EVENTS.BID_TIMEOUT, prebidEvent['bidTimeout']);
// Step 3: Send bid rejected (afaict the only expected reason would be a bid being too low)
events.emit(EVENTS.BID_REJECTED, prebidEvent['bidResponse']);

// Simulate "navigate away" behaviour
document.dispatchEvent(new Event('visibilitychange'));

// Step 4: check the number of calls made to pubx.ai
expect(navigator.sendBeacon.callCount).to.equal(0);

// Step 5: Send auction end event
events.emit(EVENTS.AUCTION_END, prebidEvent['auctionEnd']);

// Simulate end of session
document.dispatchEvent(new Event('visibilitychange'));

// Step 6: check the number of calls made to pubx.ai
expect(navigator.sendBeacon.callCount).to.equal(1);

// Step 7: check the pathname of the calls is correct (sent only to the auction endpoint)
const [expectedUrl, expectedData] = navigator.sendBeacon.args[0];
const parsedUrl = new URL(expectedUrl);
expect(parsedUrl.pathname).to.equal('/analytics/auction');

// Step 8: check that the meta information in the call is correct
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.1.0',
prebidVersion: '$prebid.version$',
pubxId: pubxId,
});

// Step 9: check that the data sent in the request is correct
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
{
...expectedAfterBid,
bids: [{
...expectedAfterBid.bids[0],
bidType: 1
}]
}
]);
});

it('auction data with only timed out bids', async () => {
// Step 1: Send auction init event
events.emit(EVENTS.AUCTION_INIT, prebidEvent['auctionInit']);

// Step 2: Send bid requested event
events.emit(EVENTS.BID_REQUESTED, prebidEvent['bidRequested']);

// Step 3: Send bid rejected (afaict the only expected reason would be a bid being too low)
events.emit(EVENTS.BID_TIMEOUT, [prebidEvent['bidResponse']]);

// Simulate "navigate away" behaviour
document.dispatchEvent(new Event('visibilitychange'));
Expand Down Expand Up @@ -816,6 +867,54 @@ describe('pubxai analytics adapter', () => {

// Step 9: check that the data sent in the request is correct
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
{
...expectedAfterBid,
bids: [{
...expectedAfterBid.bids[0],
bidType: 3
}]
}
]);
});

it('auction with no bids', async () => {
// Step 1: Send auction init event
events.emit(EVENTS.AUCTION_INIT, prebidEvent['auctionInit']);

// Step 2: Send bid requested event
events.emit(EVENTS.BID_REQUESTED, prebidEvent['bidRequested']);

// Simulate "navigate away" behaviour
document.dispatchEvent(new Event('visibilitychange'));

// Step 3: check the number of calls made to pubx.ai
expect(navigator.sendBeacon.callCount).to.equal(0);

// Step 4: Send auction end event
events.emit(EVENTS.AUCTION_END, prebidEvent['auctionEnd']);

// Simulate end of session
document.dispatchEvent(new Event('visibilitychange'));

// Step 5: check the number of calls made to pubx.ai
expect(navigator.sendBeacon.callCount).to.equal(1);

// Step 6: check the pathname of the calls is correct (sent only to the auction endpoint)
const [expectedUrl, expectedData] = navigator.sendBeacon.args[0];
const parsedUrl = new URL(expectedUrl);
expect(parsedUrl.pathname).to.equal('/analytics/auction');

// Step 7: check that the meta information in the call is correct
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.1.0',
prebidVersion: '$prebid.version$',
pubxId: pubxId,
});

// Step 8: check that the data sent in the request is correct
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
{
...expectedAfterBid,
Expand Down

0 comments on commit 0804978

Please sign in to comment.