Skip to content

Commit

Permalink
Kargo Analytics Adapter: Bid response time logging (#8510)
Browse files Browse the repository at this point in the history
* Kargo Bid Adapter: Use currency from Bid Response

* Kargo Bid Adapter: Fix failed test

* Kargo Bid Adapter: adding media type to bid response, supporting vastXml response (#8426)

* kargo adapter - adding mediaType to bid response, conditionally set vastXml field

* kargo adapter - updating tests

* Kargo Bid Adapter: onTimeout Support (#6)

* Adding additional param

* Adding response time function

* Remove debug

* Updating response time log to be set by bid response

* Adding screen width/height to request

* Test fix

* Test fix

* Removing interpretResponse signaling

* Simplifying send data function

* Kargo Analytics Adapter: Update with bid response time support

* Renaming event route for auction data

* Reset bid response data sent bool

* Using array to store logged auctions

* Update to use timeToResponse

* Test fix

Co-authored-by: Wei Wong <wwong@kargo.com>
Co-authored-by: Andy Rusiecki <andy.rusiecki@gmail.com>
  • Loading branch information
3 people authored Jul 7, 2022
1 parent 7d3d67b commit 932f581
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 6 deletions.
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) {
return
}

_logBidResponseData.auctionId = bidResponse.auctionId;
_logBidResponseData.responseTime = bidResponse.timeToRespond;
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: support@kargo.com

# Description

Analytics adapter for Kargo. Contact support@kargo.com 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
}
}
```
42 changes: 42 additions & 0 deletions test/spec/modules/kargoAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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',
timeToRespond: 192,
};

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');
});
});
});

0 comments on commit 932f581

Please sign in to comment.