Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
counter.php
1<?php
3
7use Bitrix\Main\Page\Asset;
10
12{
13 protected static $data = array();
14 protected static $enabled = true;
15 protected static $bufferRestarted = false;
16
17 public static function enable()
18 {
19 static::$enabled = true;
20 }
21
22 public static function disable()
23 {
24 static::$enabled = false;
25 }
26
27 public static function getInjectedJs($stripTags = false)
28 {
29 $accountId = static::getAccountId();
30 $params = static::injectDataParams();
31
32 $host = Context::getCurrent()->getServer()->getHttpHost();
33 $host = preg_replace("/:(80|443)$/", "", $host);
34 $host = \CUtil::JSEscape($host);
35
36 $js = <<<JS
37 var _ba = _ba || []; _ba.push(["aid", "{$accountId}"]); _ba.push(["host", "{$host}"]); {$params}
38 (function() {
39 var ba = document.createElement("script"); ba.type = "text/javascript"; ba.async = true;
40 ba.src = (document.location.protocol == "https:" ? "https://" : "http://") + "bitrix.info/ba.js";
41 var s = document.getElementsByTagName("script")[0];
42 s.parentNode.insertBefore(ba, s);
43 })();
44JS;
45
46 $js = str_replace(array("\n", "\t"), "", $js);
47 if ($stripTags === false)
48 {
49 return "<script type=\"text/javascript\">".$js."</script>";
50 }
51 else
52 {
53 return $js;
54 }
55 }
56
57 public static function injectIntoPage()
58 {
59 Asset::getInstance()->addString(static::getInjectedJs(), false, AssetLocation::AFTER_JS);
60 }
61
62 public static function getAccountId()
63 {
64 $license = Application::getInstance()->getLicense();
65 if (!$license->isDemoKey())
66 {
67 return $license->getPublicHashKey();
68 }
69 else
70 {
71 return "";
72 }
73 }
74
75 public static function getPrivateKey()
76 {
77 $license = Application::getInstance()->getLicense();
78 if (!$license->isDemoKey())
79 {
80 return $license->getHashLicenseKey();
81 }
82 else
83 {
84 return "";
85 }
86 }
87
88 public static function onBeforeEndBufferContent()
89 {
90 $request = Context::getCurrent()->getRequest();
91 $isAjaxRequest = $request->isAjaxRequest();
92 $isAdminSection = defined("ADMIN_SECTION") && ADMIN_SECTION === true;
93 if ($isAjaxRequest || $isAdminSection)
94 {
95 return;
96 }
97
98 $isSlider = $request->getQuery('IFRAME') === "Y";
99 if (static::$bufferRestarted === true && !$isSlider)
100 {
101 return;
102 }
103
104 $settings = Configuration::getValue("analytics_counter");
105 $forceEnabled = isset($settings["enabled"]) && $settings["enabled"] === true;
106
107 if ($forceEnabled === false && SiteSpeed::isIntranetSite(SITE_ID))
108 {
109 return;
110 }
111
112 if (SiteSpeed::isOn() && static::$enabled === true)
113 {
115 }
116 }
117
118 public static function onBeforeRestartBuffer()
119 {
120 static::$bufferRestarted = true;
121 }
122
123 public static function sendData($id, array $arParams)
124 {
125 static::$data[$id] = $arParams;
126 }
127
128 private static function injectDataParams()
129 {
130 $result = "";
131 foreach (static::$data as $index => $arItem)
132 {
133 foreach ($arItem as $key => $value)
134 {
135 if (is_array($value))
136 {
137 $jsValue = '"'.\CUtil::PhpToJSObject($value).'"';
138 }
139 elseif ($value instanceof JsExpression)
140 {
141 $jsValue = $value;
142 }
143 else
144 {
145 $jsValue = '"'.\CUtil::JSEscape($value).'"';
146 }
147
148 $result .= '_ba.push(["ad['.$index.']['.\CUtil::JSEscape($key).']", '.$jsValue.']);';
149 }
150 }
151
152 return $result;
153 }
154}
static getInjectedJs($stripTags=false)
Definition counter.php:27
static sendData($id, array $arParams)
Definition counter.php:123
static getCurrent()
Definition context.php:241