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

PubxAi analytics adapter #5915

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

const emptyUrl = '';
const analyticsType = 'endpoint';
const pubxaiAnalyticsVersion = 'v1.0.0';
const defaultHost = 'api.pbxai.com';
const auctionPath = '/analytics/auction';
const winningBidPath = '/analytics/bidwon';

let initOptions;
let auctionTimestamp;
let events = {
bids: []
};

var pubxaiAnalyticsAdapter = Object.assign(adapter(
{
emptyUrl,
analyticsType
}), {
track({ eventType, args }) {
if (typeof args !== 'undefined') {
if (eventType === CONSTANTS.EVENTS.BID_TIMEOUT) {
args.forEach(item => { mapBidResponse(item, 'timeout'); });
} else if (eventType === CONSTANTS.EVENTS.AUCTION_INIT) {
events.auctionInit = args;
auctionTimestamp = args.timestamp;
} else if (eventType === CONSTANTS.EVENTS.BID_REQUESTED) {
mapBidRequests(args).forEach(item => { events.bids.push(item) });
} else if (eventType === CONSTANTS.EVENTS.BID_RESPONSE) {
mapBidResponse(args, 'response');
} else if (eventType === CONSTANTS.EVENTS.BID_WON) {
send({
winningBid: mapBidResponse(args, 'bidwon')
}, 'bidwon');
}
}

if (eventType === CONSTANTS.EVENTS.AUCTION_END) {
send(events, 'auctionEnd');
}
}
});

function mapBidRequests(params) {
let arr = [];
if (typeof params.bids !== 'undefined' && params.bids.length) {
params.bids.forEach(function (bid) {
arr.push({
bidderCode: bid.bidder,
bidId: bid.bidId,
adUnitCode: bid.adUnitCode,
requestId: bid.bidderRequestId,
auctionId: bid.auctionId,
transactionId: bid.transactionId,
sizes: utils.parseSizesInput(bid.mediaTypes.banner.sizes).toString(),
renderStatus: 1,
requestTimestamp: params.auctionStart
});
});
}
return arr;
}

function mapBidResponse(bidResponse, status) {
if (status !== 'bidwon') {
let bid = events.bids.filter(o => o.bidId === bidResponse.bidId || o.bidId === bidResponse.requestId)[0];
Object.assign(bid, {
bidderCode: bidResponse.bidder,
bidId: status === 'timeout' ? bidResponse.bidId : bidResponse.requestId,
adUnitCode: bidResponse.adUnitCode,
auctionId: bidResponse.auctionId,
creativeId: bidResponse.creativeId,
transactionId: bidResponse.transactionId,
currency: bidResponse.currency,
cpm: bidResponse.cpm,
netRevenue: bidResponse.netRevenue,
mediaType: bidResponse.mediaType,
statusMessage: bidResponse.statusMessage,
floorData: bidResponse.floorData,
status: bidResponse.status,
renderStatus: status === 'timeout' ? 3 : 2,
timeToRespond: bidResponse.timeToRespond,
requestTimestamp: bidResponse.requestTimestamp,
responseTimestamp: bidResponse.responseTimestamp,
platform: navigator.platform,
deviceType: getDeviceType()
});
} else {
return {
bidderCode: bidResponse.bidder,
bidId: bidResponse.requestId,
adUnitCode: bidResponse.adUnitCode,
auctionId: bidResponse.auctionId,
creativeId: bidResponse.creativeId,
transactionId: bidResponse.transactionId,
currency: bidResponse.currency,
cpm: bidResponse.cpm,
netRevenue: bidResponse.netRevenue,
floorData: bidResponse.floorData,
renderedSize: bidResponse.size,
mediaType: bidResponse.mediaType,
statusMessage: bidResponse.statusMessage,
status: bidResponse.status,
renderStatus: 4,
timeToRespond: bidResponse.timeToRespond,
requestTimestamp: bidResponse.requestTimestamp,
responseTimestamp: bidResponse.responseTimestamp,
platform: navigator.platform,
deviceType: getDeviceType()
}
}
}

export function getDeviceType() {
if ((/ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()))) {
return 'tablet';
}
if ((/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(navigator.userAgent.toLowerCase()))) {
return 'mobile';
}
return 'desktop';
}

// add sampling rate
pubxaiAnalyticsAdapter.shouldFireEventRequest = function (samplingRate = 1) {
return (Math.floor((Math.random() * samplingRate + 1)) === parseInt(samplingRate));
}

function send(data, status) {
if (pubxaiAnalyticsAdapter.shouldFireEventRequest(initOptions.samplingRate)) {
let location = utils.getWindowLocation();
if (typeof data !== 'undefined' && typeof data.auctionInit !== 'undefined') {
Object.assign(data.auctionInit, { host: location.host, path: location.pathname, search: location.search });
}
data.initOptions = initOptions;

let pubxaiAnalyticsRequestUrl = utils.buildUrl({
protocol: 'https',
hostname: (initOptions && initOptions.hostName) || defaultHost,
pathname: status == 'bidwon' ? winningBidPath : auctionPath,
search: {
auctionTimestamp: auctionTimestamp,
pubxaiAnalyticsVersion: pubxaiAnalyticsVersion,
prebidVersion: $$PREBID_GLOBAL$$.version
}
});

ajax(pubxaiAnalyticsRequestUrl, undefined, JSON.stringify(data), { method: 'POST', contentType: 'text/plain' });
}
}

pubxaiAnalyticsAdapter.originEnableAnalytics = pubxaiAnalyticsAdapter.enableAnalytics;
pubxaiAnalyticsAdapter.enableAnalytics = function (config) {
initOptions = config.options;
pubxaiAnalyticsAdapter.originEnableAnalytics(config);
};

adapterManager.registerAnalyticsAdapter({
adapter: pubxaiAnalyticsAdapter,
code: 'pubxai'
});

export default pubxaiAnalyticsAdapter;
27 changes: 27 additions & 0 deletions modules/pubxaiAnalyticsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Overview
Module Name: PubX.io Analytics Adapter
Module Type: Analytics Adapter
Maintainer: phaneendra@pubx.ai

# Description

Analytics adapter for prebid provided by Pubx.ai. Contact alex@pubx.ai for information.

# Test Parameters

```
{
provider: 'pubxai',
options : {
aleksatr marked this conversation as resolved.
Show resolved Hide resolved
pubxId: 'xxx',
hostName: 'example.com',
samplingRate: 1
}
}
```
Property | Data Type | Is required? | Description |Example
:-----:|:-----:|:-----:|:-----:|:-----:
pubxId|string|Yes | A unique identifier provided by PubX.ai to indetify publishers. |`"a9d48e2f-24ec-4ec1-b3e2-04e32c3aeb03"`
hostName|string|No|hostName is provided by Pubx.ai. |`"https://example.com"`
samplingRate |number |No|How often the sampling must be taken. |`2` - (sample one in two cases) \ `3` - (sample one in three cases)
| | | |
Loading