Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
audiencefacebook.php
1<?
2
4
5use \Bitrix\Seo\Retargeting\Audience;
7
9{
10 const TYPE_CODE = 'facebook';
11
14 const URL_AUDIENCE_LIST = 'https://business.facebook.com/adsmanager/audiences';
15
16 protected static $listRowMap = array(
17 'ID' => 'ID',
18 'NAME' => 'NAME',
19 'COUNT_VALID' => 'APPROXIMATE_COUNT',
20 'COUNT_MATCHED' => 'APPROXIMATE_COUNT',
21 'SUPPORTED_CONTACT_TYPES' => array(
22 self::ENUM_CONTACT_TYPE_EMAIL,
23 self::ENUM_CONTACT_TYPE_PHONE,
24 self::ENUM_CONTACT_TYPE_IDFA_GAID,
25 self::ENUM_CONTACT_TYPE_INTERNAL_ID
26 ),
27 );
28
29 public function add(array $data)
30 {
31 $response = $this->getRequest()->send(array(
32 'methodName' => 'retargeting.audience.add',
33 'parameters' => array(
34 'accountId' => $this->accountId,
35 'name' => $data['NAME'],
36 'description' => $data['DESCRIPTION'],
37 )
38 ));
39
40 $responseData = $response->getData();
41 if (isset($responseData['id']))
42 {
43 $response->setId($responseData['id']);
44 }
45
46 return $response;
47 }
48
49 protected function prepareContacts(array $contacts = array())
50 {
51 $data = array();
52
53 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
54 {
55 if (!isset($contacts[$contactType]))
56 {
57 continue;
58 }
59
60 $contactsCount = count($contacts[$contactType]);
61 for ($i = 0; $i < $contactsCount; $i++)
62 {
63 $contact = $contacts[$contactType][$i];
64 $contact = hash('sha256', $contact);
65
66 switch ($contactType)
67 {
69 $data[] = array($contact, '');
70 break;
71
73 $data[] = array('', $contact);
74 break;
75 }
76 }
77 }
78
79 return array(
80 'schema' => array('EMAIL', 'PHONE'), //$schema == 'PHONE' ? 'PHONE_MD5' : 'EMAIL_MD5',
81 'data' => $data
82 );
83 }
84
85 public function importContacts($audienceId, array $contacts, array $options)
86 {
87 return $this->getRequest()->send(array(
88 'methodName' => 'retargeting.audience.contacts.add',
89 'parameters' => array(
90 'accountId' => $this->accountId,
91 'audienceId' => $audienceId,
92 'contacts' => Json::encode(
93 $this->prepareContacts($contacts)
94 )
95 )
96 ));
97 }
98
99 public function removeContacts($audienceId, array $contacts, array $options)
100 {
101 return $this->getRequest()->send(array(
102 'methodName' => 'retargeting.audience.contacts.remove',
103 'parameters' => array(
104 'accountId' => $this->accountId,
105 'audienceId' => $audienceId,
106 'contacts' => Json::encode(
107 $this->prepareContacts($contacts)
108 )
109 )
110 ));
111 }
112
113 public function getList()
114 {
115 $response = $this->getRequest()->send(array(
116 'methodName' => 'retargeting.audience.list',
117 'parameters' => array(
118 'accountId' => $this->accountId
119 )
120 ));
121 $data = $response->getData();
122 $data = array_values(array_filter($data, function ($item) {
123 return ($item['subtype'] == 'CUSTOM'); // only CUSTOM type (list of clients) is supported
124 }));
125 $response->setData($data);
126 return $response;
127 }
128
129 public static function isSupportAddAudience()
130 {
131 return true;
132 }
133
135 {
136 $sizes = [];
137 for ($i=1; $i<10;$i++)
138 {
139 $sizes[$i] = $i;
140 }
141 return [
142 'FIELDS' => ['AUDIENCE_SIZE', 'AUDIENCE_REGION'],
143 'SIZES' => $sizes,
144 ];
145 }
146
147 public function createLookalike($sourceAudienceId, array $options)
148 {
149 $result = $this->getRequest()->send(array(
150 'methodName' => 'retargeting.audience.lookalike.add',
151 'parameters' => array(
152 'accountId' => $this->accountId,
153 'audienceId' => $sourceAudienceId,
154 'options' => $options
155 )
156 ));
157 if ($result->isSuccess())
158 {
159 $result->setId($result->getData()['id']);
160 }
161 return $result;
162 }
163}
removeContacts($audienceId, array $contacts, array $options)
importContacts($audienceId, array $contacts, array $options)
createLookalike($sourceAudienceId, array $options)