Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexstat.php
1<?php
2namespace Bitrix\Seo\Adv;
3
9
46class YandexStatTable extends Entity\DataManager
47{
53 public static function getTableName()
54 {
55 return 'b_seo_yandex_direct_stat';
56 }
57
63 public static function getMap()
64 {
65 return array(
66 'ID' => array(
67 'data_type' => 'integer',
68 'primary' => true,
69 'autocomplete' => true,
70 ),
71 'CAMPAIGN_ID' => array(
72 'data_type' => 'integer',
73 'required' => true,
74 ),
75 'BANNER_ID' => array(
76 'data_type' => 'integer',
77 'required' => true,
78 ),
79 'DATE_DAY' => array(
80 'data_type' => 'date',
81 'required' => true,
82 ),
83 'CURRENCY' => array(
84 'data_type' => 'string',
85 ),
86 'SUM' => array(
87 'data_type' => 'float',
88 ),
89 'SUM_SEARCH' => array(
90 'data_type' => 'float',
91 ),
92 'SUM_CONTEXT' => array(
93 'data_type' => 'float',
94 ),
95 'CLICKS' => array(
96 'data_type' => 'integer',
97 ),
98 'CLICKS_SEARCH' => array(
99 'data_type' => 'integer',
100 ),
101 'CLICKS_CONTEXT' => array(
102 'data_type' => 'integer',
103 ),
104 'SHOWS' => array(
105 'data_type' => 'integer',
106 ),
107 'SHOWS_SEARCH' => array(
108 'data_type' => 'integer',
109 ),
110 'SHOWS_CONTEXT' => array(
111 'data_type' => 'integer',
112 ),
113 'CAMPAIGN' => array(
114 'data_type' => 'Bitrix\Seo\Adv\YandexCampaignTable',
115 'reference' => array('=this.CAMPAIGN_ID' => 'ref.ID'),
116 ),
117 'BANNER' => array(
118 'data_type' => 'Bitrix\Seo\Adv\YandexBannerTable',
119 'reference' => array('=this.BANNER_ID' => 'ref.ID'),
120 ),
121 );
122 }
123
124 public static function getBannerStat($bannerId, $dateStart, $dateFinish)
125 {
126 $result = array();
127
128 $dbRes = static::getList(array(
129 'order' => array(
130 'BANNER_ID' => 'ASC',
131 'DATE_DAY' => 'ASC',
132 ),
133 'filter' => array(
134 '=BANNER_ID' => $bannerId,
135 '>=DATE_DAY' => $dateStart,
136 '<=DATE_DAY' => $dateFinish,
137 )
138 ));
139
140 while($statEntry = $dbRes->fetch())
141 {
142 $result[$statEntry['DATE_DAY']->toString()] = $statEntry;
143 }
144
145 return $result;
146 }
147
148 public static function getCampaignStat($campaignId, $dateStart, $dateFinish)
149 {
150 $result = array();
151
152 $dbRes = static::getList(array(
153 'order' => array(
154 'DATE_DAY' => 'ASC',
155 ),
156 'group' => array('CAMPAIGN_ID', 'DATE_DAY', 'CURRENCY'),
157 'filter' => array(
158 '=CAMPAIGN_ID' => $campaignId,
159 '>=DATE_DAY' => $dateStart,
160 '<=DATE_DAY' => $dateFinish,
161 ),
162 'select' => array(
163 'CAMPAIGN_ID', 'DATE_DAY', 'CURRENCY',
164 'CAMPAIGN_SUM', 'CAMPAIGN_SUM_SEARCH', 'CAMPAIGN_SUM_CONTEXT',
165 'CAMPAIGN_SHOWS', 'CAMPAIGN_SHOWS_SEARCH', 'CAMPAIGN_SHOWS_CONTEXT',
166 'CAMPAIGN_CLICKS', 'CAMPAIGN_CLICKS_SEARCH', 'CAMPAIGN_CLICKS_CONTEXT',
167 ),
168 'runtime' => array(
169 new Entity\ExpressionField('CAMPAIGN_SUM', 'SUM(SUM)'),
170 new Entity\ExpressionField('CAMPAIGN_SUM_SEARCH', 'SUM(SUM_SEARCH)'),
171 new Entity\ExpressionField('CAMPAIGN_SUM_CONTEXT', 'SUM(SUM_CONTEXT)'),
172 new Entity\ExpressionField('CAMPAIGN_SHOWS', 'SUM(SHOWS)'),
173 new Entity\ExpressionField('CAMPAIGN_SHOWS_SEARCH', 'SUM(SHOWS_SEARCH)'),
174 new Entity\ExpressionField('CAMPAIGN_SHOWS_CONTEXT', 'SUM(SHOWS_CONTEXT)'),
175 new Entity\ExpressionField('CAMPAIGN_CLICKS', 'SUM(CLICKS)'),
176 new Entity\ExpressionField('CAMPAIGN_CLICKS_SEARCH', 'SUM(CLICKS_SEARCH)'),
177 new Entity\ExpressionField('CAMPAIGN_CLICKS_CONTEXT', 'SUM(CLICKS_CONTEXT)'),
178 ),
179 ));
180
181 while($statEntry = $dbRes->fetch())
182 {
183 $result[$statEntry['DATE_DAY']->toString()] = $statEntry;
184 }
185
186 return $result;
187 }
188
189 public static function loadBannerStat($bannerId, $dateStart, $dateFinish)
190 {
191 $directEngine = new YandexDirect();
192
193 $dbRes = YandexBannerTable::getList(array(
194 'filter' => array(
195 '=ID' => $bannerId,
196 '=ENGINE_ID' => $directEngine->getId()
197 ),
198 'select' => array(
199 'ID', 'CAMPAIGN_ID',
200 'CAMPAIGN_XML_ID' => 'CAMPAIGN.XML_ID'
201 )
202 ));
203
204 $banner = $dbRes->fetch();
205 if($banner)
206 {
207 $result = static::loadStat($directEngine, $banner['CAMPAIGN_XML_ID'], $dateStart, $dateFinish);
208 if($result['Stat'])
209 {
210 static::processStatsResult($banner['CAMPAIGN_ID'], $result, $directEngine);
211 return true;
212 }
213 }
214
215 return false;
216 }
217
218 public static function loadCampaignStat($campaignId, $dateStart, $dateFinish)
219 {
220 $directEngine = new YandexDirect();
221
222 $dbRes = YandexCampaignTable::getList(array(
223 'filter' => array(
224 '=ID' => $campaignId,
225 '=ENGINE_ID' => $directEngine->getId()
226 ),
227 'select' => array(
228 'ID', 'XML_ID'
229 )
230 ));
231
232 $campaign = $dbRes->fetch();
233 if($campaign)
234 {
235 $result = static::loadStat($directEngine, $campaign['XML_ID'], $dateStart, $dateFinish);
236 if($result['Stat'])
237 {
238 static::processStatsResult($campaignId, $result, $directEngine);
239 return true;
240 }
241 }
242
243 return false;
244 }
245
246 protected static function loadStat(YandexDirect $directEngine, $campaignXmlId, $dateStart, $dateFinish, $skipCurrency = false)
247 {
248 $dateStart = new Date($dateStart);
249 $dateFinish = new Date($dateFinish);
250
251 $queryData = array(
252 "CampaignID" => $campaignXmlId,
253 "StartDate" => $dateStart->format("Y-m-d"),
254 'EndDate' => $dateFinish->format("Y-m-d"),
255 'GroupByColumns' => array(
256 'clDate', 'clBanner'
257 ),
258 );
259
260 $currency = '';
261 if(!$skipCurrency && Loader::includeModule('currency'))
262 {
263 $baseCurrency = \CCurrency::GetBaseCurrency();
264 if($baseCurrency == 'RUR')
265 {
266 $baseCurrency = 'RUB';
267 }
268
269 if(in_array($baseCurrency, $directEngine->allowedCurrency))
270 {
271 $currency = $baseCurrency;
272 }
273 }
274
275 if($currency != '')
276 {
277 $queryData['Currency'] = $currency;
278 }
279
280 try
281 {
282 $result = $directEngine->getBannerStats($queryData);
283 $result['Currency'] = $currency;
284 }
285 catch(YandexDirectException $e)
286 {
287 if($currency != '' && $e->getCode() == YandexDirect::ERROR_WRONG_CURRENCY)
288 {
289 $result = static::loadStat($directEngine, $campaignXmlId, $dateStart, $dateFinish, true);
290 }
291 else
292 {
293 throw $e;
294 }
295 }
296
297 return $result;
298 }
299
300 protected static function processStatsResult($campaignId, array $result, YandexDirect $directEngine)
301 {
302 if($result['Stat'])
303 {
304 $bannerIds = array();
305 foreach($result['Stat'] as $statEntry)
306 {
307 $bannerIds[] = $statEntry['BannerID'];
308 }
309
310 if(count($bannerIds) > 0)
311 {
312 $dbRes = YandexBannerTable::getList(array(
313 'filter' => array(
314 '=XML_ID' => array_values(array_unique($bannerIds)),
315 '=ENGINE_ID' => $directEngine->getId()
316 ),
317 'select' => array(
318 'ID', 'XML_ID'
319 )
320 ));
321 $bannerList = array();
322 while($bannerData = $dbRes->fetch())
323 {
324 $bannerList[$bannerData['XML_ID']] = $bannerData['ID'];
325 }
326
327 if(count($bannerList) > 0)
328 {
329 foreach($result['Stat'] as $statEntry)
330 {
331 if(array_key_exists($statEntry['BannerID'], $bannerList))
332 {
333 $statFields = array(
334 'CAMPAIGN_ID' => $campaignId,
335 'BANNER_ID' => $bannerList[$statEntry['BannerID']],
336 'DATE_DAY' => new Date($statEntry['StatDate'], 'Y-m-d'),
337 'CURRENCY' => $result['Currency'],
338 'SUM' => $statEntry['Sum'],
339 'SUM_SEARCH' => $statEntry['SumSearch'],
340 'SUM_CONTEXT' => $statEntry['SumContext'],
341 'CLICKS' => $statEntry['Clicks'],
342 'CLICKS_SEARCH' => $statEntry['ClicksSearch'],
343 'CLICKS_CONTEXT' => $statEntry['ClicksContext'],
344 'SHOWS' => $statEntry['Shows'],
345 'SHOWS_SEARCH' => $statEntry['ShowsSearch'],
346 'SHOWS_CONTEXT' => $statEntry['ShowsContext'],
347 );
348
349 $statCheckRes = static::getList(array(
350 'filter' => array(
351 'BANNER_ID' => $statFields['BANNER_ID'],
352 'DATE_DAY' => $statFields['DATE_DAY'],
353 ),
354 'select' => array('ID')
355 ));
356
357 $statCheck = $statCheckRes->fetch();
358 if(!$statCheck)
359 {
360 static::add($statFields);
361 }
362 }
363 }
364 }
365 }
366 }
367 }
368
369 public static function getMissedPeriods(array $stats, $dateStart, $dateFinish)
370 {
371 $missedPeriods = array();
372
373 $datePrevoius = false;
374
375 $checkDate = new Date($dateStart);
376 $dateCurrent = new Date($dateStart);
377 $dateFinish = new Date($dateFinish);
378
379 while($dateCurrent->getTimestamp() <= $dateFinish->getTimestamp())
380 {
381 if(!array_key_exists($dateCurrent->toString(), $stats))
382 {
383 if(
384 !$datePrevoius
385 || $dateCurrent->getTimestamp() >= $checkDate->getTimestamp()
386 )
387 {
388 $missedPeriods[] = array(
389 $dateCurrent->toString(),
390 $dateCurrent->toString()
391 );
392
393 $checkDate = new Date($dateCurrent->toString());
394 $checkDate->add("+".YandexDirect::MAX_STAT_DAYS_DELTA." days");
395
396 $datePrevoius = true;
397 }
398 else
399 {
400 $missedPeriods[count($missedPeriods)-1][1] = $dateCurrent->toString();
401 }
402 }
403 else
404 {
405 $datePrevoius = false;
406 }
407
408 $dateCurrent->add("+1 days");
409 }
410
411 return $missedPeriods;
412 }
413}
static loadCampaignStat($campaignId, $dateStart, $dateFinish)
static getMissedPeriods(array $stats, $dateStart, $dateFinish)
static loadStat(YandexDirect $directEngine, $campaignXmlId, $dateStart, $dateFinish, $skipCurrency=false)
static getCampaignStat($campaignId, $dateStart, $dateFinish)
static getBannerStat($bannerId, $dateStart, $dateFinish)
static processStatsResult($campaignId, array $result, YandexDirect $directEngine)
static loadBannerStat($bannerId, $dateStart, $dateFinish)