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