S
S
smoln2015-07-20 10:38:11
PHP
smoln, 2015-07-20 10:38:11

How to display Yandex Metrica statistics on the site?

Good afternoon, I decided to draw statistics from the metrics to the site according to the required parameters. I received the key, chose the data format, then I display the data.

$today=date("Ymd");
$metrika_url = "http://api-metrika.yandex.ru/stat/traffic/summary.json?id=счетчик&pretty=1&date1=$today&date2=$today&oauth_token=токен";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$metrika_url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$metrika = curl_exec ($ch);
curl_close($ch);
 
$metrika_o = json_decode($metrika);
echo "Статистика посещений за сегодня:<br>";
echo "Посетителей: ".$metrika_o->totals->visits."<br>";
echo "Из них новых: ".$metrika_o->totals->new_visitors."<br>";
echo "Просмотров: ".$metrika_o->totals->page_views."<br>";
echo "Просмотров: ".$metrika_o->totals->visit_time."<br>";

Displays general statistics, everything is correct, but as soon as I try to display statistics on advertising systems, nothing comes of it.
$metrika_r_url = "http://api-metrika.yandex.ru/stat/sources/marketing.json?id=счетчик&pretty=1&date1=$today&date2=$today&oauth_token=токен";
$chr = curl_init();
curl_setopt ($chr, CURLOPT_URL,$metrika_r_url);
curl_setopt ($chr, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($chr, CURLOPT_TIMEOUT, 60);
curl_setopt ($chr, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($chr, CURLOPT_RETURNTRANSFER, 1);
$metrikar = curl_exec ($chr);
curl_close($chr);
$metrika_r = json_decode($metrikar);
echo "Конверсия:<br>";
echo "Посетителей: ".$metrika_r->visits->conversion."<br>";

Who can tell me what I'm doing wrong... could this be due to the fact that the statistics themselves are not attached to my account, but my account has only full rights... or maybe I'm stupid and don't see simple errors)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-07-21
@smoln

<div class="table-title">
        <h3>Статистика посещений за последний месяц c <?php echo date('d-m-Y', strtotime('-1 month')); ?> по <?php echo date("d-m-Y"); ?>  из разных поисковых систем:</h3>
    </div>
    <table class="table-fill">
        <thead>
        <tr>
            <th class="text-left">Откуда</th>
            <th class="text-left">Количество визитов</th>
            <th class="text-left">Показатель отказов</th>
        </tr>
        </thead>
        <tbody class="table-hover">

        <?php
        $today=date("Ymd");
        $month_ago=date('Ymd', strtotime('-1 month'));
        $metrika_url = "http://api-metrika.yandex.ru/stat/sources/marketing.json?id=метрика&date1=$month_ago&date2=$today&sort=date&per_page=1000&oauth_token=токен";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $metrika_url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        $metrika = curl_exec ($ch);
        curl_close ($ch);
        $metrika_o = json_decode($metrika,true);
        
        foreach($metrika_o['data'] as $key=>$value){
            echo "<tr><td class='text-left'>".$value['name']."</td><td class='text-left'>".$value['visits']."</td><td class='text-left'>".($value['denial']*100)."%</td></tr>";
        }
        
        ?>
</tbody>
        </table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question