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

pubxaiAnalyticsAdapter : collect rejected and nobid cases' data in a better way. #12263

Merged
merged 19 commits into from
Sep 23, 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
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