Skip to content

Commit

Permalink
Separate bids & won calls (#2015)
Browse files Browse the repository at this point in the history
* separate bids & won calls

* readd polyfills
  • Loading branch information
Yann-Pravo authored and jaiminpanchal27 committed Jan 22, 2018
1 parent 25f1e5a commit c58a158
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
55 changes: 30 additions & 25 deletions modules/adomikAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import adapter from 'src/AnalyticsAdapter';
import CONSTANTS from 'src/constants.json';
import adaptermanager from 'src/adaptermanager';
import { logInfo } from 'src/utils';
import find from 'core-js/library/fn/array/find';
import findIndex from 'core-js/library/fn/array/find-index';

Expand All @@ -12,19 +13,14 @@ const bidResponse = CONSTANTS.EVENTS.BID_RESPONSE;
const bidWon = CONSTANTS.EVENTS.BID_WON;
const bidTimeout = CONSTANTS.EVENTS.BID_TIMEOUT;

let bidwonTimeout = 1000;

let adomikAdapter = Object.assign(adapter({}),
{
// Track every event needed
track({ eventType, args }) {
switch (eventType) {
case auctionInit:
adomikAdapter.initializeBucketEvents()
adomikAdapter.currentContext.id = args.auctionId
adomikAdapter.currentContext.timeout = args.timeout
if (args.config.bidwonTimeout !== undefined && typeof args.config.bidwonTimeout === 'number') {
bidwonTimeout = args.config.bidwonTimeout;
}
break;

case bidTimeout:
Expand All @@ -39,12 +35,9 @@ let adomikAdapter = Object.assign(adapter({}),
break;

case bidWon:
adomikAdapter.bucketEvents.push({
type: 'winner',
event: {
id: args.adId,
placementCode: args.adUnitCode
}
adomikAdapter.sendWonEvent({
id: args.adId,
placementCode: args.adUnitCode
});
break;

Expand All @@ -61,24 +54,25 @@ let adomikAdapter = Object.assign(adapter({}),
break;

case auctionEnd:
setTimeout(() => {
if (adomikAdapter.bucketEvents.length > 0) {
adomikAdapter.sendTypedEvent();
}
}, bidwonTimeout);
if (adomikAdapter.bucketEvents.length > 0) {
adomikAdapter.sendTypedEvent();
}
break;
}
}
}
);

adomikAdapter.initializeBucketEvents = function() {
adomikAdapter.bucketEvents = [];
}

adomikAdapter.sendTypedEvent = function() {
const groupedTypedEvents = adomikAdapter.buildTypedEvents();

const bulkEvents = {
uid: adomikAdapter.currentContext.uid,
ahbaid: adomikAdapter.currentContext.id,
timeout: adomikAdapter.currentContext.timeout,
hostname: window.location.hostname,
eventsByPlacementCode: groupedTypedEvents.map(function(typedEventsByType) {
let sizes = [];
Expand Down Expand Up @@ -108,8 +102,11 @@ adomikAdapter.sendTypedEvent = function() {
})
};

const stringBulkEvents = JSON.stringify(bulkEvents)
logInfo('Events sent to adomik prebid analytic ' + stringBulkEvents);

// Encode object in base64
const encodedBuf = window.btoa(JSON.stringify(bulkEvents));
const encodedBuf = window.btoa(stringBulkEvents);

// Create final url and split it in 1600 characters max (+endpoint length)
const encodedUri = encodeURIComponent(encodedBuf);
Expand All @@ -122,6 +119,17 @@ adomikAdapter.sendTypedEvent = function() {
})
};

adomikAdapter.sendWonEvent = function (wonEvent) {
const stringWonEvent = JSON.stringify(wonEvent)
logInfo('Won event sent to adomik prebid analytic ' + wonEvent);

// Encode object in base64
const encodedBuf = window.btoa(stringWonEvent);
const encodedUri = encodeURIComponent(encodedBuf);
const img = new Image(1, 1);
img.src = `https://${adomikAdapter.currentContext.url}/?q=${encodedUri}&id=${adomikAdapter.currentContext.id}&won=true`
}

adomikAdapter.buildBidResponse = function (bid) {
return {
bidder: bid.bidderCode.toUpperCase(),
Expand Down Expand Up @@ -181,23 +189,20 @@ adomikAdapter.buildTypedEvents = function () {
return groupedTypedEvents;
}

// Initialize adomik object
adomikAdapter.currentContext = {};
adomikAdapter.bucketEvents = [];

adomikAdapter.adapterEnableAnalytics = adomikAdapter.enableAnalytics;

adomikAdapter.enableAnalytics = function (config) {
adomikAdapter.currentContext = {};

const initOptions = config.options;
if (initOptions) {
adomikAdapter.currentContext = {
uid: initOptions.id,
url: initOptions.url,
debug: initOptions.debug,
id: '',
timeouted: false,
timeout: 0,
}
logInfo('Adomik Analytics enabled with config', initOptions);
adomikAdapter.adapterEnableAnalytics(config);
}
};
Expand Down
33 changes: 12 additions & 21 deletions test/spec/modules/adomikAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Adomik Prebid Analytic', function () {
beforeEach(() => {
sinon.spy(adomikAnalytics, 'track');
sinon.spy(adomikAnalytics, 'sendTypedEvent');
sinon.spy(adomikAnalytics, 'sendWonEvent');
});

afterEach(() => {
Expand Down Expand Up @@ -51,25 +52,21 @@ describe('Adomik Prebid Analytic', function () {
expect(adomikAnalytics.currentContext).to.deep.equal({
uid: '123456',
url: 'testurl',
debug: undefined,
id: '',
timeouted: false,
timeout: 0,
timeouted: false
});

// Step 1: Send init auction event
events.emit(constants.EVENTS.AUCTION_INIT, {config: initOptions, auctionId: 'test-test-test', timeout: 3000});
// Step 2: Send init auction event
events.emit(constants.EVENTS.AUCTION_INIT, {config: initOptions, auctionId: 'test-test-test'});

expect(adomikAnalytics.currentContext).to.deep.equal({
uid: '123456',
url: 'testurl',
debug: undefined,
id: 'test-test-test',
timeouted: false,
timeout: 3000,
timeouted: false
});

// Step 2: Send bid requested event
// Step 3: Send bid requested event
events.emit(constants.EVENTS.BID_REQUESTED, { bids: [bid] });

expect(adomikAnalytics.bucketEvents.length).to.equal(1);
Expand All @@ -81,7 +78,7 @@ describe('Adomik Prebid Analytic', function () {
}
});

// Step 3: Send bid response event
// Step 4: Send bid response event
events.emit(constants.EVENTS.BID_RESPONSE, bid);

expect(adomikAnalytics.bucketEvents.length).to.equal(2);
Expand All @@ -102,29 +99,23 @@ describe('Adomik Prebid Analytic', function () {
}
});

// Step 4: Send bid won event
// Step 5: Send bid won event
events.emit(constants.EVENTS.BID_WON, bid);

expect(adomikAnalytics.bucketEvents.length).to.equal(3);
expect(adomikAnalytics.bucketEvents[2]).to.deep.equal({
type: 'winner',
event: {
id: '1234',
placementCode: '0000',
}
});
expect(adomikAnalytics.bucketEvents.length).to.equal(2);

// Step 5: Send bid timeout event
// Step 6: Send bid timeout event
events.emit(constants.EVENTS.BID_TIMEOUT, {});

expect(adomikAnalytics.currentContext.timeouted).to.equal(true);

// Step 6: Send auction end event
// Step 7: Send auction end event
var clock = sinon.useFakeTimers();
events.emit(constants.EVENTS.AUCTION_END, {});

setTimeout(function() {
sinon.assert.callCount(adomikAnalytics.sendTypedEvent, 1);
sinon.assert.callCount(adomikAnalytics.sendWonEvent, 1);
done();
}, 3000);

Expand Down

0 comments on commit c58a158

Please sign in to comment.