Skip to content

Instantly share code, notes, and snippets.

@adambankin
Created October 16, 2018 17:34
Show Gist options
  • Save adambankin/13413fd27ebe2f4333e0cd056664e813 to your computer and use it in GitHub Desktop.
Save adambankin/13413fd27ebe2f4333e0cd056664e813 to your computer and use it in GitHub Desktop.
post.js
$(document).ready(function() {
//link to T1
$('#t1Logo').on('click', () =>{
window.location.href='https://t1.mediamath.com/app/#monitor';
})
$('#title').on('click', () =>{
window.location.href='/';
})
//connect the api to the ids
var currentDevice = '';
var currentMarket = '';
var currentLocation = '';
var marketTitle = '';
//the keys for the different devices
var devices = {
iphone: '( [<26,1,41001>] )',
android: '( [<26,1,41002>] )',
ipOrAd : '( [<26,1,41001>] OR [<26,1,41002>] )'
}
//the keys for the different markets
var markets = {
mmCuratedMarket: '( [<14,1,1000>] )',
appNexus: '( [<55,1,1>] )' ,
mmAndApp: '( [<14,1,1000>] ) AND ( [<55,1,1>] )'
}
//the keys for the different locations
var locations = {
us: '( [<5,1,60231>] )',
aust: '( [<5,1,60017>] )',
usOrAust: '( [<5,1,60017>] OR [<5,1,60231>] )'
}
//adjusting format of cookies
//get the cookie to work with the expression string for the graph
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
//setting pieces used for expression string
function getMarkets () {
return markets[currentMarket];
}
function getLocations () {
return locations[currentLocation];
}
function getDevices () {
return devices[currentDevice];
}
//changing the dropdown to it's respective name if clicked
$('#iphone').on('click', () =>{
$('#device').html('Iphone');
currentDevice = 'iphone';
});
$('#android').on('click', () =>{
$('#device').html('Android');
currentDevice = 'android';
});
$('#ipOrAd').on('click', () =>{
$('#device').html('Iphone or Android');
currentDevice = 'ipOrAd';
});
$('#us').on('click', () =>{
$('#locate').html('United States');
currentLocation = 'us';
});
$('#aust').on('click', () =>{
$('#locate').html('Australia');
currentLocation = 'aust';
});
$('#usOrAust').on('click', () =>{
$('#locate').html('United States or Australia');
currentLocation = 'usOrAust';
});
$('#mmCuratedMarket').on('click', () =>{
$('#market').html('MediaMath Curated Market');
currentMarket = 'mmCuratedMarket';
});
$('#appNexus').on('click', () =>{
$('#market').html('AppNexus');
currentMarket = 'appNexus';
});
$('#mmAndApp').on('click', () =>{
$('#market').html('MediaMath Curated Market and AppNexus');
currentMarket = 'mmAndApp';
});
$('#mmOrApp').on('click', () =>{
$('#market').html('MediaMath Curated Market or AppNexus');
currentMarket = 'split';
});
//Expression string used for the api to get the information from T1
function constructExpression () {
const markets = getMarkets();
const locations = getLocations();
const devices = getDevices();
const bids = $('#cass').val();
//Adjusting expression string can break it, exercise caution
var expression = `expression=( ( ${devices} ) AND ( ${locations} ) AND ( [<17,1,127> s0 d86400 l1] ) AND ( [<21,1,82346> hinherited i0] OR [<21,1,154749> hoverridden i0] ) AND ( NOT ( [<22,1,1>] ) ) AND ( [<12,1,13>] ) AND ( ${markets} ) )
price=${bids}
start_date=2018-10-18
end_date=2018-11-01
frequency_cap=20_1days`;
return expression
}
function generateTable (data, results) {
data = data.result;
var table = '<table class="table"><thead> <tr> <th scope="col"> Bid Price </th><th scope="col"> Total Spend </th> <th scope="col"> Opportunities </th> <th scope="col"> Unique Users </th> </thead> <tbody>';
for(var i = 0; i < data.length; i++){
table += '<tr style="display: table-row"><td>'
+ fnum(data[i].bid_price[0].value) + '</td><td>'
+ fnum(data[i].total_spend[0].value) +'</td><td>'
+ fnum(data[i].opportunities) + '</td><td>'
+ fnum(data[i].unique_users) + '</td></tr>'
}
return table += '</tbody></table>';
}
$('#doForecast').on('click', () => {
if(currentMarket === 'split'){
currentMarket = 'mmCuratedMarket';
var req1 = Promise.resolve($.ajax({
type: 'POST',
url: '/forecast-proxy',
data: {
expression: constructExpression(),
bearerToken: getCookie('bearerToken'),
adamaSession: getCookie('adama_session')
}
}))
currentMarket = 'appNexus';
var req2 = Promise.resolve($.ajax({
type: 'POST',
url: '/forecast-proxy',
data: {
expression: constructExpression(),
bearerToken: getCookie('bearerToken'),
adamaSession: getCookie('adama_session')
}
}))
Promise.all([req1, req2]).then(function (results) {
var tables ='';
for (var i = 0; i <1; i++) {//this is lazy fix don't EVER do this
marketTitle = 'MediaMath Curated Market';
console.log(marketTitle)
tables += [marketTitle+generateTable(results[0])];
marketTitle = 'AppNexus';
console.log(marketTitle)
tables += [marketTitle+generateTable(results[1])];
}
$('#forecastData').html(tables);
})
} else {
$.ajax({
type: 'POST',
url: '/forecast-proxy',
data: {
expression: constructExpression(),
bearerToken: getCookie('bearerToken'),
adamaSession: getCookie('adama_session')
}
}).then(function (data) {
$('#forecastData').html(market.innerHTML+[generateTable(data)]);
})
}
return false;
})
$('#saveForecast').on('click',() =>{
$('table').csvExport({
title: device.innerHTML + ', ' + locate.innerHTML + ', ' + market.innerHTML
});
})
//Editing Data layout in table
function fnum(x) {
if(isNaN(x)) return x;
if(x < 9999) {
return x.toFixed(1);
}
if(x < 100000) {
return Math.round(x/1000).toFixed(1) + "K";
}
if(x < 1000000) {
return Math.round(x/1000) + "K";
}
if( x < 10000000) {
return (x/1000000).toFixed(2) + "M";
}
if( x < 100000000) {
return (x/10000000).toFixed(2) + "M";
}
if(x < 1000000000) {
return Math.round((x/1000000)) + "M";
}
if( x < 10000000000) {
return (x/1000000000).toFixed(2) + "B";
}
if(x < 1000000000000) {
return Math.round((x/1000000000)) + "B";
}
return "1T+";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment