B
B
bal_square2017-10-03 16:23:16
PHP
bal_square, 2017-10-03 16:23:16

Yandex.Metrica statistics for 10 accounts, is it possible to speed up?

Hello everyone, I wrote a function that loads data from Yandex metrics and displays it on the screen in the form I need, I have an array of clientLogin and token that store login data, the fact is that when the number of accounts increases, for example, up to 10, requests take a very long time, tell me how to speed up the code and is it even possible? I tried to write code for Yandex Direct and load statistics from there, but it takes even longer

/*Подгружаем данные с яндекс.метрики*/
function metric_request ($date,$metric,$token,$direct_login){
  
$ids_temp=file_get_contents('https://api-metrika.yandex.ru/management/v1/counters?oauth_token='.$token);
$ids_temp_o=json_decode($ids_temp);
$ids = array();
$size=count($ids_temp_o->counters);
  
for($i=0;$i<$size;$i++){
$ids[$i]=$ids_temp_o->counters[$i]->id;
}
  
  
$query = array(
    'date1' => $date,
    'date2' => 'today',
    'ids' => $ids,
    'metrics' => $metric,
    'oauth_token' => $token,
   	'direct_client_logins'=> $direct_login,
);
$response=file_get_contents('https://api-metrika.yandex.ru/stat/v1/data?'.http_build_query($query));
$response_o=json_decode($response);
return $response_o;
};	

$size=count($clientLogin);
for ($i=0;$i<$size;$i++){
  
    $ClicksCost_7days=metric_request ('7daysAgo','ym:ad:<currency>AdCost',$token[$i],$clientLogin[$i]);
    $MidConsumption=$ClicksCost_7days->totals[0] / 7;
    $balance = get_amount_apiv4($token[$i],$clientLogin[$i]);
    $Days_to_Paid = round($balance/$MidConsumption , 0, PHP_ROUND_HALF_DOWN);

    $ClicksAmount_Cost_today=metric_request('yesterday','ym:ad:clicks,ym:ad:<currency>AdCost',$token[$i],$clientLogin[$i]);
    
  
    $MidCostToday=round($ClicksAmount_Cost_today->totals[1] / $ClicksAmount_Cost_today->totals[0],2,PHP_ROUND_HALF_UP);
  
    $Denied = metric_request('yesterday','ym:s:bounceRate',$token[$i],$clientLogin[$i]);
  
   	echo "<div id=\"main_form\">
     
     <p class=\"text\" style=\"background:chocolate;\">Логин: {$clientLogin[$i]}</p>
     <p class=\"text\">Ниша: просто текст</p>
     <p class=\"text\">Оставшийся бюджет: ".$balance." RUB</p>
     <p class=\"text\">Необходимо пополнение: ".$Days_to_Paid." день(дней)</p>
     <p class=\"text\">Количество кликов (за сегодня): ".$ClicksAmount_Cost_today->totals[0]."</p>
     <p class=\"text\">Средняя цена кликов (за сегодня): ".$MidCostToday." RUB</p>
     <p class=\"text\">Отказы (за сегодня): ".round($Denied->totals[0],2, PHP_ROUND_HALF_UP)." %</p>
     <p class=\"text\">Расход (за сегодня): ".round($ClicksAmount_Cost_today->totals[1],2, PHP_ROUND_HALF_UP)." RUB</p></div>
     "; 
    
     
   }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2017-10-03
@bal_square

Most likely, Yandex.API cannot be accelerated.
Use a background process to regularly update the metric data and save it in a certain database (launched by cron, for example), and in your code, output data from this cache to the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question