From 932f5815836a01977b950a488a850d08e31419cc Mon Sep 17 00:00:00 2001 From: Jeremy Sadwith Date: Thu, 7 Jul 2022 04:28:34 -0400 Subject: [PATCH] Kargo Analytics Adapter: Bid response time logging (#8510) * 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 Co-authored-by: Andy Rusiecki --- modules/kargoAnalyticsAdapter.js | 94 +++++++++++++++++-- modules/kargoAnalyticsAdapter.md | 33 +++++++ .../modules/kargoAnalyticsAdapter_spec.js | 42 +++++++++ 3 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 modules/kargoAnalyticsAdapter.md create mode 100644 test/spec/modules/kargoAnalyticsAdapter_spec.js 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'); + }); + }); +});