Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
handlers.php
1<?php
2
4
7use Bitrix\Main\Page\Asset;
10
11Loc::loadMessages(__FILE__);
12
14final class Handlers
15{
16 public static function onGetCounterTypes()
17 {
18 return array(
19 'conversion_visit_day' => array('MODULE' => 'conversion', 'GROUP' => 'day', 'NAME' => 'Day visits'),
20 );
21 }
22
23 public static function onGetAttributeTypes()
24 {
25 $userAgent = $_SERVER['HTTP_USER_AGENT'];
26
27 // http://stackoverflow.com/questions/18070154/get-operating-system-info-with-php
28 $operatingSystemValues = array(
29 'windows' => array('NAME' => 'Windows' , 'REGEX' => '/windows|win98|win95|win16/i' ),
30 'macos' => array('NAME' => 'Mac OS' , 'REGEX' => '/macintosh|mac os x|mac_powerpc/i'),
31 'android' => array('NAME' => 'Android' , 'REGEX' => '/android/i' ),
32 'linux' => array('NAME' => 'Linux' , 'REGEX' => '/linux|ubuntu/i' ),
33 'ios' => array('NAME' => 'iOS' , 'REGEX' => '/iphone|ipod|ipad/i' ),
34 'blackberry' => array('NAME' => 'BlackBerry', 'REGEX' => '/blackberry/i' ),
35 'webos' => array('NAME' => 'Web OS' , 'REGEX' => '/webos/i' ),
36 );
37
38 $browser = null; // TODO hack
39
40 $browserValues = array(
41 'edge' => array('NAME' => 'Edge', 'REGEX' => '/edge\//i'),
42 'ie' => array('NAME' => 'Internet Explorer', 'REGEX' => '/msie|trident/i'),
43 'firefox' => array('NAME' => 'Firefox', 'REGEX' => '/firefox/i' ),
44 'chrome' => array('NAME' => 'Chrome', 'REGEX' => '/chrome/i' ),
45 'opera' => array('NAME' => 'Opera', 'REGEX' => '/opera/i' ),
46 'safari' => array('NAME' => 'Safari', 'REGEX' => '/safari/i' ),
47 'netscape' => array('NAME' => 'Netscape', 'REGEX' => '/netscape/i' ),
48 'maxthon' => array('NAME' => 'Maxthon', 'REGEX' => '/maxthon/i' ),
49 'konqueror' => array('NAME' => 'Konqueror', 'REGEX' => '/konqueror/i'),
50 'mobile' => array('NAME' => 'Handheld', 'REGEX' => '/mobile/i' ),
51 );
52
53 $detector = new MobileDetect;
54
55 $searchEngineValues = array(
56 'google' => array('NAME' => 'Google' , 'REGEX' => '#^https?://www\.google\.[a-z]{2,3}/#' ),
57 'bing' => array('NAME' => 'Bing' , 'REGEX' => '#^https?://www\.bing\.[a-z]{2,3}/#' ),
58 'yahoo' => array('NAME' => 'Yahoo' , 'REGEX' => '#^https?://r\.search\.yahoo\.[a-z]{2,3}/#'),
59 'ask' => array('NAME' => 'Ask' , 'REGEX' => '#^https?://www\.ask\.[a-z]{2,3}/#' ),
60 'yandex' => array('NAME' => 'Yandex' , 'REGEX' => '#^https?://yandex\.[a-z]{2,3}/#' ),
61 'mail.ru' => array('NAME' => 'Mail.ru', 'REGEX' => '#^https?://go\.mail\.ru/#' ),
62 'rambler' => array('NAME' => 'Rambler', 'REGEX' => '#^https?://nova\.rambler\.ru/#' ),
63 );
64
65 return array(
66
67 // SITE
68
69 'conversion_site' => array(
70 'MODULE' => 'conversion',
71 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_SITE_NAME'),
72 'SORT' => 1100,
73 'GET_VALUES' => function (array $ids)
74 {
75 $values = array();
76
77 $result = SiteTable::getList(array(
78 'select' => array('LID', 'NAME'),
79 'filter' => array('LID' => $ids),
80 'order' => array('SORT' => 'ASC'),
81 ));
82
83 while ($row = $result->fetch())
84 {
85 $values[$row['LID']] = array('NAME' => $row['NAME']);
86 }
87
88 return $values;
89 },
90 'SET_DAY_CONTEXT' => function (DayContext $dayContext)
91 {
92 if ($siteId = DayContext::getSiteId())
93 {
94 $dayContext->setAttribute('conversion_site', $siteId);
95 }
96 },
97 ),
98
99 // OPERATING SYSTEM
100
101 'conversion_operating_system' => array(
102 'MODULE' => 'conversion',
103 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_OPERATING_SYSTEM_NAME'),
104 'SORT' => 2100,
105 'GET_VALUES' => function (array $ids) use ($operatingSystemValues)
106 {
107 $values = array();
108
109 foreach ($ids as $id)
110 {
111 if ($value = $operatingSystemValues[$id])
112 {
113 $values[$id] = $value;
114 }
115 }
116
117 return $values;
118 },
119 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use ($operatingSystemValues, $userAgent)
120 {
121 if ($userAgent)
122 {
123 foreach ($operatingSystemValues as $name => $type)
124 {
125 if (preg_match($type['REGEX'], $userAgent))
126 {
127 $dayContext->setAttribute('conversion_operating_system', $name);
128 break;
129 }
130 }
131 }
132 },
133 ),
134
135 // BROWSER
136
137 'conversion_browser' => array(
138 'MODULE' => 'conversion',
139 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_BROWSER_NAME'),
140 'SORT' => 3100, // must be before conversion_device_desktop!
141 'GET_VALUES' => function (array $ids) use ($browserValues)
142 {
143 $values = array();
144
145 foreach ($ids as $id)
146 {
147 if ($value = $browserValues[$id])
148 {
149 $values[$id] = $value;
150 }
151 }
152
153 return $values;
154 },
155 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use ($browserValues, $userAgent, & $browser)
156 {
157 if ($userAgent)
158 {
159 foreach ($browserValues as $name => $type)
160 {
161 if (preg_match($type['REGEX'], $userAgent))
162 {
163 $dayContext->setAttribute('conversion_browser', $name);
164 $browser = $name;
165 break;
166 }
167 }
168 }
169 },
170 ),
171
172 // DEVICE
173
174 'conversion_device_tablet' => array(
175 'MODULE' => 'conversion',
176 'GROUP' => 'device',
177 'SORT' => 4100, // must be before conversion_device_mobile!
178 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_DEVICE_TABLET_NAME'),
179 'SPLIT_BY' => 'conversion_operating_system',
180 'BG_COLOR' => '#be6ac4',
181 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use ($detector)
182 {
183 if ($detector->isTablet())
184 {
185 $dayContext->setAttribute('conversion_device_tablet');
186 }
187 },
188 ),
189
190 'conversion_device_mobile' => array(
191 'MODULE' => 'conversion',
192 'GROUP' => 'device',
193 'SORT' => 4200, // must be after conversion_device_tablet!
194 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_DEVICE_MOBILE_NAME'),
195 'SPLIT_BY' => 'conversion_operating_system',
196 'BG_COLOR' => '#4bbedb',
197 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use ($detector)
198 {
199 if ($detector->isMobile())
200 {
201 $dayContext->setAttribute('conversion_device_mobile');
202 }
203 },
204 ),
205
206 'conversion_device_desktop' => array(
207 'MODULE' => 'conversion',
208 'GROUP' => 'device',
209 'SORT' => 4500, // must be after conversion_browser!
210 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_DEVICE_DESKTOP_NAME'),
211 'SPLIT_BY' => 'conversion_browser',
212 'BG_COLOR' => '#cf4343',
213 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use (& $browser)
214 {
215 if ($browser)
216 {
217 $dayContext->setAttribute('conversion_device_desktop');
218 }
219 },
220 ),
221
222 // SOURCE
223
224 'conversion_search_engine' => array(
225 'MODULE' => 'conversion',
226 'GROUP' => 'source',
227 'SORT' => 5500, // must be after seo_yandex_direct_source!
228 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_SEARCH_ENGINE_NAME'),
229 'SPLIT_BY' => 'conversion_search_engine',
230 'BG_COLOR' => '#be6ac4',
231 'GET_VALUES' => function (array $ids) use ($searchEngineValues)
232 {
233 $values = array();
234
235 foreach ($ids as $id)
236 {
237 if ($value = $searchEngineValues[$id])
238 {
239 $values[$id] = $value;
240 }
241 }
242
243 return $values;
244 },
245 'SET_DAY_CONTEXT' => function (DayContext $dayContext) use ($searchEngineValues)
246 {
247 if ($referer = $_SERVER['HTTP_REFERER'])
248 {
249 foreach ($searchEngineValues as $name => $type)
250 {
251 if (preg_match($type['REGEX'], $referer))
252 {
253 $dayContext->setAttribute('conversion_search_engine', $name);
254 break;
255 }
256 }
257 }
258 },
259 ),
260
261 );
262 }
263
264 public static function onSetDayContextAttributes(DayContext $dayContext)
265 {
266 foreach (self::onGetAttributeTypes() as $name => $type)
267 {
268 if ($setDayContext = $type['SET_DAY_CONTEXT'])
269 {
270 $setDayContext($dayContext);
271 }
272 }
273 }
274
275 public static function onGetAttributeGroupTypes()
276 {
277 return array(
278 'source' => array(
279 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_GROUP_SOURCE_NAME'),
280 'SORT' => 100,
281 ),
282 'device' => array(
283 'NAME' => Loc::getMessage('CONVERSION_ATTRIBUTE_GROUP_DEVICE_NAME'),
284 'SORT' => 200,
285 ),
286 );
287 }
288
289 public static function onProlog()
290 {
291 static $done = false;
292 if (! $done)
293 {
294 $done = true;
295
296 \CJSCore::init();
298
299 // For composite site this script must not be changing often!!!
300 Asset::getInstance()->addString(
301 '<script type="text/javascript">
302 (function () {
303 "use strict";
304
305 var counter = function ()
306 {
307 var cookie = (function (name) {
308 var parts = ("; " + document.cookie).split("; " + name + "=");
309 if (parts.length == 2) {
310 try {return JSON.parse(decodeURIComponent(parts.pop().split(";").shift()));}
311 catch (e) {}
312 }
313 })("'.DayContext::getVarName().'");
314
315 if (cookie && cookie.EXPIRE >= BX.message("SERVER_TIME"))
316 return;
317
318 var request = new XMLHttpRequest();
319 request.open("POST", "/bitrix/tools/conversion/ajax_counter.php", true);
320 request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
321 request.send(
322 "SITE_ID="+encodeURIComponent("'.DayContext::getSiteId().'")+
323 "&sessid="+encodeURIComponent(BX.bitrix_sessid())+
324 "&HTTP_REFERER="+encodeURIComponent(document.referrer)
325 );
326 };
327
328 if (window.frameRequestStart === true)
329 BX.addCustomEvent("onFrameDataReceived", counter);
330 else
331 BX.ready(counter);
332 })();
333 </script>',
334 false,
335 AssetLocation::AFTER_JS_KERNEL
336 // Do not use AssetMode unless you absolutely sure what you're doing!
337 // Maybe default value or AssetMode::ALL is appropriate, all other modes do not work!
338 // This script must be executed on every hit!!!
339 );
340 }
341 }
342}
static onSetDayContextAttributes(DayContext $dayContext)
Definition handlers.php:264
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29