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

New version of Konduit Accelerate module #5164

Merged
merged 18 commits into from
May 7, 2020
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
225 changes: 225 additions & 0 deletions modules/konduitAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import { ajax } from '../src/ajax.js';
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import * as utils from '../src/utils.js';
import { targeting } from '../src/targeting.js';
import { config } from '../src/config.js';
import CONSTANTS from '../src/constants.json';

const TRACKER_HOST = 'tracker.konduit.me';

const analyticsType = 'endpoint';

const eventDataComposerMap = {
[CONSTANTS.EVENTS.AUCTION_INIT]: obtainAuctionInfo,
[CONSTANTS.EVENTS.AUCTION_END]: obtainAuctionInfo,
[CONSTANTS.EVENTS.BID_REQUESTED]: obtainBidRequestsInfo,
[CONSTANTS.EVENTS.BID_TIMEOUT]: obtainBidTimeoutInfo,
[CONSTANTS.EVENTS.BID_RESPONSE]: obtainBidResponseInfo,
[CONSTANTS.EVENTS.BID_WON]: obtainWinnerBidInfo,
[CONSTANTS.EVENTS.NO_BID]: obtainNoBidInfo,
};

// This function is copy from prebid core
function formatQS(query) {
return Object
.keys(query)
.map(k => Array.isArray(query[k])
? query[k].map(v => `${k}[]=${v}`).join('&')
: `${k}=${query[k]}`)
.join('&');
}

// This function is copy from prebid core
function buildUrl(obj) {
return (obj.protocol || 'http') + '://' +
(obj.host ||
obj.hostname + (obj.port ? `:${obj.port}` : '')) +
(obj.pathname || '') +
(obj.search ? `?${formatQS(obj.search || '')}` : '') +
(obj.hash ? `#${obj.hash}` : '');
}
Comment on lines +23 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify why you made your own local copies of these functions instead of just using them from the utils import? Are you concerned these functions would change in core unexpectedly for you? Or is there some other reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jsnellbaker!
This is a good question. The main reason for the local copies is we would like the Konduit module to be compatible with previous Prebid versions.
Please let us know if there is a better way to maintain compatibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The releases of Prebid are all snapshots of the repository taken at the respective times. Your module would only be available for use with the version of Prebid.js that published it (and future versions). It wouldn't inherently be backwards compatible, and I'm not sure there's any way within the current build/release process to make it work for older versions.

In regard to these functions, the local copies would seem to only help with the point I made before about potential worry of the core code updating and changing the logic slightly. I wouldn't say it won't happen, but changes like that are considered very carefully in terms of the impact and we would take steps to ensure it's handled well for everyone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsnellbaker thank you for these details, everything makes sense.

Our use case is in fact to be backwards compatible.

We have a number of existing publishers that are not ready to upgrade their Prebid version yet. For us it would be easier to have a backwards compatible module that can be used with previous Prebid versions without code modification.

This is the main reason we have the local copies and hopefully it is acceptable to leave it as is.
Please let us know if you have any concerns.


const getWinnerBidFromAggregatedEvents = () => {
return konduitAnalyticsAdapter.context.aggregatedEvents
.filter(evt => evt.eventType === CONSTANTS.EVENTS.BID_WON)[0];
};

const isWinnerBidDetected = () => {
return !!getWinnerBidFromAggregatedEvents();
};
const isWinnerBidExist = () => {
return !!targeting.getWinningBids()[0];
};

const konduitAnalyticsAdapter = Object.assign(
adapter({ analyticsType }),
{
track ({ eventType, args }) {
if (CONSTANTS.EVENTS.AUCTION_INIT === eventType) {
konduitAnalyticsAdapter.context.aggregatedEvents.splice(0);
}

if (eventDataComposerMap[eventType]) {
konduitAnalyticsAdapter.context.aggregatedEvents.push({
eventType,
data: eventDataComposerMap[eventType](args),
});
}

if (eventType === CONSTANTS.EVENTS.AUCTION_END) {
if (!isWinnerBidDetected() && isWinnerBidExist()) {
const bidWonData = eventDataComposerMap[CONSTANTS.EVENTS.BID_WON](targeting.getWinningBids()[0]);

konduitAnalyticsAdapter.context.aggregatedEvents.push({
eventType: CONSTANTS.EVENTS.BID_WON,
data: bidWonData,
});
}
sendRequest({ method: 'POST', path: '/analytics-initial-event', payload: composeRequestPayload() });
}
}
}
);

function obtainBidTimeoutInfo (args) {
return args.map(item => item.bidder).filter(utils.uniques);
}

