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 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
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.timeToRespond;
Copy link
Collaborator

Choose a reason for hiding this comment

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

so having this object at module scope we could possibly see issues if two auctions are running at same time.

The right thing would prob be to just create a local object based off your wanted params here.

Even the auctionTimeout part may change between auctions.

Now it is probably fine majority of the time. So I can be fine with leaving it if you want.

Let me know.

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