D
D
den94ka2014-09-09 11:29:36
PHP
den94ka, 2014-09-09 11:29:36

How to activate the visor in Yandex Metrica in PHP?

I'm trying to create a Yandex metrics counter - it doesn't work. More precisely, it is possible to create a regular counter, but not with a click map and a webvisor.
Here is the code to create a regular counter:

$data = array(
            'site'      => "$url",
            'name'    => "$name",
            'oauth_token' => 'TOKEN'
        );
        $url = "http://api-metrika.yandex.ru/counters";
        $content = json_encode($data);
 
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
 
        $json_response = curl_exec($curl);
 
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 
        if ( $status != 200 ) {
            die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }
 
        curl_close($curl);

Everything works and the counter is created. Here is the code that tries to add a web viewer and a heatmap:
$code_options = array(
            'clickmap' => '1',
            'visor' => '1'
        );
        $c_options = json_encode($code_options);
        $data = array(
            'site'      => "$url",
            'name'    => "$name",
            'code_options' => "$c_options",
            'oauth_token' => 'TOKEN'
        );
        $url = "http://api-metrika.yandex.ru/counters";
        $content = json_encode($data);
 
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
 
        $json_response = curl_exec($curl);
 
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 
        if ( $status != 200 ) {
            die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }
 
        curl_close($curl);

This code also creates a counter, but the web viewer and click map are not activated.
What am I doing wrong? Maybe I misunderstood the "structure" type in the Yandex Metrics documentation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2014-09-09
@den94ka

I don't think code_options should be re-encoded in JSON.
The api metric is very strange in places, but not so much.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question