1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
accountfacebook.php
См. документацию.
1<?php
2
4
10use \Bitrix\Seo\Analytics\Account;
14
16{
17 const TYPE_CODE = 'facebook';
18
19 public function getList()
20 {
21 $response = $this->request->send(array(
22 'methodName' => 'analytics.account.list',
23 'parameters' => array()
24 ));
25
26 return $response;
27 }
28
29 public function getProfile()
30 {
31 $response = $this->request->send([
32 'methodName' => 'analytics.profile',
33 'parameters' => [],
34 ]
35 );
36
37 if ($response && $response->isSuccess())
38 {
39 $response = $response->getData();
40 if (is_array($response))
41 {
42 return array(
43 'ID' => $response['id'] ?? null,
44 'NAME' => $response['name'] ?? null,
45 'LINK' => '',
46 'PICTURE' => $response['picture']['data']['url'] ?? null,
47 );
48 }
49 }
50
51
52 return null;
53 }
54
63 public function getExpenses($accountId, Date $dateFrom = null, Date $dateTo = null)
64 {
65 $parameters = [
66 'ACCOUNT_ID' => $accountId,
67 ];
68 if($dateFrom && $dateTo)
69 {
70 $parameters['DATE_FROM'] = $dateFrom->format('Ymd');
71 $parameters['DATE_TO'] = $dateTo->format('Ymd');
72 }
73 $response = $this->getRequest()->send([
74 'methodName' => 'analytics.expenses.get',
75 'parameters' => $parameters,
76 ]);
77
78 $data = $response->getData();
79 $expenses = new Expenses();
80 $expenses->add([
81 'impressions' => $data['impressions'],
82 'clicks' => $data['clicks'],
83 'actions' => $data['actions'],
84 'cpc' => $data['cpc'],
85 'cpm' => $data['cpm'],
86 'spend' => $data['spend'],
87 'currency' => $data['currency'],
88 ]);
89
91 $response->setData(['expenses' => $expenses]);
92
93 return $response;
94 }
95
96 protected function prepareExpensesData($data)
97 {
98 return $data;
99 }
100
106 public function hasExpensesReport()
107 {
108 return true;
109 }
110
119 public function getExpensesReport($accountId, Date $dateFrom = null, Date $dateTo = null)
120 {
121 if (mb_substr($accountId, 0, 4) === 'act_')
122 {
123 $accountId = mb_substr($accountId, 4);
124 }
125
126 $parameters = [
127 'ACCOUNT_ID' => $accountId,
128 ];
129 if($dateFrom && $dateTo)
130 {
131 $parameters['DATE_FROM'] = $dateFrom->format('Ymd');
132 $parameters['DATE_TO'] = $dateTo->format('Ymd');
133 }
134 $response = $this->getRequest()->send([
135 'methodName' => 'analytics.expenses.report',
136 'parameters' => $parameters,
137 ]);
138
139 return $response;
140 }
141
149 public function getDailyExpensesReport(?string $accountId, ?Date $dateFrom, ?Date $dateTo): Result
150 {
151 if (mb_substr($accountId, 0, 4) === 'act_')
152 {
153 $accountId = mb_substr($accountId, 4);
154 }
155
156 $parameters = [
157 'ACCOUNT_ID' => $accountId,
158 ];
159
160 if ($dateFrom && $dateTo)
161 {
162 $parameters['DATE_FROM'] = $dateFrom->format('Ymd');
163 $parameters['DATE_TO'] = $dateTo->format('Ymd');
164 }
165
166 $response = $this->getRequest()->send([
167 'methodName' => 'analytics.expenses.ads.report',
168 'parameters' => $parameters,
169 'streamTimeout' => static::LOAD_DAILY_EXPENSES_TIMEOUT,
170 'listenHttpErrors' => true,
171 ]);
172
173 $result = new Result();
174
175 if (!$response->isSuccess())
176 {
177 $innerErrors = implode(',', $response->getErrorMessages());
178 $errorMessage = $this->buildErrorMessage("Error occurred while load daily expenses: {$innerErrors}");
179
180 return $result->addError(new Error($errorMessage));
181 }
182
183 $data = $response->getData();
184 $result->setData(['expenses' => Helpers\ExpensesAdapter::translateExpensesReportToDailyExpenses($data)]);
185
186 return $result;
187 }
188
189 public function hasDailyExpensesReport(): bool
190 {
191 return true;
192 }
193
199 public function hasPublicPages()
200 {
201 return true;
202 }
203
209 public function getPublicPages($accountId)
210 {
211 $response = $this->getRequest()->send([
212 'method' => 'GET',
213 'endpoint' => 'act_'.$accountId.'/promote_pages',
214 'fields' => [
215 'fields' => 'id,name,about,cover,emails,phone',
216 ]
217 ]);
218
219 if($response->isSuccess())
220 {
221 $pages = [];
222 $data = $response->getData();
223 foreach($data as $page)
224 {
225 $pages[] = new Page([
226 'id' => $page['id'],
227 'name' => $page['name'],
228 'about' => $page['about'],
229 'image' => $page['cover']['source'],
230 'phone' => $page['phone'],
231 'email' => $page['emails'],
232 ]);
233 }
234 $response->setData($pages);
235 }
236
237 return $response;
238 }
239
246 protected function updatePublicPage($publicPageId, array $params)
247 {
249 $fields = [];
250 if(isset($params['phone']))
251 {
252 $fields['phone'] = $params['phone'];
253 }
254 if(isset($params['email']))
255 {
256 $fields['emails'] = $params['email'];
257 }
258
259 $response = $this->getRequest()->send([
260 'method' => 'POST',
261 'endpoint' => $publicPageId,
262 'fields' => $fields,
263 ]);
264 if(!$response->isSuccess())
265 {
266 $result->addErrors($response->getErrors());
267 }
268
269 $response = $this->getRequest()->send([
270 'method' => 'GET',
271 'endpoint' => $publicPageId.'/call_to_actions',
272 'fields' => [
273 'fields' => 'id,type'
274 ],
275 ]);
276 if($response->isSuccess())
277 {
278 $callToAction = $response->getData();
279 if($callToAction['type'] == 'CALL_NOW' && isset($fields['phone']))
280 {
281 $response = $this->getRequest()->send([
282 'method' => 'POST',
283 'endpoint' => $callToAction['id'],
284 'fields' => [
285 'intl_number_with_plus' => $fields['phone']
286 ]
287 ]);
288 }
289 elseif($callToAction == 'EMAIL' && isset($fields['emails']))
290 {
291 $response = $this->getRequest()->send([
292 'method' => 'POST',
293 'endpoint' => $callToAction['id'],
294 'fields' => [
295 'email_address' => $fields['emails']
296 ]
297 ]);
298 }
299 }
300
301 return $response;
302 }
303
312 public function updateAnalyticParams($accountId, array $params, array $publicPageIds = [])
313 {
314 // get all ads
315 // get current ad creative for each
316 // create new ad creative for each with new url_tags
317 // update each ad with new creative
319 if(empty($params))
320 {
321 return $result;
322 }
323 if(!empty($params['url_tags']))
324 {
325 $updateAdResult = $this->updateAdUrlTags($accountId, $params['url_tags']);
326 if(!$updateAdResult->isSuccess())
327 {
328 $result->addErrors($updateAdResult->getErrors());
329 }
330 }
331
332 if($this->hasPublicPages() && !empty($params['phone']) || !empty($params['email']))
333 {
334 foreach($publicPageIds as $publicPageId)
335 {
336 $updatePageResult = $this->updatePublicPage($publicPageId, $params);
337 if(!$updatePageResult->isSuccess())
338 {
339 $result->addErrors($updatePageResult->getErrors());
340 }
341 }
342 }
343
344 return $result;
345 }
346
352 protected function getAds($accountId)
353 {
354 $adSetResult = $this->getAdSetIds($accountId);
355 if($adSetResult->isSuccess())
356 {
357 $adSetIds = $adSetResult->getData();
358 if(empty($adSetIds))
359 {
360 return $adSetResult;
361 }
362 }
363 else
364 {
365 return $adSetResult;
366 }
367 $fields = [
368 'fields' => 'id,adset_id,campaign_id,creative',
369 ];
370
371 $adsResult = $this->getRequest()->send([
372 'method' => 'GET',
373 'endpoint' => 'act_'.$accountId.'/ads',
374 'fields' => $fields,
375 ]);
376 if($adsResult->isSuccess())
377 {
378 $ads = $adsResult->getData();
379 $result = [];
380 foreach($ads as $ad)
381 {
382 if(in_array($ad['adset_id'], $adSetIds))
383 {
384 $result[] = $ad;
385 }
386 }
387 $adsResult->setData($result);
388 }
389
390 return $adsResult;
391 }
392
397 protected function getAdCreative($creativeId)
398 {
399 $fields = [
400 'fields' => 'id,account_id,actor_id,adlabels,applink_treatment,asset_feed_spec,body,branded_content_sponsor_page_id,'.
401 'call_to_action_type,effective_instagram_story_id,effective_object_story_id,image_crops,image_hash,'.
402 'image_url,instagram_actor_id,instagram_permalink_url,instagram_story_id,link_og_id,link_url,'.
403 'messenger_sponsored_message,name,object_id,object_story_id,object_story_spec,object_type,object_url,'.
404 'platform_customizations,portrait_customizations,product_set_id,recommender_settings,status,template_url,'.
405 'template_url_spec,thumbnail_url,title,url_tags,use_page_actor_override,video_id',
406 ];
407
408 return $this->getRequest()->send([
409 'method' => 'GET',
410 'endpoint' => $creativeId,
411 'fields' => $fields,
412 ]);
413 }
414
423 protected function updateAdCreative($accountId, $adId, array $creative)
424 {
425 unset($creative['id']);
426 foreach($creative as $key => $value)
427 {
428 if(is_array($value))
429 {
430 $creative[$key] = Json::encode($value);
431 }
432 }
433
434 $response = $this->getRequest()->send([
435 'method' => 'POST',
436 'endpoint' => 'act_'.$accountId.'/adcreatives',
437 'fields' => $creative,
438 ]);
439
440 if($response->isSuccess())
441 {
442 $data = $response->getData();
443 if(isset($data['id']))
444 {
445 $response = $this->getRequest()->send([
446 'method' => 'POST',
447 'enpoint' => $adId,
448 'fields' => ['creative' => $data['id']],
449 ]);
450 }
451 else
452 {
453 $response->addError(new Error('Could not find id after Ad Creative add'));
454 }
455 }
456
457 return $response;
458 }
459
467 protected function updateAdUrlTags($accountId, array $urlParams)
468 {
469 $result = $this->getAds($accountId);
470 if(!$result->isSuccess() || empty($result->getData()))
471 {
472 return $result;
473 }
474
475 $ads = $result->getData();
476 foreach($ads as $ad)
477 {
478 if(!isset($ad['creative']) || !isset($ad['creative']['id']))
479 {
480 continue;
481 }
482 $getAdCreativeResult = $this->getAdCreative($ad['creative']['id']);
483 if($getAdCreativeResult->isSuccess())
484 {
485 $creative = $getAdCreativeResult->getData();
486 $currentUrlParams = $this->parseUrlParams($creative['url_tags']);
487 $creative['url_tags'] = http_build_query($this->mergeUrlParams($currentUrlParams, $urlParams));
488
489 $updateAdCreativeResult = $this->updateAdCreative($accountId, $ad['id'], $creative);
490 if(!$updateAdCreativeResult->isSuccess())
491 {
492 $result->addErrors($updateAdCreativeResult->getErrors());
493 }
494 }
495 else
496 {
497 $result->addErrors($getAdCreativeResult->getErrors());
498 }
499 }
500
501 return $result;
502 }
503
508 protected function parseUrlParams($string)
509 {
510 $result = [];
511
512 if(empty($string))
513 {
514 return $result;
515 }
516
517 $pairs = explode('&', $string);
518 foreach($pairs as $pair)
519 {
520 list($name, $value) = explode('=', $pair);
521 $result[$name] = urldecode($value);
522 }
523
524 return $result;
525 }
526
532 protected function mergeUrlParams(array $currentParams, array $newParams)
533 {
534 foreach($newParams as $name => $value)
535 {
536 if(empty($value))
537 {
538 if(isset($currentParams[$name]))
539 {
540 unset($currentParams[$name]);
541 }
542 }
543 else
544 {
545 $currentParams[$name] = $value;
546 }
547 }
548
549 return $currentParams;
550 }
551
555 protected function getPublisherPlatforms()
556 {
557 return ['facebook', 'messenger', 'audience_network'];
558 }
559
565 public function getAdSetIds($accountId)
566 {
567 $response = $this->getRequest()->send([
568 'method' => 'GET',
569 'endpoint' => 'act_'.$accountId.'/adsets',
570 'fields' => [
571 'fields' => 'id,name,targeting'
572 ],
573 ]);
574 if($response->isSuccess())
575 {
576 $data = $response->getData();
577 $facebook = $instagram = [];
578 foreach($data as $adSet)
579 {
580 $all[] = $adSet['id'];
581 if(
582 isset($adSet['targeting']) && is_array($adSet['targeting']) &&
583 isset($adSet['targeting']['publisher_platforms']) && is_array($adSet['targeting']['publisher_platforms']) &&
584 count($adSet['targeting']['publisher_platforms']) == 1 && reset($adSet['targeting']['publisher_platforms']) == 'instagram'
585 )
586 {
587 $instagram[] = $adSet['id'];
588 }
589 else
590 {
591 $facebook[] = $adSet['id'];
592 }
593 }
594 if(static::TYPE_CODE === 'instagram')
595 {
596 $result = $instagram;
597 }
598 else
599 {
600 $result = $facebook;
601 }
602 $response->setData($result);
603 }
604
605 return $response;
606 }
607}
Определения result.php:20
Определения error.php:15
Определения response.php:5
Определения date.php:9
format($format)
Определения date.php:110
Определения json.php:9
buildErrorMessage(string $error)
Определения account.php:194
updatePublicPage($publicPageId, array $params)
Определения accountfacebook.php:246
mergeUrlParams(array $currentParams, array $newParams)
Определения accountfacebook.php:532
updateAnalyticParams($accountId, array $params, array $publicPageIds=[])
Определения accountfacebook.php:312
getDailyExpensesReport(?string $accountId, ?Date $dateFrom, ?Date $dateTo)
Определения accountfacebook.php:149
getExpensesReport($accountId, Date $dateFrom=null, Date $dateTo=null)
Определения accountfacebook.php:119
updateAdCreative($accountId, $adId, array $creative)
Определения accountfacebook.php:423
updateAdUrlTags($accountId, array $urlParams)
Определения accountfacebook.php:467
getExpenses($accountId, Date $dateFrom=null, Date $dateTo=null)
Определения accountfacebook.php:63
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$name
Определения menu_edit.php:35
Определения aliases.php:54
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$page
Определения order_form.php:33
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$fields
Определения yandex_run.php:501