Skip to content

Commit

Permalink
KRAK-3663 - Analytics timeout support (#5)
Browse files Browse the repository at this point in the history
* Initial draft of timeout support

* Adding support for noBid flag

* Only send timeout data

* Working implementation and spec

* Cleanup

* README for analytics

* Stylistic updates

* Clean up test array
  • Loading branch information
jsadwith authored May 18, 2022
1 parent 0b79c6d commit 222a46c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 6 deletions.
68 changes: 62 additions & 6 deletions modules/kargoAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
import { _each, logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';

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 CONSTANTS = require('../src/constants.json');

const analyticsType = 'endpoint';

let _logData = {
auctionId: '',
auctionTimeout: 0,
};

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

function handleTimeout (timeouts) {
let sent = false;
_each(timeouts, timeout => {
if (sent || timeout.bidder !== KARGO_BIDDER_CODE) {
return
}

_logData.auctionId = timeout.auctionId;
sendTimeoutData(_logData);
sent = true;
});
}

function sendTimeoutData (data) {
try {
ajax(
`${EVENT_URL}/timeout`,
null,
{
aid: data.auctionId,
ato: data.auctionTimeout,
},
{
method: 'GET',
}
);
} catch (err) {
logError('Error sending timeout data: ', err);
}
}

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

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

Module Name: Kargo Analytics Adapter
Module Type: Analytics Adapter
Maintainer: support@kargo.com

# Description

Analytics adapter for Kargo meant to measure timeouts. Contact support@kargo.com for information.

# Usage

The simplest way to enable the analytics adapter is this

```javascript
pbjs.enableAnalytics([{
provider: 'kargo'
}]);
```

# Test Parameters

```
{
provider: 'kargo'
}
```
50 changes: 50 additions & 0 deletions test/spec/modules/kargoAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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',
options: {
bundleId: '',
account: 'kargo',
},
};

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 timeout should send one request with auction ID and timeout', function() {
const bidTimeout = [
{
bidder: 'kargo',
auctionId: '66529d4c-8998-47c2-ab3e-5b953490b98f'
},
{
bidder: 'kargo',
auctionId: '66529d4c-8998-47c2-ab3e-5b953490b98f'
}
];
events.emit(constants.EVENTS.AUCTION_INIT, {
timeout: 100
});
events.emit(constants.EVENTS.BID_TIMEOUT, bidTimeout);

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

0 comments on commit 222a46c

Please sign in to comment.