diff --git a/modules/kargoAnalyticsAdapter.js b/modules/kargoAnalyticsAdapter.js index 83c20846c0d..8ddf14aaa83 100644 --- a/modules/kargoAnalyticsAdapter.js +++ b/modules/kargoAnalyticsAdapter.js @@ -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; diff --git a/modules/kargoAnalyticsAdapter.md b/modules/kargoAnalyticsAdapter.md new file mode 100644 index 00000000000..5a1e538902a --- /dev/null +++ b/modules/kargoAnalyticsAdapter.md @@ -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 + } +} +``` \ No newline at end of file diff --git a/test/spec/modules/kargoAnalyticsAdapter_spec.js b/test/spec/modules/kargoAnalyticsAdapter_spec.js new file mode 100644 index 00000000000..c27c8499aa1 --- /dev/null +++ b/test/spec/modules/kargoAnalyticsAdapter_spec.js @@ -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'); + }); + }); +});