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

Adxcg analytics adapter #1599

Merged
merged 1 commit into from
Oct 3, 2017
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
94 changes: 94 additions & 0 deletions modules/adxcgAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import adaptermanager from 'src/adaptermanager';
import * as url from 'src/url';
import * as utils from 'src/utils';

const emptyUrl = '';
const analyticsType = 'endpoint';
const adxcgAnalyticsVersion = 'v1.04';

let initOptions;
let auctionTimestamp;
let events = {
bidRequests: [],
bidResponses: []
};

var adxcgAnalyticsAdapter = Object.assign(adapter(
{
emptyUrl,
analyticsType
}), {
track({eventType, args}) {
if (typeof args !== 'undefined') {
if (eventType === 'bidTimeout') {
events.bidTimeout = args;
} else if (eventType === 'auctionInit') {
events.auctionInit = args;
auctionTimestamp = args.timestamp;
} else if (eventType === 'bidRequested') {
events.bidRequests.push(args);
} else if (eventType === 'bidResponse') {
events.bidResponses.push(mapBidResponse(args));
} else if (eventType === 'bidWon') {
send({
bidWon: mapBidResponse(args)
});
}
}

if (eventType === 'auctionEnd') {
send(events);
}
}
});

function mapBidResponse(bidResponse) {
return {
adUnitCode: bidResponse.adUnitCode,
statusMessage: bidResponse.statusMessage,
bidderCode: bidResponse.bidderCode,
adId: bidResponse.adId,
mediaType: bidResponse.mediaType,
creative_id: bidResponse.creative_id,
width: bidResponse.width,
height: bidResponse.height,
cpm: bidResponse.cpm,
timeToRespond: bidResponse.timeToRespond
};
}

function send(data) {
data.initOptions = initOptions;
data.auctionTimestamp = auctionTimestamp;

let location = utils.getTopWindowLocation();
let secure = location.protocol == 'https:';
Copy link
Collaborator

Choose a reason for hiding this comment

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

we prefer triple equals


let adxcgAnalyticsRequestUrl = url.format({
protocol: secure ? 'https' : 'http',
hostname: secure ? 'hbarxs.adxcg.net' : 'hbarx.adxcg.net',
pathname: '/pbrx',
search: {
auctionTimestamp: auctionTimestamp,
adxcgAnalyticsVersion: adxcgAnalyticsVersion,
prebidVersion: $$PREBID_GLOBAL$$.version
}
});

ajax(adxcgAnalyticsRequestUrl, undefined, JSON.stringify(data), {method: 'POST'});
}

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

adaptermanager.registerAnalyticsAdapter({
adapter: adxcgAnalyticsAdapter,
code: 'adxcg'
});

export default adxcgAnalyticsAdapter;
81 changes: 81 additions & 0 deletions test/spec/modules/adxcgAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import adxcgAnalyticsAdapter from 'modules/adxcgAnalyticsAdapter';
import { expect } from 'chai';
let adaptermanager = require('src/adaptermanager');
let events = require('src/events');
let constants = require('src/constants.json');

describe('adxcg analytics adapter', () => {
let xhr;
let requests;

beforeEach(() => {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = request => requests.push(request);
});

afterEach(() => {
xhr.restore();
});

describe('track', () => {
it('builds and sends auction data', () => {
let auctionTimestamp = 42;
let initOptions = {
publisherId: '42'
};
let bidRequest = {
requestId: 'requestIdData'
};
let bidResponse = {
adId: 'adIdData',
ad: 'adContent'
};

adaptermanager.registerAnalyticsAdapter({
code: 'adxcg',
adapter: adxcgAnalyticsAdapter
});

adaptermanager.enableAnalytics({
provider: 'adxcg',
options: initOptions
});

events.emit(constants.EVENTS.AUCTION_INIT, {
timestamp: auctionTimestamp
});
events.emit(constants.EVENTS.BID_REQUESTED, bidRequest);
events.emit(constants.EVENTS.BID_RESPONSE, bidResponse);
events.emit(constants.EVENTS.AUCTION_END, {});

expect(requests.length).to.equal(1);

let auctionEventData = JSON.parse(requests[0].requestBody);

expect(auctionEventData.bidRequests.length).to.equal(1);
expect(auctionEventData.bidRequests[0]).to.deep.equal(bidRequest);

expect(auctionEventData.bidResponses.length).to.equal(1);
expect(auctionEventData.bidResponses[0].adId).to.equal(bidResponse.adId);
expect(auctionEventData.bidResponses[0]).to.not.have.property('ad');

expect(auctionEventData.initOptions).to.deep.equal(initOptions);
expect(auctionEventData.auctionTimestamp).to.equal(auctionTimestamp);

events.emit(constants.EVENTS.BID_WON, {
adId: 'adIdData',
ad: 'adContent'
});

expect(requests.length).to.equal(2);

let winEventData = JSON.parse(requests[1].requestBody);

expect(winEventData.bidWon.adId).to.equal(bidResponse.adId);
expect(winEventData.bidWon).to.not.have.property('ad');
expect(winEventData.initOptions).to.deep.equal(initOptions);
expect(winEventData.auctionTimestamp).to.equal(auctionTimestamp);
});
});
});