18 private ?
array $profile =
null;
19 private ?
string $currency =
null;
23 $this->accountYandex = $accountYandex;
26 private function getProfile():
Result
30 if ($this->profile !==
null)
32 $result->setData($this->profile);
37 $profile = $this->accountYandex->getProfile();
38 if ($profile ===
null || empty($profile[
'NAME']))
40 $result->addError(
new Error(
'Can not find user name for yandex account.'));
45 $this->profile = $profile;
46 $result->setData($this->profile);
51 private function getClient(
array $headers = []):
Result
55 $profile = $this->getProfile();
56 if (!$profile->isSuccess())
58 return $result->addErrors($profile->getErrors());
61 $client = clone $this->accountYandex->getRequest()->getClient();
62 $client->setHeader(
'Authorization',
'Bearer ' . $this->getAuthToken());
63 $client->setHeader(
'Client-Login', $profile->getData()[
'NAME']);
65 foreach ($headers as
$name => $value)
67 $client->setHeader(
$name, $value);
70 return $result->setData([
'client' => $client]);
73 private function getAuthToken()
75 return $this->accountYandex->getRequest()->getAuthAdapter()->getToken();
87 $reportName = sprintf(
89 md5($dateFrom->
format(
'Y-m-d') . $dateTo->
format(
'Y-m-d')),
94 'SelectionCriteria' => [
95 'DateFrom' => $dateFrom->
format(
'Y-m-d'),
96 'DateTo' => $dateTo->
format(
'Y-m-d'),
111 'ReportName' => $reportName,
112 'ReportType' =>
'AD_PERFORMANCE_REPORT',
113 'DateRangeType' =>
'CUSTOM_DATE',
115 'IncludeVAT' =>
'YES',
116 'IncludeDiscount' =>
'NO',
121 'returnMoneyInMicros' =>
'false',
122 'skipReportHeader' =>
'true',
125 return $this->getEntityData(
'reports',
$request, $headers);
139 'SelectionCriteria' => [
140 'CampaignIds' => $campaignIds,
147 'TextAdFieldNames' => [
151 'TextImageAdFieldNames' => [
157 return $this->getEntityData(
'ads',
$request);
167 if ($this->currency !==
null)
173 $cacheString =
'analytics_yandex_currency';
174 $cachePath =
'/seo/analytics/yandex/';
176 $cache = Cache::createInstance();
178 if ($cache->initCache($cacheTime, $cacheString, $cachePath))
180 $currency = $cache->getVars()[
'currency'];
183 if (!empty($currency))
185 $this->currency = (string)$currency;
190 $cache->clean($cacheString, $cachePath);
195 'SelectionCriteria' => new \stdClass(),
196 'FieldNames' => [
'Currency'],
211 if (isset(
$response[
'result'][
'Campaigns']))
213 $firstCampaign = current(
$response[
'result'][
'Campaigns']);
214 $currency = $firstCampaign[
'Currency'] ??
null;
222 if ($cache->startDataCache($cacheTime))
224 $cache->endDataCache([
'currency' => $currency]);
227 $this->currency = (string)$currency;
244 $clientResult = $this->getClient($headers);
245 if (!$clientResult->isSuccess())
247 return $result->addErrors($clientResult->getErrors());
251 $client = $clientResult->getData()[
'client'];
253 $sendResult = $this->send($client, $this->getYandexServerAddress($entityName),
$request);
254 if (!$sendResult->isSuccess())
256 return $result->addErrors($sendResult->getErrors());
263 return $result->addError(
new Error(
"Empty response for {$entityName}"));
266 if ($client->getHeaders()->getContentType() ===
'application/json')
272 catch (\Exception $exception)
274 return $result->addError(
new Error($exception->getMessage()));
296 private function send(AdsHttpClient $client,
string $url,
array $request): ResponseResult
298 $result =
new ResponseResult();
304 if ($client->getStatus() === 200)
313 if ($errorByHttpStatus->getCode() === 201 || $errorByHttpStatus->getCode() === 202)
315 $retryIn = (int)($client->getHeaders()->get(
'retryIn') ?? 10);
320 $result->addError($errorByHttpStatus);
330 private function getYandexServerAddress(
string $source): string
332 $url =
'https://api.direct.yandex.com/json/v5/';
336 'campaigns' =>
$url .=
'campaigns',
337 'reports' =>
$url .=
'reports',
338 'ads' =>
$url .=
'ads',
339 default =>
throw new SystemException(
"Unknown source {$source}"),
348 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_UNKNOWN');
352 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_201');
356 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_202');
360 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_400');
364 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_500');
368 $message = Loc::getMessage(
'SEO_ANALYTICS_SERVICE_HELPERS_YANDEX_SENDER_HTTP_ERROR_502');
379 if (isset(
$error[
'error_string']))
383 if (isset(
$error[
'error_detail']))
389 if (isset(
$error[
'error_code']))
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)