Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
audiencevkontakte.php
1<?php
2
4
6use \Bitrix\Seo\Retargeting\Audience;
7use \Bitrix\Seo\Retargeting\Response;
8
10{
11 const TYPE_CODE = 'vkontakte';
12
15 const URL_AUDIENCE_LIST = 'https://ads.vk.com/hq/audience/user_lists';
16
17 const ENUM_CONTACT_TYPE_IDFA_GAID = 'gaid'; // IDFA (Identifier For Advertising) or device ID (Android ID and UDID on iOS)
19
20 protected const USER_LIST_TYPE_HUMAN = 'human';
21
22 protected static $listRowMap = [
23 'ID' => 'ID',
24 'NAME' => 'NAME',
25 'COUNT_VALID' => 'AUDIENCE_COUNT',
26 'COUNT_MATCHED' => 'AUDIENCE_COUNT',
27 'SUPPORTED_CONTACT_TYPES' => [
32 ],
33 ];
34
35 public static function isSupportRemoveContacts()
36 {
37 return false;
38 }
39
40 public function add(array $data)
41 {
42 $response = $this->getRequest()->send([
43 'methodName' => 'retargeting.audience.add',
44 'parameters' => [
45 'name' => $data['NAME'],
46 'contacts' => [[
47 'email' => 'example@example.com',
48 ]],
49 ]
50 ]);
51
52 $responseData = $response->getData();
53 if (isset($responseData['id']))
54 {
55 $response->setId($responseData['id']);
56 }
57
58 return $response;
59 }
60
61 protected function prepareContacts(array $contacts = [])
62 {
63 $data = [];
64 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
65 {
66 if (!isset($contacts[$contactType]))
67 {
68 continue;
69 }
70 foreach ($contacts[$contactType] as $contact)
71 {
72 $data[] = [$contactType => $contact];
73 }
74 }
75
76 return $data;
77 }
78
79 public function importContacts($audienceId, array $contacts, array $options)
80 {
81 return $this->getRequest()->send([
82 'methodName' => 'retargeting.audience.contacts.add',
83 'parameters' => [
84 'audienceId' => $audienceId,
85 'contacts' => $this->prepareContacts($contacts)
86 ]
87 ]);
88 }
89
90 public function removeContacts($audienceId, array $contacts, array $options)
91 {
92 return Response::create(static::TYPE_CODE);
93 }
94
95 public function getList()
96 {
97 $result = $this->getRequest()->send(array(
98 'methodName' => 'retargeting.audience.list',
99 'parameters' => array(
100 'accountId' => $this->accountId
101 )
102 ));
103
104 if ($result->isSuccess())
105 {
106 $list = [];
107 $data = $result->getData();
108
109 if (is_array($data['items']))
110 {
111 $list = array_values(array_filter($data['items'], function ($item) {
112 return $item['type'] === self::USER_LIST_TYPE_HUMAN;
113 }));
114 }
115 $result->setData($list);
116 }
117
118 return $result;
119 }
120
121 public static function isSupportAddAudience()
122 {
123 return true;
124 }
125
127 {
128 return [];
129 // $sizes = [];
130 // for ($i=1; $i<10;$i++)
131 // {
132 // $sizes[$i] = $i;
133 // }
134 // return [
135 // 'FIELDS' => ['AUDIENCE_SIZE'],
136 // 'SIZES' => $sizes
137 // ];
138 }
139 public static function isSupportCreateLookalikeFromSegments(): bool
140 {
141 return false;
142 }
143
144 // public function createLookalike($sourceAudienceId, array $options)
145 // {
146 // $result = $this->getRequest()->send(array(
147 // 'methodName' => 'retargeting.audience.lookalike.request.add',
148 // 'parameters' => array(
149 // 'accountId' => $this->accountId,
150 // 'audienceId' => $sourceAudienceId,
151 // )
152 // ));
153 // if (!$result->isSuccess())
154 // {
155 // return $result;
156 // }
157 //
158 // $data = $result->getData();
159 // if ($data['request_id'])
160 // {
161 // $result->setId($data['request_id']);
162 // $this->addLookalikeAudienceAgent($data['request_id'], $options['AUDIENCE_SIZE']);
163 // }
164 // return $result;
165 // }
166
167
168 protected function addLookalikeAudienceAgent($audienceRequestId, $audienceSize)
169 {
170 // \CAgent::AddAgent($this->getLookalikeAudienceAgentName($audienceRequestId, $audienceSize), 'seo', 'N', 60);
171 }
172
173 protected function getLookalikeAudienceAgentName($audienceRequestId, $audienceSize)
174 {
175 $clientId = $this->service->getClientId();
176 $clientId = preg_replace('/[^a-zA-Z0-9_-]/', '', (string)$clientId);
177 $accountId = preg_replace('/[^a-zA-Z0-9_-]/', '', (string)$this->accountId);
178 $audienceRequestId = preg_replace('/[^a-zA-Z0-9_-]/', '', (string)$audienceRequestId);
179 $audienceSize = (int)$audienceSize;
180
181 return __CLASS__ . '::processLookalikeAudienceAgent("'.$clientId.'", "'.$accountId.'", "' . $audienceRequestId . '", "'.$audienceSize.'");';
182 }
183
184 public static function processLookalikeAudienceAgent($clientId, $accountId, $audienceRequestId, $audienceSize)
185 {
187 $service->setClientId($clientId);
188
189 $audience = new static($accountId);
190 $audience->setService($service);
191
192 $result = $audience->getRequest()->send(array(
193 'methodName' => 'retargeting.audience.lookalike.request.get',
194 'parameters' => array(
195 'accountId' => $accountId,
196 'requestId' => $audienceRequestId,
197 )
198 ));
199 if (!$result->isSuccess())
200 {
201 return '';
202 }
203
204 $data = $result->getData();
205 $audienceRequest = array_filter($data['items'],
206 function ($item) use ($audienceRequestId)
207 {
208 return $audienceRequestId == $item['id'];
209 }
210 );
211
212 if (empty($audienceRequest))
213 {
214 return '';
215 }
216
217 $audienceRequest = array_shift($audienceRequest);
218
219 if ($audienceRequest['status'] == 'search_in_progress') // not processed yet
220 {
221 return $audience->getLookalikeAudienceAgentName($audienceRequestId, $audienceSize);
222 }
223 if ($audienceRequest['status'] == 'search_done') // processing complete
224 {
225 $audience->getRequest()->send(array(
226 'methodName' => 'retargeting.audience.lookalike.add',
227 'parameters' => array(
228 'accountId' => $accountId,
229 'requestId' => $audienceRequestId,
230 'level' => $audienceSize
231 )
232 ));
233 }
234 return '';
235 }
236}
removeContacts($audienceId, array $contacts, array $options)
static processLookalikeAudienceAgent($clientId, $accountId, $audienceRequestId, $audienceSize)
getLookalikeAudienceAgentName($audienceRequestId, $audienceSize)
addLookalikeAudienceAgent($audienceRequestId, $audienceSize)
importContacts($audienceId, array $contacts, array $options)