L
L
Londeren2014-01-27 13:53:47
PHP
Londeren, 2014-01-27 13:53:47

Yandex.Metrica: why aren't goals created via the API?

Manual smoked.
The code is the following:

<?php

$params = array(
      'name' => 'GOAL',
      'type' => 'url',
      'depth' => '',
      'conditions' => array(
        array(
          'type' =>  'exact',
          'url' => 'GOAL_URL',
        )
      )
    ) ;


 $params = json_encode($params);

    $opts = array(
      'http' => array(
        'method' => "POST",
        'header' => "Authorization: OAuth " . $token . "\r\n" .
                    "Accept: application/x-yametrika+json" . "\r\n" .
                    "Content-Type: application/x-yametrika+json" . "\r\n" .
                    "Content-length: " . strlen($params) . "\r\n",
        'content' => $params
      )
    ) ;

    $context = stream_context_create($opts);

    $result = file_get_contents('http://api-metrika.yandex.ru' . $uri . '.json?pretty=1', null, $context);

$result returns a list of current targets without any errors, no new one is created.
What could be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2014-01-27
@Londeren

This is stupidly not in the API description, but $params should be wrapped in another array:
'content' => array('goal'=>$params),
clubs.ya.ru/metrika/replies.xml?item_no=9965
And immediately and one more incredibly logical thing - in response, a stupid list of all goals, including the added one, is returned. And you need to look for a newly created goal among them yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question