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

KRAK-3663 - Analytics timeout support #5

Merged
merged 8 commits into from
May 18, 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
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,
{
jsadwith marked this conversation as resolved.
Show resolved Hide resolved
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');
});
});
});