Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question