Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accountfacebook.php
1<?
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->getRequest()->getClient()->get(
32 'https://graph.facebook.com/me?fields=id,name,picture,link&access_token=' .
33 urlencode($this->getRequest()->getAuthAdapter()->getToken())
34 );
35
36 if ($response)
37 {
38 $response = Json::decode($response);
39 if (is_array($response))
40 {
41 return array(
42 'ID' => $response['id'] ?? null,
43 'NAME' => $response['name'] ?? null,
44 'LINK' => '',
45 'PICTURE' => $response['picture']['data']['url'] ?? null,
46 );
47 }
48 }
49
50
51 return null;
52 }
53
62 public function getExpenses($accountId, Date $dateFrom = null, Date $dateTo = null)
63 {
64 $parameters = [
65 'ACCOUNT_ID' => $accountId,
66 ];
67 if($dateFrom && $dateTo)
68 {
69 $parameters['DATE_FROM'] = $dateFrom->format('Ymd');
70 $parameters['DATE_TO'] = $dateTo->format('Ymd');
71 }
72 $response = $this->getRequest()->send([
73 'methodName' => 'analytics.expenses.get',
74 'parameters' => $parameters,
75 ]);
76
77 $data = $response->getData();
78 $expenses = new Expenses();
79 $expenses->add([
80 'impressions' => $data['impressions'],
81 'clicks' => $data['clicks'],
82 'actions' => $data['actions'],
83 'cpc' => $data['cpc'],
84 'cpm' => $data['cpm'],
85 'spend' => $data['spend'],
86 'currency' => $data['currency'],
87 ]);
88
89 $response = (new ResponseFacebook());
90 $response->setData(['expenses' => $expenses]);
91
92 return $response;
93 }
94
95 protected function prepareExpensesData($data)
96 {
97 return $data;
98 }
99
105 public function hasExpensesReport()
106 {
107 return true;
108 }
109
118 public function getExpensesReport($accountId, Date $dateFrom = null, Date $dateTo = null)
119 {
120 if (mb_substr($accountId, 0, 4) === 'act_')
121 {
122 $accountId = mb_substr($accountId, 4);
123 }
124
125 $parameters = [
126 'ACCOUNT_ID' => $accountId,
127 ];
128 if($dateFrom && $dateTo)
129 {
130 $parameters['DATE_FROM'] = $dateFrom->format('Ymd');
131 $parameters['DATE_TO'] = $dateTo->format('Ymd');
132 }
133 $response = $this->getRequest()->send([
134 'methodName' => 'analytics.expenses.report',
135 'parameters' => $parameters,
136 ]);
137
138 return $response;
139 }
140
146 public function hasPublicPages()
147 {
148 return true;
149 }
150
156 public function getPublicPages($accountId)
157 {
158 $response = $this->getRequest()->send([
159 'method' => 'GET',
160 'endpoint' => 'act_'.$accountId.'/promote_pages',
161 'fields' => [
162 'fields' => 'id,name,about,cover,emails,phone',
163 ]
164 ]);
165
166 if($response->isSuccess())
167 {
168 $pages = [];
169 $data = $response->getData();
170 foreach($data as $page)
171 {
172 $pages[] = new Page([
173 'id' => $page['id'],
174 'name' => $page['name'],
175 'about' => $page['about'],
176 'image' => $page['cover']['source'],
177 'phone' => $page['phone'],
178 'email' => $page['emails'],
179 ]);
180 }
181 $response->setData($pages);
182 }
183
184 return $response;
185 }
186
193 protected function updatePublicPage($publicPageId, array $params)
194 {
195 $result = new ResponseFacebook();
196 $fields = [];
197 if(isset($params['phone']))
198 {
199 $fields['phone'] = $params['phone'];
200 }
201 if(isset($params['email']))
202 {
203 $fields['emails'] = $params['email'];
204 }
205
206 $response = $this->getRequest()->send([
207 'method' => 'POST',
208 'endpoint' => $publicPageId,
209 'fields' => $fields,
210 ]);
211 if(!$response->isSuccess())
212 {
213 $result->addErrors($response->getErrors());
214 }
215
216 $response = $this->getRequest()->send([
217 'method' => 'GET',
218 'endpoint' => $publicPageId.'/call_to_actions',
219 'fields' => [
220 'fields' => 'id,type'
221 ],
222 ]);
223 if($response->isSuccess())
224 {
225 $callToAction = $response->getData();
226 if($callToAction['type'] == 'CALL_NOW' && isset($fields['phone']))
227 {
228 $response = $this->getRequest()->send([
229 'method' => 'POST',
230 'endpoint' => $callToAction['id'],
231 'fields' => [
232 'intl_number_with_plus' => $fields['phone']
233 ]
234 ]);
235 }
236 elseif($callToAction == 'EMAIL' && isset($fields['emails']))
237 {
238 $response = $this->getRequest()->send([
239 'method' => 'POST',
240 'endpoint' => $callToAction['id'],
241 'fields' => [
242 'email_address' => $fields['emails']
243 ]
244 ]);
245 }
246 }
247
248 return $response;
249 }
250
259 public function updateAnalyticParams($accountId, array $params, array $publicPageIds = [])
260 {
261 // get all ads
262 // get current ad creative for each
263 // create new ad creative for each with new url_tags
264 // update each ad with new creative
265 $result = new ResponseFacebook();
266 if(empty($params))
267 {
268 return $result;
269 }
270 if(!empty($params['url_tags']))
271 {
272 $updateAdResult = $this->updateAdUrlTags($accountId, $params['url_tags']);
273 if(!$updateAdResult->isSuccess())
274 {
275 $result->addErrors($updateAdResult->getErrors());
276 }
277 }
278
279 if($this->hasPublicPages() && !empty($params['phone']) || !empty($params['email']))
280 {
281 foreach($publicPageIds as $publicPageId)
282 {
283 $updatePageResult = $this->updatePublicPage($publicPageId, $params);
284 if(!$updatePageResult->isSuccess())
285 {
286 $result->addErrors($updatePageResult->getErrors());
287 }
288 }
289 }
290
291 return $result;
292 }
293
299 protected function getAds($accountId)
300 {
301 $adSetResult = $this->getAdSetIds($accountId);
302 if($adSetResult->isSuccess())
303 {
304 $adSetIds = $adSetResult->getData();
305 if(empty($adSetIds))
306 {
307 return $adSetResult;
308 }
309 }
310 else
311 {
312 return $adSetResult;
313 }
314 $fields = [
315 'fields' => 'id,adset_id,campaign_id,creative',
316 ];
317
318 $adsResult = $this->getRequest()->send([
319 'method' => 'GET',
320 'endpoint' => 'act_'.$accountId.'/ads',
321 'fields' => $fields,
322 ]);
323 if($adsResult->isSuccess())
324 {
325 $ads = $adsResult->getData();
326 $result = [];
327 foreach($ads as $ad)
328 {
329 if(in_array($ad['adset_id'], $adSetIds))
330 {
331 $result[] = $ad;
332 }
333 }
334 $adsResult->setData($result);
335 }
336
337 return $adsResult;
338 }
339
344 protected function getAdCreative($creativeId)
345 {
346 $fields = [
347 'fields' => 'id,account_id,actor_id,adlabels,applink_treatment,asset_feed_spec,body,branded_content_sponsor_page_id,'.
348 'call_to_action_type,effective_instagram_story_id,effective_object_story_id,image_crops,image_hash,'.
349 'image_url,instagram_actor_id,instagram_permalink_url,instagram_story_id,link_og_id,link_url,'.
350 'messenger_sponsored_message,name,object_id,object_story_id,object_story_spec,object_type,object_url,'.
351 'platform_customizations,portrait_customizations,product_set_id,recommender_settings,status,template_url,'.
352 'template_url_spec,thumbnail_url,title,url_tags,use_page_actor_override,video_id',
353 ];
354
355 return $this->getRequest()->send([
356 'method' => 'GET',
357 'endpoint' => $creativeId,
358 'fields' => $fields,
359 ]);
360 }
361
370 protected function updateAdCreative($accountId, $adId, array $creative)
371 {
372 unset($creative['id']);
373 foreach($creative as $key => $value)
374 {
375 if(is_array($value))
376 {
377 $creative[$key] = Json::encode($value);
378 }
379 }
380
381 $response = $this->getRequest()->send([
382 'method' => 'POST',
383 'endpoint' => 'act_'.$accountId.'/adcreatives',
384 'fields' => $creative,
385 ]);
386
387 if($response->isSuccess())
388 {
389 $data = $response->getData();
390 if(isset($data['id']))
391 {
392 $response = $this->getRequest()->send([
393 'method' => 'POST',
394 'enpoint' => $adId,
395 'fields' => ['creative' => $data['id']],
396 ]);
397 }
398 else
399 {
400 $response->addError(new Error('Could not find id after Ad Creative add'));
401 }
402 }
403
404 return $response;
405 }
406
414 protected function updateAdUrlTags($accountId, array $urlParams)
415 {
416 $result = $this->getAds($accountId);
417 if(!$result->isSuccess() || empty($result->getData()))
418 {
419 return $result;
420 }
421
422 $ads = $result->getData();
423 foreach($ads as $ad)
424 {
425 if(!isset($ad['creative']) || !isset($ad['creative']['id']))
426 {
427 continue;
428 }
429 $getAdCreativeResult = $this->getAdCreative($ad['creative']['id']);
430 if($getAdCreativeResult->isSuccess())
431 {
432 $creative = $getAdCreativeResult->getData();
433 $currentUrlParams = $this->parseUrlParams($creative['url_tags']);
434 $creative['url_tags'] = http_build_query($this->mergeUrlParams($currentUrlParams, $urlParams));
435
436 $updateAdCreativeResult = $this->updateAdCreative($accountId, $ad['id'], $creative);
437 if(!$updateAdCreativeResult->isSuccess())
438 {
439 $result->addErrors($updateAdCreativeResult->getErrors());
440 }
441 }
442 else
443 {
444 $result->addErrors($getAdCreativeResult->getErrors());
445 }
446 }
447
448 return $result;
449 }
450
455 protected function parseUrlParams($string)
456 {
457 $result = [];
458
459 if(empty($string))
460 {
461 return $result;
462 }
463
464 $pairs = explode('&', $string);
465 foreach($pairs as $pair)
466 {
467 list($name, $value) = explode('=', $pair);
468 $result[$name] = urldecode($value);
469 }
470
471 return $result;
472 }
473
479 protected function mergeUrlParams(array $currentParams, array $newParams)
480 {
481 foreach($newParams as $name => $value)
482 {
483 if(empty($value))
484 {
485 if(isset($currentParams[$name]))
486 {
487 unset($currentParams[$name]);
488 }
489 }
490 else
491 {
492 $currentParams[$name] = $value;
493 }
494 }
495
496 return $currentParams;
497 }
498
502 protected function getPublisherPlatforms()
503 {
504 return ['facebook', 'messenger', 'audience_network'];
505 }
506
512 public function getAdSetIds($accountId)
513 {
514 $response = $this->getRequest()->send([
515 'method' => 'GET',
516 'endpoint' => 'act_'.$accountId.'/adsets',
517 'fields' => [
518 'fields' => 'id,name,targeting'
519 ],
520 ]);
521 if($response->isSuccess())
522 {
523 $data = $response->getData();
524 $facebook = $instagram = [];
525 foreach($data as $adSet)
526 {
527 $all[] = $adSet['id'];
528 if(
529 isset($adSet['targeting']) && is_array($adSet['targeting']) &&
530 isset($adSet['targeting']['publisher_platforms']) && is_array($adSet['targeting']['publisher_platforms']) &&
531 count($adSet['targeting']['publisher_platforms']) == 1 && reset($adSet['targeting']['publisher_platforms']) == 'instagram'
532 )
533 {
534 $instagram[] = $adSet['id'];
535 }
536 else
537 {
538 $facebook[] = $adSet['id'];
539 }
540 }
541 if(static::TYPE_CODE === 'instagram')
542 {
543 $result = $instagram;
544 }
545 else
546 {
547 $result = $facebook;
548 }
549 $response->setData($result);
550 }
551
552 return $response;
553 }
554}
updatePublicPage($publicPageId, array $params)
mergeUrlParams(array $currentParams, array $newParams)
updateAnalyticParams($accountId, array $params, array $publicPageIds=[])
getExpensesReport($accountId, Date $dateFrom=null, Date $dateTo=null)
updateAdCreative($accountId, $adId, array $creative)
updateAdUrlTags($accountId, array $urlParams)
getExpenses($accountId, Date $dateFrom=null, Date $dateTo=null)