<?php // , : startDate endDate // Adfox .. date_default_timezone_set("Europe/Moscow"); //$startDate = '04.09.15'; //$endDate = '04.09.15'; // $startDate = date('dmy', strtotime('-1 day')); $endDate = $startDate; // getCampaigns // $campIDs ID // dateAddedFrom , function getCampaigns() { $ch = curl_init(); $options = array( CURLOPT_URL => 'https://login.adfox.ru/API.php?object=account&action=list&actionObject=superCampaign&dateAddedFrom=2015-08-01&loginAccount=&loginPassword=-sha-256&isXML=1', CURLOPT_HEADER => "Content-Type:application/xml", CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_RETURNTRANSFER => TRUE ); curl_setopt_array($ch, $options); $data = curl_exec($ch); curl_close($ch); // Adfox , // file_put_contents('request.txt', $data, FILE_APPEND); // Adfox XML vals $parser=xml_parser_create('UTF-8'); xml_parse_into_struct($parser, $data, $vals, $index); $campIDs = array(); $j = 0; // vals, ID campIDs for ($i = 0; $i < count($vals); ++$i) { if ($vals[$i]["tag"]=="ID") { $ID = $vals[$i]["value"]; } if ($vals[$i]["tag"]=="NAME") { $name = $vals[$i]["value"]; $campIDs[$j]['id'] = $ID; $campIDs[$j]['name'] = $name; $j += 1; } } return $campIDs; } // getBanners ID // campReport ID + - - - function getBanners($id, $startDate, $endDate) { $ch = curl_init(); $options = array( CURLOPT_URL => 'https://login.adfox.ru/commonReportsOutputForm.php?period=&startDate='.$startDate.'&endDate='.$endDate.'&criteria=superCampaignBanner&isNewSearch=on&objectName=superCampaign&objectID='.$id.'&ignoreStartDate=off&loginAccount=&loginPassword=-sha-256&isXML=1', CURLOPT_HEADER => "Content-Type:application/xml", CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_RETURNTRANSFER => TRUE ); curl_setopt_array($ch, $options); $data = curl_exec($ch); curl_close($ch); // Adfox , //file_put_contents('request.txt', $data, FILE_APPEND); $parser=xml_parser_create('UTF-8'); xml_parse_into_struct($parser, $data, $vals, $index); $campReport = array(); $j = 0; for ($i = 0; $i < count($vals); ++$i) { if ($vals[$i]["tag"]=="CRITERIA") { $bannername = $vals[$i]["value"]; } if ($vals[$i]["tag"]=="FLIGHTNAME") { $flightname = $vals[$i]["value"]; } if ($vals[$i]["tag"]=="IMPRESSIONS") { $impressions = $vals[$i]["value"]; } if ($vals[$i]["tag"]=="CLICKS") { $clicks = $vals[$i]["value"]; $campReport[$j]['campaign'] = $id; $campReport[$j]['flightname'] = $flightname; $campReport[$j]['bannername'] = $bannername; $campReport[$j]['impressions'] = $impressions; $campReport[$j]['clicks'] = $clicks; $j += 1; } } return $campReport; } // $campIDs = getCampaigns(); // for ($i = 0; $i < count($campIDs); ++$i) { $campReport = getBanners($campIDs[$i]['id'], $startDate, $endDate); // data.txt // data.txt Cron for ($j = 0; $j < count($campReport); ++$j) { file_put_contents('/var/www/html/adfox/data.txt', $startDate."\t".$campReport[$j]['campaign']."\t".$campReport[$j]['flightname']."\t".$campReport[$j]['bannername']."\t".$campReport[$j]['impressions']."\t".$campReport[$j]['clicks']."\n", FILE_APPEND); // () echo 'Record created - '.$startDate.' - '.$campReport[$j]['bannername']."\n"; } }
<html> <head> <script src="jquery-1.11.3.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> </head> <body> <script> // , data.txt var banners = []; // , , data.txt // CTR ( ) var dates1 = []; var shows1 = []; var clicks1 = []; var ctr1 = []; var dates2 = []; var shows2 = []; var clicks2 = []; var ctr2 = []; var dates3 = []; var shows3 = []; var clicks3 = []; var ctr3 = []; // data.txt $.get("data.txt", function(data){ var lines = data.split("\n"); for (var i = 0, len = lines.length; i < len; i++) { // , //console.log(lines[i]); // 6 , data.txt elements = lines[i].split("\t"); // , elements // ( - 0, 1) banners.push(elements[2]); } // , //console.log(banners); // bannerNames 3 Array.prototype.unique = function() { var tmp = {}, out = []; for(var i = 0, n = this.length; i < n; ++i) { if(!tmp[this[i]]) { tmp[this[i]] = true; out.push(this[i]); } } return out; } bannersNames = banners.unique().slice(0, 3); // , 3 //console.log(bannersNames); // data.txt , bannerNames for (var i = 0, len = lines.length; i < len; i++) { elements = lines[i].split("\t"); // data.txt , , if (elements[2] == bannersNames[0]) { dates1.push(elements[0]); shows1.push(parseInt(elements[4])); clicks1.push(parseInt(elements[5])); // toFixed(3) - CTR if (parseInt(elements[4]) > 0) { ctr1.push(Number(parseFloat(elements[5] / elements[4] * 100).toFixed(3))); } else { ctr1.push(0); } } // if (elements[2] == bannersNames[1]) { dates2.push(elements[0]); shows2.push(parseInt(elements[4])); clicks2.push(parseInt(elements[5])); if (parseInt(elements[4]) > 0) { ctr2.push(Number(parseFloat(elements[5] / elements[4] * 100).toFixed(3))); } else { ctr2.push(0); } } // if (elements[2] == bannersNames[2]) { dates3.push(elements[0]); shows3.push(parseInt(elements[4])); clicks3.push(parseInt(elements[5])); if (parseInt(elements[4]) > 0) { ctr3.push(Number(parseFloat(elements[5] / elements[4] * 100).toFixed(3))); } else { ctr3.push(0); } } } // , CTR ( ) // : [0.22, 0.25, 0.30, 0.24] //console.log(ctr1); //console.log(ctr2); //console.log(ctr3); // CTR $(function () { // container <div id="container"... $('#container').highcharts({ // title: { text: 'Campaign banners CTR', x: -20 //center }, // subtitle: { text: 'Source: ADFOX', x: -20 }, // dates1 xAxis: { categories: dates1 }, // yAxis: { title: { text: 'CTR %' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, // tooltip: { valueSuffix: '%' }, // legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', borderWidth: 0 }, // , : CTR series: [{ name: bannersNames[0], data: ctr1 }, { name: bannersNames[1], data: ctr2 }, { name: bannersNames[2], data: ctr3 }], credits: { enabled: false } }); }); }); </script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> </body> </html>
Source: https://habr.com/ru/post/266307/
All Articles