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

Kargo Analytics Adapter: Bid response time logging #8510

Merged
merged 21 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
94 changes: 88 additions & 6 deletions modules/kargoAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,96 @@
import { logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import CONSTANTS from '../src/constants.json';

var kargoAdapter = adapter({
analyticsType: 'endpoint',
url: 'https://krk.kargo.com/api/v1/event/prebid'
});
const EVENT_URL = 'https://krk.kargo.com/api/v1/event';
const KARGO_BIDDER_CODE = 'kargo';

const analyticsType = 'endpoint';

let _initOptions = {};

let _logBidResponseData = {
auctionId: '',
auctionTimeout: 0,
responseTime: 0,
};

let _bidResponseDataLogged = [];

var kargoAnalyticsAdapter = Object.assign(
adapter({ analyticsType }), {
track({ eventType, args }) {
switch (eventType) {
case CONSTANTS.EVENTS.AUCTION_INIT: {
_logBidResponseData.auctionTimeout = args.timeout;
break;
}
case CONSTANTS.EVENTS.BID_RESPONSE: {
handleBidResponseData(args);
break;
}
}
}
}
);

// handleBidResponseData: Get auction data for Kargo bids and send to server
function handleBidResponseData (bidResponse) {
// Verify this is Kargo and we haven't seen this auction data yet
if (bidResponse.bidder !== KARGO_BIDDER_CODE || _bidResponseDataLogged.includes(bidResponse.auctionId) !== false) {
jsadwith marked this conversation as resolved.
Show resolved Hide resolved
return
}

_logBidResponseData.auctionId = bidResponse.auctionId;
_logBidResponseData.responseTime = bidResponse.responseTimestamp - bidResponse.requestTimestamp;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think prebid core sets a param on this object called timeToRespond which is essentially this.

Also, you want to only track when your exchange does a VALID bid response? Not when it no-bids or errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use timeToResponse - thanks for pointing that out.

Confirmed - for now we want to just track valid bid responses.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this still is using response - request timestamps.

It is really up to you, just noting that there is timeToRespond available which is essentially the same thing.

sendAuctionData(_logBidResponseData);
}

// sendAuctionData: Send auction data to the server
function sendAuctionData (data) {
try {
_bidResponseDataLogged.push(data.auctionId);

if (!shouldFireEventRequest()) {
return;
}

ajax(
`${EVENT_URL}/auction-data`,
null,
{
aid: data.auctionId,
ato: data.auctionTimeout,
rt: data.responseTime,
it: 0,
},
{
method: 'GET',
}
);
} catch (err) {
logError('Error sending auction data: ', err);
}
}

// Sampling rate out of 100
function shouldFireEventRequest () {
const samplingRate = (_initOptions && _initOptions.sampling) || 100;
return ((Math.floor(Math.random() * 100) + 1) <= parseInt(samplingRate));
}

kargoAnalyticsAdapter.originEnableAnalytics = kargoAnalyticsAdapter.enableAnalytics;

kargoAnalyticsAdapter.enableAnalytics = function (config) {
_initOptions = config.options;
kargoAnalyticsAdapter.originEnableAnalytics(config);
};

adapterManager.registerAnalyticsAdapter({
adapter: kargoAdapter,
adapter: kargoAnalyticsAdapter,
code: 'kargo'
});

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

Module Name: Kargo Analytics Adapter
Module Type: Analytics Adapter
Maintainer: [email protected]

# Description

Analytics adapter for Kargo. Contact [email protected] for information.

# Usage

The simplest way to enable the analytics adapter is this

```javascript
pbjs.enableAnalytics([{
provider: 'kargo',
options: {
sampling: 100 // value out of 100
}
}]);
```

# Test Parameters

```
{
provider: 'kargo',
options: {
sampling: 100
}
}
```
43 changes: 43 additions & 0 deletions test/spec/modules/kargoAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import kargoAnalyticsAdapter from 'modules/kargoAnalyticsAdapter.js';
import { expect } from 'chai';
import { server } from 'test/mocks/xhr.js';
let events = require('src/events');
let constants = require('src/constants.json');

describe('Kargo Analytics Adapter', function () {
const adapterConfig = {
provider: 'kargoAnalytics',
};

after(function () {
kargoAnalyticsAdapter.disableAnalytics();
});

describe('main test flow', function () {
beforeEach(function () {
kargoAnalyticsAdapter.enableAnalytics(adapterConfig);
sinon.stub(events, 'getEvents').returns([]);
});

afterEach(function () {
events.getEvents.restore();
});

it('bid response data should send one request with auction ID, auction timeout, and response time', function() {
const bidResponse = {
bidder: 'kargo',
auctionId: '66529d4c-8998-47c2-ab3e-5b953490b98f',
requestTimestamp: 1652283459030,
responseTimestamp: 1652283459222,
};

events.emit(constants.EVENTS.AUCTION_INIT, {
timeout: 1000
});
events.emit(constants.EVENTS.BID_RESPONSE, bidResponse);

expect(server.requests.length).to.equal(1);
expect(server.requests[0].url).to.equal('https://krk.kargo.com/api/v1/event/auction-data?aid=66529d4c-8998-47c2-ab3e-5b953490b98f&ato=1000&rt=192&it=0');
});
});
});