1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
accountvkontakte.php
См. документацию.
1
<?
2
3
namespace
Bitrix\Seo\Analytics\Services;
4
5
use Bitrix\Seo\Analytics\Internals\Expenses;
6
use Bitrix\Main\Web\Json;
7
use Bitrix\Main\Web\Uri;
8
use Bitrix\Main\Type\Date;
9
use Bitrix\Seo\Analytics\Internals\Page;
10
use Bitrix\Seo\Retargeting\Response;
11
use Bitrix\Seo\Retargeting\Services\ResponseVkontakte;
12
use Bitrix\Seo\Retargeting\IRequestDirectly;
13
14
class
AccountVkontakte
extends
\Bitrix\Seo\Analytics\Account
implements
IRequestDirectly
15
{
16
const
TYPE_CODE
=
'vkontakte'
;
17
18
const
MAX_ADS_EDIT
= 20;
19
const
CURRENCY_CODE
=
'RUB'
;
20
21
protected
static
$listRowMap
=
array
(
22
'ID'
=>
'ACCOUNT_ID'
,
23
'NAME'
=>
'NAME'
,
24
);
25
26
public
function
getList
()
27
{
28
// https://vk.com/dev/ads.getAccounts
29
30
$result
= $this->
getRequest
()->send([
31
'method'
=>
'GET'
,
32
'endpoint'
=>
'ads.getAccounts'
,
33
]);
34
if
(
$result
->isSuccess())
35
{
36
$list = [];
37
while
($item =
$result
->fetch())
38
{
39
if
($item[
'ACCOUNT_TYPE'
] ===
'general'
)
40
{
41
$list[] = $item;
42
}
43
}
44
$result
->setData($list);
45
}
46
47
return
$result
;
48
}
49
50
public
function
getProfile
()
51
{
52
$response
= $this->
getRequest
()->send(
array
(
53
'method'
=>
'GET'
,
54
'endpoint'
=>
'users.get'
,
55
'fields'
=>
array
(
56
//'user_ids' => array(),
57
'fields'
=>
'photo_50,screen_name'
58
)
59
));
60
61
62
if
(
$response
->isSuccess())
63
{
64
$data
=
$response
->fetch();
65
return
array
(
66
'ID'
=>
$data
[
'ID'
],
67
'NAME'
=>
$data
[
'FIRST_NAME'
] .
' '
.
$data
[
'LAST_NAME'
],
68
'LINK'
=>
'https://vk.com/'
.
$data
[
'SCREEN_NAME'
],
69
'PICTURE'
=>
$data
[
'PHOTO_50'
],
70
);
71
}
72
else
73
{
74
return
null
;
75
}
76
}
77
84
public
function
getExpenses
($accountId,
Date
$dateFrom =
null
,
Date
$dateTo =
null
)
85
{
86
$result
=
new
ResponseVkontakte
();
87
$fields
= [
88
'account_id'
=> $accountId,
89
'ids_type'
=>
'office'
,
90
'ids'
=> $accountId,
91
];
92
93
if
($dateFrom && $dateTo)
94
{
95
$fields
[
'period'
] =
'day'
;
96
$fields
[
'date_from'
] = $dateFrom->format(
'Y-m-d'
);
97
$fields
[
'date_to'
] = $dateTo->format(
'Y-m-d'
);
98
}
99
else
100
{
101
$fields
[
'period'
] =
'overall'
;
102
$fields
[
'date_from'
] =
'0'
;
103
$fields
[
'date_to'
] =
'0'
;
104
}
105
$response
= $this->
getRequest
()->send([
106
'method'
=>
'GET'
,
107
'endpoint'
=>
'ads.getStatistics'
,
108
'fields'
=>
$fields
,
109
]);
110
if
(
$response
->isSuccess())
111
{
112
$data
=
$response
->getData();
113
if
(isset(
$data
[0]))
114
{
115
$data
=
$data
[0];
116
}
117
$expenses =
new
Expenses
();
118
foreach
(
$data
[
'stats'
] as $stat)
119
{
120
$expenses->add([
121
'impressions'
=> $stat[
'impressions'
],
122
'clicks'
=> $stat[
'clicks'
],
123
'actions'
=> $stat[
'clicks'
],
124
'spend'
=> $stat[
'spent'
],
125
'currency'
=> static::CURRENCY_CODE,
126
]);
127
}
128
$result
->setData([
'expenses'
=> $expenses]);
129
}
130
else
131
{
132
$result
->addErrors(
$response
->getErrors());
133
}
134
135
return
$result
;
136
}
137
141
public
function
hasPublicPages
()
142
{
143
return
true
;
144
}
145
151
public
function
getPublicPages
($accountId)
152
{
153
$adsLayoutResult = $this->
getAdsLayout
($accountId);
154
if
(!$adsLayoutResult->isSuccess())
155
{
156
return
$adsLayoutResult;
157
}
158
$groupIDs = [];
159
$ads = $adsLayoutResult->getData();
160
foreach
($ads as $ad)
161
{
162
if
(isset($ad[
'group_id'
]))
163
{
164
$groupIDs[] = $ad[
'group_id'
];
165
}
166
}
167
168
$response
= $this->
getGroups
($groupIDs);
169
if
(!
$response
->isSuccess())
170
{
171
return
$response
;
172
}
173
$result
= [];
174
$groups
=
$response
->getData();
175
foreach
(
$groups
as
$page
)
176
{
177
$result
[] =
new
Page
([
178
'id'
=>
$page
[
'id'
],
179
'name'
=>
$page
[
'name'
],
180
'about'
=>
$page
[
'description'
],
181
'image'
=>
$page
[
'photo_200'
],
182
'phone'
=>
$page
[
'phone'
],
183
]);
184
}
185
$response
->setData(
$result
);
186
187
return
$response
;
188
}
189
197
public
function
updateAnalyticParams
($accountId,
array
$params
,
array
$publicPageIds = [])
198
{
199
$result
=
new
ResponseVkontakte
();
200
if
(empty(
$params
))
201
{
202
return
$result
;
203
}
204
$result
= $this->
getAdsLayout
($accountId);
205
if
(!
$result
->isSuccess() || empty(
$result
->getData()))
206
{
207
return
$result
;
208
}
209
210
$data
= [];
211
$ads =
$result
->getData();
212
$groupIDs = $postIDs = [];
213
foreach
($ads as $ad)
214
{
215
if
(isset($ad[
'group_id'
]) && in_array($ad[
'group_id'
], $publicPageIds))
216
{
217
$groupIDs[] = $ad[
'group_id'
];
218
}
219
if
(isset($ad[
'post_id'
]))
220
{
221
$postIDs[] = $ad[
'post_id'
];
222
}
223
if
(!empty(
$params
[
'url_tags'
]) && isset($ad[
'link_domain'
]) && !empty($ad[
'link_domain'
]) && !empty($ad[
'link_url'
]))
224
{
225
$url
=
new
Uri
($ad[
'link_url'
]);
226
$url
->addParams(
$params
[
'url_tags'
]);
227
$data
[] = [
228
'ad_id'
=> $ad[
'id'
],
229
'link_url'
=>
$url
->getUri(),
230
];
231
}
232
233
if
(
count
(
$data
) == self::MAX_ADS_EDIT)
234
{
235
$editAdsResult = $this->
editAds
($accountId,
$data
);
236
if
(!$editAdsResult->isSuccess())
237
{
238
$result
->addErrors($editAdsResult->getErrors());
239
}
240
$data
= [];
241
}
242
}
243
244
if
(!empty(
$data
))
245
{
246
$editAdsResult = $this->
editAds
($accountId,
$data
);
247
if
(!$editAdsResult->isSuccess())
248
{
249
$result
->addErrors($editAdsResult->getErrors());
250
}
251
}
252
253
//edit groups
254
if
(!empty($groupIDs))
255
{
256
$result
= $this->
editGroupAnalyticParams
($groupIDs,
$params
);
257
if
(!
$result
->isSuccess())
258
{
259
return
$result
;
260
}
261
}
262
//edit posts
263
if
(!empty($postIDs))
264
{
265
$result
= $this->
editPostAnalyticParams
($postIDs,
$params
);
266
}
267
268
return
$result
;
269
}
270
277
protected
function
editAds
($accountId,
array
$data
)
278
{
279
return
$this->
getRequest
()->send([
280
'method'
=>
'POST'
,
281
'endpoint'
=>
'ads.updateAds'
,
282
'fields'
=> [
283
'account_id'
=> $accountId,
284
'data'
=> Json::encode(
$data
)
285
]
286
]);
287
}
288
294
protected
function
getAdsLayout
($accountId)
295
{
296
$fields
= [
297
'account_id'
=> $accountId,
298
'include_deleted'
=>
'0'
,
299
];
300
301
$response
= $this->
getRequest
()->send([
302
'method'
=>
'GET'
,
303
'endpoint'
=>
'ads.getAdsLayout'
,
304
'fields'
=>
$fields
,
305
]);
306
307
if
(
$response
->isSuccess())
308
{
309
$ads =
$response
->getData();
310
foreach
($ads as &$ad)
311
{
312
if
(isset($ad[
'link_url'
]))
313
{
314
$parsedData = $this->
parseVkUrl
($ad[
'link_url'
]);
315
if
($parsedData)
316
{
317
$ad += $parsedData;
318
}
319
}
320
}
321
$response
->setData($ads);
322
}
323
324
return
$response
;
325
}
326
333
protected
function
editGroupAnalyticParams
(
array
$groupIDs,
array
$params
)
334
{
335
$result
= $this->
getGroups
(array_values($groupIDs));
336
if
(!
$result
->isSuccess())
337
{
338
return
$result
;
339
}
340
$groups
=
$result
->getData();
341
foreach
(
$groups
as $group)
342
{
343
$data
= [];
344
if
(isset(
$params
[
'url_tags'
]) && !empty(
$params
[
'url_tags'
]) && isset($group[
'site'
]))
345
{
346
$uri
=
new
Uri
($group[
'site'
]);
347
$uri
->addParams(
$params
[
'url_tags'
]);
348
$data
[
'website'
] =
$uri
->getUri();
349
}
350
if
(isset(
$params
[
'phone'
]) && !empty(
$params
[
'phone'
]) && $group[
'phone'
] !=
$params
[
'phone'
])
351
{
352
$data
[
'phone'
] =
$params
[
'phone'
];
353
}
354
if
(!empty(
$data
))
355
{
356
$data
[
'id'
] = $group[
'id'
];
357
$response
= $this->
getRequest
()->send([
358
'method'
=>
'POST'
,
359
'endpoint'
=>
'groups.edit'
,
360
'fields'
=>
$data
,
361
]);
362
if
(!
$response
->isSuccess())
363
{
364
$result
->addErrors(
$response
->getErrors());
365
}
366
}
367
368
if
(isset(
$params
[
'url_tags'
]) && !empty(
$params
[
'url_tags'
]) && isset($group[
'links'
]) && is_array($group[
'links'
]) && !empty($group[
'links'
]))
369
{
370
foreach
($group[
'links'
] as $link)
371
{
372
$url
=
new
Uri
($link[
'url'
]);
373
$url
->addParams(
$params
[
'url_tags'
]);
374
if
(
$url
->getUri() != $link[
'url'
])
375
{
376
$response
= $this->
getRequest
()->send([
377
'method'
=>
'POST'
,
378
'emdpoint'
=>
'groups.deleteLink'
,
379
'fields'
=> [
380
'group_id'
=> $group[
'id'
],
381
'link_id'
=> $link[
'id'
],
382
]
383
]);
384
if
(
$response
->isSuccess())
385
{
386
$response
= $this->
getRequest
()->send([
387
'method'
=>
'POST'
,
388
'endpoint'
=>
'groups.addLink'
,
389
'fields'
=> [
390
'group_id'
=> $group[
'id'
],
391
'text'
=> $link[
'desc'
],
392
'link'
=>
$url
->getUri(),
393
]
394
]);
395
}
396
if
(!
$response
->isSuccess())
397
{
398
$result
->addErrors(
$response
->getErrors());
399
}
400
}
401
}
402
}
403
404
// todo add here edit call_to_action button
405
}
406
407
return
$result
;
408
}
409
415
protected
function
getGroups
(
array
$groupIDs)
416
{
417
return
$this->
getRequest
()->send([
418
'method'
=>
'GET'
,
419
'endpoint'
=>
'groups.getById'
,
420
'fields'
=> [
421
'group_ids'
=> implode(
','
, $groupIDs),
422
'fields'
=>
'name,type,id,links,site,status,description,phone'
423
]
424
]);
425
}
426
431
protected
function
parseVkUrl
(
$url
)
432
{
433
if
(preg_match(
'#vk\.com\/wall-(\d+)_(\d+)#'
,
$url
,
$matches
) &&
count
(
$matches
) > 2)
434
{
435
return
[
436
'group_id'
=>
$matches
[1],
437
'post_id'
=>
'-'
.$matches[1].
'_'
.
$matches
[2],
438
];
439
}
440
441
return
false
;
442
}
443
450
protected
function
editPostAnalyticParams
(
array
$postIDs,
array
$params
)
451
{
452
$result
= $this->
getPosts
($postIDs);
453
if
(!
$result
->isSuccess())
454
{
455
return
$result
;
456
}
457
458
$posts =
$result
->getData();
459
foreach
($posts as
$post
)
460
{
461
if
(
$post
[
'post_type'
] ==
'post_ads'
&& isset(
$post
[
'attachments'
]) && is_array(
$post
[
'attachments'
]) &&
count
(
$post
[
'attachments'
]) == 1)
462
{
463
$attachment = reset(
$post
[
'attachments'
]);
464
if
($attachment[
'type'
] !=
'link'
|| mb_strpos($attachment[
'link'
][
'url'
],
'vk.com'
) !==
false
)
465
{
466
continue
;
467
}
468
$data
= [];
469
if
(isset(
$params
[
'phone'
]) && !empty(
$params
[
'phone'
]) && mb_strpos($attachment[
'link'
][
'url'
],
'tel:'
) === 0)
470
{
471
$data
= [
472
'owner_id'
=>
$post
[
'owner_id'
],
473
'post_id'
=>
$post
[
'id'
],
474
'attachments'
=>
'tel:'
.$params[
'phone'
],
475
];
476
}
477
elseif
(isset(
$params
[
'url_tags'
]) && !empty(
$params
[
'url_tags'
]))
478
{
479
$url
=
new
Uri
($attachment[
'link'
][
'url'
]);
480
$url
->addParams(
$params
[
'url_tags'
]);
481
if
(
$url
->getUri() != $attachment[
'link'
][
'url'
])
482
{
483
$data
= [
484
'owner_id'
=>
$post
[
'owner_id'
],
485
'post_id'
=>
$post
[
'id'
],
486
'attachments'
=>
$url
->getUri(),
487
];
488
}
489
}
490
if
(!empty(
$data
))
491
{
492
$response
= $this->
getRequest
()->send([
493
'method'
=>
'POST'
,
494
'endpoint'
=>
'wall.editAdsStealth'
,
495
'fields'
=>
$data
,
496
]);
497
if
(!
$response
->isSuccess())
498
{
499
$result
->addErrors(
$response
->getErrors());
500
}
501
}
502
}
503
}
504
505
return
$result
;
506
}
507
513
protected
function
getPosts
(
array
$postIDs)
514
{
515
return
$this->
getRequest
()->send([
516
'method'
=>
'GET'
,
517
'endpoint'
=>
'wall.getById'
,
518
'fields'
=> [
519
'posts'
=> implode(
','
, array_values($postIDs)),
520
'fields'
=>
'id,attachments'
,
521
]
522
]);
523
}
524
}
Bitrix\Main\Type\Date
Определения
date.php:9
Bitrix\Main\Web\Uri
Определения
uri.php:17
Bitrix\Seo\Analytics\Account
Определения
account.php:12
Bitrix\Seo\Analytics\Internals\Expenses
Определения
expenses.php:8
Bitrix\Seo\Analytics\Services\AccountVkontakte
Определения
accountvkontakte.php:15
Bitrix\Seo\Analytics\Services\AccountVkontakte\$listRowMap
static $listRowMap
Определения
accountvkontakte.php:21
Bitrix\Seo\Analytics\Services\AccountVkontakte\TYPE_CODE
const TYPE_CODE
Определения
accountvkontakte.php:16
Bitrix\Seo\Analytics\Services\AccountVkontakte\MAX_ADS_EDIT
const MAX_ADS_EDIT
Определения
accountvkontakte.php:18
Bitrix\Seo\Analytics\Services\AccountVkontakte\editGroupAnalyticParams
editGroupAnalyticParams(array $groupIDs, array $params)
Определения
accountvkontakte.php:333
Bitrix\Seo\Analytics\Services\AccountVkontakte\CURRENCY_CODE
const CURRENCY_CODE
Определения
accountvkontakte.php:19
Bitrix\Seo\Analytics\Services\AccountVkontakte\updateAnalyticParams
updateAnalyticParams($accountId, array $params, array $publicPageIds=[])
Определения
accountvkontakte.php:197
Bitrix\Seo\Analytics\Services\AccountVkontakte\editPostAnalyticParams
editPostAnalyticParams(array $postIDs, array $params)
Определения
accountvkontakte.php:450
Bitrix\Seo\Analytics\Services\AccountVkontakte\getAdsLayout
getAdsLayout($accountId)
Определения
accountvkontakte.php:294
Bitrix\Seo\Analytics\Services\AccountVkontakte\parseVkUrl
parseVkUrl($url)
Определения
accountvkontakte.php:431
Bitrix\Seo\Analytics\Services\AccountVkontakte\editAds
editAds($accountId, array $data)
Определения
accountvkontakte.php:277
Bitrix\Seo\Analytics\Services\AccountVkontakte\getList
getList()
Определения
accountvkontakte.php:26
Bitrix\Seo\Analytics\Services\AccountVkontakte\getPosts
getPosts(array $postIDs)
Определения
accountvkontakte.php:513
Bitrix\Seo\Analytics\Services\AccountVkontakte\getGroups
getGroups(array $groupIDs)
Определения
accountvkontakte.php:415
Bitrix\Seo\Analytics\Services\AccountVkontakte\getPublicPages
getPublicPages($accountId)
Определения
accountvkontakte.php:151
Bitrix\Seo\Analytics\Services\AccountVkontakte\getProfile
getProfile()
Определения
accountvkontakte.php:50
Bitrix\Seo\Analytics\Services\AccountVkontakte\hasPublicPages
hasPublicPages()
Определения
accountvkontakte.php:141
Bitrix\Seo\Analytics\Services\AccountVkontakte\getExpenses
getExpenses($accountId, Date $dateFrom=null, Date $dateTo=null)
Определения
accountvkontakte.php:84
Bitrix\Seo\Retargeting\BaseApiObject\getRequest
getRequest()
Определения
baseapiobject.php:44
Bitrix\Seo\Retargeting\Services\ResponseVkontakte
Определения
responsevkontakte.php:12
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$result
$result
Определения
get_property_values.php:14
Bitrix\Seo\Retargeting\IRequestDirectly
Определения
irequestdirectly.php:6
$uri
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения
urlrewrite.php:61
$groups
$groups
Определения
options.php:30
Bitrix\Main\Page
Определения
aliases.php:54
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$page
$page
Определения
order_form.php:33
count
</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
$params
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения
template.php:799
$post
$post
Определения
template.php:8
$response
$response
Определения
result.php:21
$matches
$matches
Определения
index.php:22
$url
$url
Определения
iframe.php:7
$fields
$fields
Определения
yandex_run.php:501
bitrix
modules
seo
lib
analytics
services
accountvkontakte.php
Создано системой
1.14.0