Created
February 19, 2020 11:38
-
-
Save dvygolov/922d7dbc59eb36fa0219037bb1705504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Filters; | |
use Core\Filter\AbstractFilter; | |
use Core\Locale\LocaleService; | |
use Traffic\Model\StreamFilter; | |
use Traffic\RawClick; | |
/* | |
Кастомный фильтр для Кейтаро для работы с капами по офферам. | |
Скопировать файл фильтра в папку application\filters затем перелогиниться в трекер | |
Устанавливаете фильтр в потоке, в поле пишите кап. | |
Фильтр проверяет общее кол-во лидов по ВСЕМ офферам в потоке | |
Если общее кол-во продаж за сегодня по этим офферам меньше заданного капа - пропускаем траф. | |
©2020 by Yellow Web | |
*/ | |
class ywbcapfilter extends AbstractFilter | |
{ | |
public function getModes() | |
{ | |
return [ | |
StreamFilter::ACCEPT => LocaleService::t('filters.binary_options.' . StreamFilter::ACCEPT), | |
StreamFilter::REJECT => LocaleService::t('filters.binary_options.' . StreamFilter::REJECT), | |
]; | |
} | |
public function getTemplate() | |
{ | |
return '<input class="form-control" ng-model="filter.payload" />'; | |
} | |
public function isPass(StreamFilter $filter, RawClick $rawClick) | |
{ | |
$apiKey="<YOUR_APIKEY>"; | |
$apiAddress="http://<YOUR_TRACKER_ADDRESS>/admin_api/v1/"; | |
//запрашиваем все данные по потоку, чтобы вынуть из него идентификаторы офферов | |
$fullAddress=$apiAddress.'streams/'.$filter->getStreamId(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $fullAddress); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Api-Key: '.$apiKey)); | |
$res=curl_exec($ch); | |
$streamParams=json_decode($res,true); | |
//вынимаем идентификаторы офферов | |
$offerIds=[]; | |
foreach($streamParams['offers'] as $offer) | |
{ | |
array_push($offerIds,$offer['offer_id']); | |
} | |
//запрашиваем отчёт по кол-ву продаж у наших офферов за сегодня | |
$ch = curl_init(); | |
//здесь меняем пояс, если ваш часовой пояс не Москва!!! | |
$tz='Europe/Moscow'; | |
date_default_timezone_set($tz); | |
$params = [ | |
'columns' => [], | |
'metrics' => ['sales'], | |
'filters' => [ | |
['name' => 'offer_id', 'operator' => 'IN_LIST', 'expression' => $offerIds] | |
], | |
'grouping' => ['offer'], | |
'range' => [ | |
'timezone' => $tz, | |
'from' => date('Y-m-d'), | |
'to' => date('Y-m-d') | |
] | |
]; | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $apiAddress.'report/build'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Api-Key: '.$apiKey)); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); | |
$res=curl_exec($ch); | |
$report=json_decode($res,true); | |
$totalSales=0; | |
foreach($report['rows'] as $row) | |
{ | |
$totalSales+= $row['sales']; | |
} | |
//взяли кап из настроек фильтра | |
$cap = $filter->getPayload(); | |
return ($filter->getMode() == StreamFilter::ACCEPT && $totalSales<$cap) | |
|| ($filter->getMode() == StreamFilter::REJECT && $totalSales>=$cap); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment