1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
audience.php
См. документацию.
1<?
2
3namespace Bitrix\Seo\Retargeting;
4
5use Bitrix\Main\NotImplementedException;
6use Bitrix\Main\Type\DateTime;
7use Bitrix\Seo\Retargeting\Internals\QueueTable;
8
9abstract class Audience extends BaseApiObject
10{
11 const TYPE_FACEBOOK = 'facebook';
12 const TYPE_VKONTAKTE = 'vkontakte';
13 const TYPE_MYCOM = 'mycom';
14 const TYPE_YANDEX = 'yandex';
15 const TYPE_GOOGLE = 'google';
16
17 const ENUM_CONTACT_TYPE_EMAIL = 'email'; // email
18 const ENUM_CONTACT_TYPE_PHONE = 'phone'; // phone
19 const ENUM_CONTACT_TYPE_IDFA_GAID = 'idfa_gaid'; // IDFA (Identifier For Advertising) or device ID (Android ID and UDID on iOS)
20 const ENUM_CONTACT_TYPE_INTERNAL_ID = 'int'; // internal social net id like Vk ID or Fb ID
21
25
26 protected $accountId;
27 protected $audienceId;
28 protected static $listRowMap = [
29 'ID' => 'ID',
30 'NAME' => 'NAME',
31 'COUNT_VALID' => 'COUNT',
32 'COUNT_MATCHED' => 'COUNT',
33 'SUPPORTED_CONTACT_TYPES' => [
34 self::ENUM_CONTACT_TYPE_EMAIL,
35 self::ENUM_CONTACT_TYPE_PHONE,
36 self::ENUM_CONTACT_TYPE_IDFA_GAID,
37 self::ENUM_CONTACT_TYPE_INTERNAL_ID
38 ],
39 ];
40 protected $isQueueModeEnabled = false;
41 protected $isQueueAutoRemove = true;
42 protected $queueDaysAutoRemove = 7;
43 protected $emptyResponse = null;
44
45 public function __construct($accountId = null)
46 {
47 $this->accountId = $accountId;
48 parent::__construct();
49 }
50
51 public function setAccountId($accountId)
52 {
53 return $this->accountId = $accountId;
54 }
55
56 public static function normalizeEmail($email)
57 {
58 return trim(mb_strtolower($email));
59 }
60
61 public static function normalizePhone($phone)
62 {
63 return preg_replace("/[^\+0-9]/", '', $phone);
64 }
65
66 public static function isSupportMultiTypeContacts()
67 {
68 return true;
69 }
70
71 public static function isSupportAccount()
72 {
73 return true;
74 }
75
76 public static function isSupportAddAudience()
77 {
78 return false;
79 }
80
81 public static function isAddingRequireContacts()
82 {
83 return false;
84 }
85
86 public static function isSupportRemoveContacts()
87 {
88 return true;
89 }
90
91 public static function isSupportCreateLookalikeFromSegments(): bool
92 {
93 return true;
94 }
95
97 {
98 return false;
99 }
100
101 public static function getUrlAudienceList()
102 {
103 return static::URL_AUDIENCE_LIST;
104 }
105
106 public static function getMaxContactsPerPacket()
107 {
108 return static::MAX_CONTACTS_PER_PACKET;
109 }
110
111 public static function getMinContactsForActivating()
112 {
113 return static::MIN_CONTACTS_FOR_ACTIVATING;
114 }
115
116 public function disableQueueAutoRemove()
117 {
118 $this->isQueueAutoRemove = false;
119 }
120
121 public function enableQueueAutoRemove($daysNumber = null)
122 {
123 $this->isQueueAutoRemove = true;
124 if ($daysNumber)
125 {
126 $this->queueDaysAutoRemove = $daysNumber;
127 }
128 }
129
130 public function disableQueueMode()
131 {
132 $this->isQueueModeEnabled = false;
133 }
134
135 public function enableQueueMode()
136 {
137 $this->isQueueModeEnabled = true;
138 }
139
140 public function isQueueModeEnabled()
141 {
142 return $this->isQueueModeEnabled;
143 }
144
145 public function getById($itemId)
146 {
147 $itemsResult = $this->getList();
148 while($itemData = $itemsResult->fetch())
149 {
150 $itemData = $this->normalizeListRow($itemData);
151 if ($itemData['ID'] == $itemId)
152 {
153 return $itemData;
154 }
155 }
156
157 return null;
158 }
159
160 protected function normalizeContacts(array $contacts = array())
161 {
162 $data = array();
163 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
164 {
165 if (!isset($contacts[$contactType]))
166 {
167 continue;
168 }
169
170 $contactsCount = count($contacts[$contactType]);
171 for ($i = 0; $i < $contactsCount; $i++)
172 {
173 if (empty($contacts[$contactType][$i]))
174 {
175 continue;
176 }
177
178 $contactPhone = null;
179 $contact = $contacts[$contactType][$i];
180 switch ($contactType)
181 {
182 case self::ENUM_CONTACT_TYPE_EMAIL:
183 $contact = static::normalizeEmail($contact);
184 break;
185
186 case self::ENUM_CONTACT_TYPE_PHONE:
187 $contact = static::normalizePhone($contact);
188 if (mb_substr($contact, 0, 1) == '8' && mb_strlen($contact) > 8)
189 {
190 $contactPhone = '+7'.mb_substr($contact, 1);
191 }
192 break;
193 }
194
195 if ($contact)
196 {
197 $data[$contactType][] = $contact;
198 }
199
200 if ($contactPhone)
201 {
202 $data[$contactType][] = $contactPhone;
203 }
204 }
205 }
206
207 return $data;
208 }
209
210 protected function addToQueue($audienceId, $contacts, $options = [], $isRemove = false)
211 {
212 $dateAutoRemove = null;
213 if ($this->isQueueAutoRemove && $this->queueDaysAutoRemove > 0)
214 {
215 $dateAutoRemove = new DateTime();
216 $dateAutoRemove->add($this->queueDaysAutoRemove . ' DAY');
217 }
218
219 if ($isRemove)
220 {
222 }
223 else if ($this->isQueueAutoRemove)
224 {
226 }
227 else
228 {
230 }
231
232
233 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
234 {
235 if (!isset($contacts[$contactType]))
236 {
237 continue;
238 }
239
240 $contactsCount = count($contacts[$contactType]);
241 for ($i = 0; $i < $contactsCount; $i++)
242 {
243 $contact = $contacts[$contactType][$i];
244 $resultDb = QueueTable::add(array(
245 'TYPE' => static::TYPE_CODE,
246 'ACCOUNT_ID' => $this->accountId,
247 'CLIENT_ID' => $this->service instanceof IMultiClientService ? $this->service->getClientId() : null,
248 'AUDIENCE_ID' => $audienceId,
249 'PARENT_ID' => $options['parentId'] ?: null,
250 'CONTACT_TYPE' => $contactType,
251 'VALUE' => $contact,
252 'ACTION' => $action,
253 'DATE_AUTO_REMOVE' => $dateAutoRemove,
254 ));
255 $resultDb->isSuccess();
256 }
257 }
258
259 return true;
260 }
261
262 protected function deleteFromQueue($audienceId, $contacts)
263 {
264 foreach (static::$listRowMap['SUPPORTED_CONTACT_TYPES'] as $contactType)
265 {
266 if (!isset($contacts[$contactType]))
267 {
268 continue;
269 }
270
271 $contactsCount = count($contacts[$contactType]);
272 for ($i = 0; $i < $contactsCount; $i++)
273 {
274 $contact = $contacts[$contactType][$i];
275 $itemDb = QueueTable::getList(array(
276 'select' => array('ID'),
277 'filter' => array(
278 'TYPE' => static::TYPE_CODE,
279 'ACCOUNT_ID' => $this->accountId,
280 'AUDIENCE_ID' => $audienceId,
281 'CONTACT_TYPE' => $contactType,
282 'VALUE' => $contact,
283 )
284 ));
285 while ($item = $itemDb->fetch())
286 {
287 $result = QueueTable::delete($item['ID']);
288 $result->isSuccess();
289 }
290 }
291 }
292
293 return true;
294 }
295
302 public function addContacts($audienceId, array $contacts, array $options)
303 {
304 $contacts = $this->normalizeContacts($contacts);
305 if ($this->isQueueModeEnabled())
306 {
307 $this->addToQueue($audienceId, $contacts, $options, false);
308 if ($this->emptyResponse === null)
309 {
310 $this->emptyResponse = Response::create(static::TYPE_CODE);
311 $this->emptyResponse->setData(array());
312 }
313
314 return $this->emptyResponse;
315 }
316 else
317 {
318 return $this->importContacts($audienceId, $contacts, $options);
319 }
320 }
321
328 public function deleteContacts($audienceId, array $contacts, array $options)
329 {
330 if ($this->isQueueModeEnabled())
331 {
332 $this->addToQueue($audienceId, $contacts, $options, true);
333 $response = Response::create(static::TYPE_CODE);
334 $response->setData(array());
335 return $response;
336 }
337 else
338 {
339 return $this->removeContacts($audienceId, $contacts, $options);
340 }
341 }
342
349 abstract public function add(array $data);
350
356 abstract public function getList();
357
358
367 abstract protected function importContacts($audienceId, array $contacts, array $options);
368
369
378 abstract protected function removeContacts($audienceId, array $contacts, array $options);
379
380 public function createLookalike($sourceAudienceId, array $options)
381 {
382 throw new NotImplementedException('Method '.static::class.'::`addLookalike()` not implemented.');
383 }
384
385 public function isQueueProcessed($parentId)
386 {
387 return !QueueTable::getCount(['=PARENT_ID' => $parentId]);
388 }
389}
const ENUM_CONTACT_TYPE_INTERNAL_ID
Определения audience.php:20
const ENUM_CONTACT_TYPE_IDFA_GAID
Определения audience.php:19
static $listRowMap
Определения audience.php:28
const TYPE_VKONTAKTE
Определения audience.php:12
deleteContacts($audienceId, array $contacts, array $options)
Определения audience.php:328
removeContacts($audienceId, array $contacts, array $options)
const TYPE_YANDEX
Определения audience.php:14
static getMaxContactsPerPacket()
Определения audience.php:106
static isAddingRequireContacts()
Определения audience.php:81
static normalizePhone($phone)
Определения audience.php:61
deleteFromQueue($audienceId, $contacts)
Определения audience.php:262
addToQueue($audienceId, $contacts, $options=[], $isRemove=false)
Определения audience.php:210
__construct($accountId=null)
Определения audience.php:45
setAccountId($accountId)
Определения audience.php:51
disableQueueAutoRemove()
Определения audience.php:116
static isSupportMultiTypeContacts()
Определения audience.php:66
enableQueueAutoRemove($daysNumber=null)
Определения audience.php:121
const TYPE_MYCOM
Определения audience.php:13
isQueueProcessed($parentId)
Определения audience.php:385
const ENUM_CONTACT_TYPE_PHONE
Определения audience.php:18
static normalizeEmail($email)
Определения audience.php:56
static isSupportAddAudience()
Определения audience.php:76
static getUrlAudienceList()
Определения audience.php:101
static isSupportAccount()
Определения audience.php:71
importContacts($audienceId, array $contacts, array $options)
const TYPE_GOOGLE
Определения audience.php:15
normalizeContacts(array $contacts=array())
Определения audience.php:160
static isSupportRemoveContacts()
Определения audience.php:86
const URL_AUDIENCE_LIST
Определения audience.php:24
const MIN_CONTACTS_FOR_ACTIVATING
Определения audience.php:23
addContacts($audienceId, array $contacts, array $options)
Определения audience.php:302
const MAX_CONTACTS_PER_PACKET
Определения audience.php:22
getLookalikeAudiencesParams()
Определения audience.php:96
static isSupportCreateLookalikeFromSegments()
Определения audience.php:91
static getMinContactsForActivating()
Определения audience.php:111
const ENUM_CONTACT_TYPE_EMAIL
Определения audience.php:17
createLookalike($sourceAudienceId, array $options)
Определения audience.php:380
const TYPE_FACEBOOK
Определения audience.php:11
getById($itemId)
Определения audience.php:145
static normalizeListRow(array $row)
Определения baseapiobject.php:17
const ACTION_IMPORT_AND_AUTO_REMOVE
Определения queue.php:39
static create($type)
Определения response.php:108
$options
Определения commerceml2.php:49
$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
$email
Определения payment.php:49
$i
Определения factura.php:643
</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
$response
Определения result.php:21
$action
Определения file_dialog.php:21