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

Custom 2017 08 25 #2

Closed
wants to merge 20 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added hpcAnalyticsAdapter
  • Loading branch information
ptomasroos committed Aug 26, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 35bd325de0c11fc58425d7168fc079e77c033dd9
99 changes: 99 additions & 0 deletions modules/hpcAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import CONSTANTS from 'src/constants.json';
import adaptermanager from 'src/adaptermanager';
const utils = require('src/utils');

const url = 'https://api.bitgn.com/hpc?t=8e186e7f-1dd6-4b35-9846-05882a441bc0';
const analyticsType = 'endpoint';

let auctionInitConst = CONSTANTS.EVENTS.AUCTION_INIT;
let auctionEndConst = CONSTANTS.EVENTS.AUCTION_END;
let bidWonConst = CONSTANTS.EVENTS.BID_WON;

let initOptions = {gender: null, age: null, id: null, shard: null};
let bidWon = {options: {}, events: []};
let eventStack = {options: {}, events: []};

let auctionStatus = 'not_started';

function checkOptions() {
return initOptions.shard !== null;
}

function buildBidWon(eventType, args) {
bidWon.options = initOptions;
bidWon.events = [{args: args, eventType: eventType}];
}

function buildEventStack() {
eventStack.options = initOptions;
}

function send(eventType, data, sendDataType) {
let fullUrl = url + '&gender=' + initOptions.gender + '&age=' + initOptions.age + '&id=' + initOptions.id + '&shard=' + initOptions.shard;

ajax(
fullUrl,
(result) => utils.logInfo('Event ' + eventType + ' sent ' + sendDataType + ' to hpc analytic with result' + result),
JSON.stringify(data)
);
}

function pushEvent(eventType, args) {
eventStack.events.push({eventType, args});
}

function flushEvents() {
eventStack.events = [];
}

let hpcAdapter = Object.assign(adapter({url, analyticsType}),
{
track({eventType, args}) {
if (!checkOptions()) {
return;
}

let info = Object.assign({}, args);

if (info && info.ad) {
info.ad = '';
}

if (eventType === auctionInitConst) {
auctionStatus = 'started';
flushEvents();
}

if ((eventType === bidWonConst) && auctionStatus === 'not_started') {
buildBidWon(eventType, info);
send(eventType, bidWon, 'bidWon');
return;
}

if (eventType === auctionEndConst) {
buildEventStack(eventType);
send(eventType, eventStack, 'eventStack');
flushEvents();
auctionStatus = 'not_started';
} else {
pushEvent(eventType, info);
}
}
});

hpcAdapter.originEnableAnalytics = hpcAdapter.enableAnalytics;

hpcAdapter.enableAnalytics = function (config) {
initOptions = config.options;
utils.logInfo('HPC Analytics enabled with config', initOptions);
hpcAdapter.originEnableAnalytics(config);
};

adaptermanager.registerAnalyticsAdapter({
adapter: hpcAdapter,
code: 'hpc'
});

export default hpcAdapter;