Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
audienceyandex.php
1<?php
2
4
9use \Bitrix\Seo\Retargeting\Audience;
11
13{
14 const TYPE_CODE = 'yandex';
15
18 const URL_AUDIENCE_LIST = 'https://audience.yandex.ru/';
19 private const EXAMPLE_MAIL = 'example@mail.domain';
20
23
24 protected static $listRowMap = array(
25 'ID' => 'ID',
26 'NAME' => 'NAME',
27 'COUNT_VALID' => 'VALID_UNIQUE_QUANTITY',
28 'COUNT_MATCHED' => 'MATCHED_QUANTITY',
29 'HASHED' => 'HASHED',
30 'STATUS' => 'STATUS',
31 'SUPPORTED_CONTACT_TYPES' => array(
32 self::ENUM_CONTACT_TYPE_PHONE,
33 self::ENUM_CONTACT_TYPE_EMAIL
34 ),
35 );
36
37 public static function normalizeListRow(array $row)
38 {
39 $map = array(
40 //'email' => [self::ENUM_CONTACT_TYPE_EMAIL],
41 //'phone' => [self::ENUM_CONTACT_TYPE_PHONE],
42 'crm' => [
43 self::ENUM_CONTACT_TYPE_PHONE,
44 self::ENUM_CONTACT_TYPE_EMAIL
45 ],
46 );
47 static::$listRowMap['SUPPORTED_CONTACT_TYPES'] = $map[$row['CONTENT_TYPE']];
48 return parent::normalizeListRow($row);
49 }
50
51 public static function isSupportMultiTypeContacts()
52 {
53 return true;
54 }
55
56 public static function isSupportAccount()
57 {
58 return false;
59 }
60
61 public static function isAddingRequireContacts()
62 {
63 return true;
64 }
65
66 public static function isSupportRemoveContacts()
67 {
68 return false;
69 }
70
71 public static function isSupportAddAudience()
72 {
73 return true;
74 }
75
76 public static function isSupportCreateLookalikeFromSegments(): bool
77 {
78 return false;
79 }
80
87 public function add(array $data)
88 {
89 $response = $this->request->send(array(
90 'methodName' => 'retargeting.audience.add',
91 'parameters' => array(
92 'name' => $data['NAME'],
93 'hashed' => 0,
94 'contacts' => $this->prepareContacts(["email" => [self::EXAMPLE_MAIL]]),
95 ),
96 ));
97
98 $responseData = $response->getData();
99 if (isset($responseData['id']))
100 {
101 $response->setId($responseData['id']);
102 }
103
104 return $response
105 ;
106 }
107
115 protected function prepareContacts(array $contacts = array(), $hashed = false, $type = '')
116 {
117 // filter by type
118 if ($type)
119 {
120 $contacts = [
121 $type => isset($contacts[$type]) ? $contacts[$type] : []
122 ];
123 }
124
125 // prepare data
126 $eol = "\r\n";
127 $separator = ",";
128 $types = array_keys($contacts);
129 $typeCount = count($types);
130 $result = implode($separator, $types);
131 foreach ($types as $index => $contactType)
132 {
133 foreach ($contacts[$contactType] as $contact)
134 {
135 $contact = $hashed ? md5($contact) : $contact;
136 $data = array_fill(0, $typeCount, "");
137 $data[$index] = $contact;
138 $result .= $eol . implode($separator, $data);
139 }
140 }
141
142 return $result;
143 }
144
152 public function importContacts($audienceId, array $contacts, array $options)
153 {
154 $createNewAudience = ($audienceId == static::NEW_AUDIENCE_FAKE_ID);
155 $audienceData = $this->getById($audienceId);
156 if (!$audienceData)
157 {
158 $result = new Result();
159 $result->addError(new Error('Audience '.$audienceId.' not found'));
160 return $result;
161 }
162
163 $hashed = (bool)$audienceData['HASHED'];
164 $payload = $this->prepareContacts($contacts, $hashed, $options['type']);
165
166 if ($createNewAudience)
167 {
168 $name = $options['audienceName'] ?: Loc::getMessage('SEO_RETARGETING_SERVICE_AUDIENCE_NAME_TEMPLATE', ['#DATE#' => FormatDate('j F')]);
169 $response = $this->getRequest()->send(array(
170 'methodName' => 'retargeting.audience.add',
171 'parameters' => array(
172 'name' => $name,
173 'hashed' => $hashed ? 1 : 0,
174 'contacts' => $payload
175 ),
176 'timeout' => static::UPDATE_AUDIENCE_TIMEOUT
177 ));
178 }
179 else
180 {
181 $response = $this->getRequest()->send(array(
182 'methodName' => 'retargeting.audience.contacts.rewrite',
183 'parameters' => array(
184 'audienceId' => $audienceId,
185 'contacts' => $payload
186 ),
187 'timeout' => static::UPDATE_AUDIENCE_TIMEOUT
188 ));
189 }
190
191 return $response;
192 }
193
202 public function removeContacts($audienceId, array $contacts, array $options)
203 {
204 $response = Response::create(static::TYPE_CODE);
205 return $response;
206 }
207
213 public function getList()
214 {
215 $response = $this->getRequest()->send(array(
216 'methodName' => 'retargeting.audience.list',
217 'parameters' => array()
218 ));
219 $data = $response->getData();
220 if (is_array($data['segments']))
221 {
222 $data = array_values(array_filter($data['segments'], function ($item) {
223 return (
224 $item['type'] == 'uploading' && // based on uploaded data
225 $item['content_type'] == 'crm'// Data from crm
226 );
227 }));
228 }
229 else
230 {
231 $data = [];
232 }
233
234 $data = $this->addNewAudienceValue($data);
235 $response->setData($data);
236 return $response;
237 }
238
244 protected function addNewAudienceValue($data)
245 {
246 array_unshift($data, [
247 'name' => Loc::getMessage("SEO_RETARGETING_SERVICE_AUDIENCE_YANDEX_ADD"),
248 'id' => static::NEW_AUDIENCE_FAKE_ID,
249 'valid_unique_quantity' => '',
250 'matched_quantity' => '',
251 'status' => '',
252 'hashed' => false,
253 ]);
254 return $data;
255 }
256
260 public function getLookalikeAudiencesParams(): array
261 {
262 return [
263 'FIELDS' => [
264 'AUDIENCE_LOOKALIKE',
265 'DEVICE_DISTRIBUTION',
266 'GEO_DISTRIBUTION'
267 ],
268 'AUDIENCE_LOOKALIKE' => [
269 'MIN' => 1,
270 'MAX' => 5,
271 ],
272 ];
273 }
274
281 public function createLookalike($sourceAudienceId, array $options): Response
282 {
283 $result = $this->getRequest()->send([
284 'methodName' => 'retargeting.audience.lookalike.add',
285 'parameters' => [
286 'name' => (string) $options['name'],
287 'lookalike_link' => (int) $sourceAudienceId,
288 'lookalike_value' => (int) $options['lookalike_value'],
289 'maintain_device_distribution' => (bool) $options['maintain_device_distribution'],
290 'maintain_geo_distribution' => (bool) $options['maintain_geo_distribution'],
291 ],
292 ]);
293
294 if ($result->isSuccess())
295 {
296 $result->setId($result->getData()['id']);
297 }
298
299 return $result;
300 }
301}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
removeContacts($audienceId, array $contacts, array $options)
prepareContacts(array $contacts=array(), $hashed=false, $type='')
importContacts($audienceId, array $contacts, array $options)
createLookalike($sourceAudienceId, array $options)