function obtainAuctionInfo (auction) {
return {
auctionId: auction.auctionId,
timestamp: auction.timestamp,
auctionEnd: auction.auctionEnd,
auctionStatus: auction.auctionStatus,
adUnitCodes: auction.adUnitCodes,
labels: auction.labels,
timeout: auction.timeout
};
}

function obtainBidRequestsInfo (bidRequests) {
return {
bidderCode: bidRequests.bidderCode,
time: bidRequests.start,
bids: bidRequests.bids.map(function (bid) {
return {
transactionId: bid.transactionId,
adUnitCode: bid.adUnitCode,
bidId: bid.bidId,
startTime: bid.startTime,
sizes: utils.parseSizesInput(bid.sizes).toString(),
params: bid.params
};
}),
};
}

function obtainBidResponseInfo (bidResponse) {
return {
bidderCode: bidResponse.bidder,
transactionId: bidResponse.transactionId,
adUnitCode: bidResponse.adUnitCode,
statusMessage: bidResponse.statusMessage,
mediaType: bidResponse.mediaType,
renderedSize: bidResponse.size,
cpm: bidResponse.cpm,
currency: bidResponse.currency,
netRevenue: bidResponse.netRevenue,
timeToRespond: bidResponse.timeToRespond,
bidId: bidResponse.bidId,
requestId: bidResponse.requestId,
creativeId: bidResponse.creativeId
};
}

function obtainNoBidInfo (bidResponse) {
return {
bidderCode: bidResponse.bidder,
transactionId: bidResponse.transactionId,
adUnitCode: bidResponse.adUnitCode,
bidId: bidResponse.bidId,
};
}

function obtainWinnerBidInfo (bidResponse) {
return {
adId: bidResponse.adId,
bidderCode: bidResponse.bidder,
adUnitCode: bidResponse.adUnitCode,
statusMessage: bidResponse.statusMessage,
mediaType: bidResponse.mediaType,
renderedSize: bidResponse.size,
cpm: bidResponse.cpm,
currency: bidResponse.currency,
netRevenue: bidResponse.netRevenue,
timeToRespond: bidResponse.timeToRespond,
bidId: bidResponse.requestId,
dealId: bidResponse.dealId,
status: bidResponse.status,
creativeId: bidResponse.creativeId
};
}

function composeRequestPayload () {
const konduitId = config.getConfig('konduit.konduitId');
const { width, height } = window.screen;

return {
konduitId,
prebidVersion: '$prebid.version$',
environment: {
screen: { width, height },
language: navigator.language,
},
events: konduitAnalyticsAdapter.context.aggregatedEvents,
};
}

function sendRequest ({ host = TRACKER_HOST, method, path, payload }) {
const formattedUrlOptions = {
protocol: 'https',
hostname: host,
pathname: path,
};
if (method === 'GET') {
formattedUrlOptions.search = payload;
}

let konduitAnalyticsRequestUrl = buildUrl(formattedUrlOptions);

ajax(
konduitAnalyticsRequestUrl,
undefined,
method === 'POST' ? JSON.stringify(payload) : null,
{
contentType: 'application/json',
method,
withCredentials: true
}
);
}

konduitAnalyticsAdapter.originEnableAnalytics = konduitAnalyticsAdapter.enableAnalytics;

konduitAnalyticsAdapter.enableAnalytics = function (analyticsConfig) {
const konduitId = config.getConfig('konduit.konduitId');

if (!konduitId) {
utils.logError('A konduitId in config is required to use konduitAnalyticsAdapter');
return;
}

konduitAnalyticsAdapter.context = {
aggregatedEvents: [],
};

konduitAnalyticsAdapter.originEnableAnalytics(analyticsConfig);
};

adapterManager.registerAnalyticsAdapter({
adapter: konduitAnalyticsAdapter,
code: 'konduit'
});

export default konduitAnalyticsAdapter;
32 changes: 32 additions & 0 deletions modules/konduitAnalyticsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Overview
```
Module Name: Konduit Analytics Adapter
Module Type: Analytics Adapter
Maintainer: support@konduit.me
```
# Description
Konduit Analytics adapter pushes Prebid events into Konduit platform, which is then organizes the data and presents it to a client in different insightful views.
For more information, visit the [official Konduit website](https://konduitvideo.com/).
# Usage
Konduit Analytics can be enabled with a standard `enableAnalytics` call.
Note it is also important to provide a valid Konduit identifier as a config parameter.
```javascript
pbjs.setConfig({
konduit: {
konduitId: your_konduit_id,
}
});
pbjs.enableAnalytics({
provider: 'konduit'
})
```
Loading