forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KRAK-3663 - Analytics timeout support (#5)
* 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
Showing
3 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |