1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
analyticsevent.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
14
15final class AnalyticsEvent
16{
17 public const STATUS_SUCCESS = 'success';
18 public const STATUS_ERROR = 'error';
19 public const STATUS_ATTEMPT = 'attempt';
20 public const STATUS_CANCEL = 'cancel';
21
22 private ?string $section;
23 private ?string $subSection;
24 private ?string $element;
25 private ?string $type;
26 private ?string $p1;
27 private ?string $p2;
28 private ?string $p3;
29 private ?string $p4;
30 private ?string $p5;
32 private string $status = self::STATUS_SUCCESS;
33 private ?int $userId;
34 private string $userAgent;
35 private string $host;
36 private string $dbname;
37
38 private bool $isInvalid = false;
39
40 public function __construct(
41 private readonly string $event,
42 private readonly string $tool,
43 private readonly string $category,
44 )
45 {
46 $userAgent = Context::getCurrent()?->getRequest()->getUserAgent();
47 if ($userAgent && \is_string($userAgent))
48 {
49 $this->setUserAgent($userAgent);
50 }
51
52 $httpHost = Context::getCurrent()?->getServer()->getHttpHost();
53 if ($httpHost && \is_string($httpHost))
54 {
55 $this->setHost($httpHost);
56 }
57
58 $dbname = \defined('BX24_DB_NAME') ? BX24_DB_NAME : null;
59 if ($dbname && \is_string($dbname))
60 {
61 $this->setDbName($dbname);
62 }
63 }
64
65 public function setUserId(int $userId): self
66 {
67 $this->userId = $userId;
68
69 return $this;
70 }
71
72 public function setSection(string $section): self
73 {
74 $this->section = $section;
75
76 return $this;
77 }
78
79 public function setSubSection(string $subSection): self
80 {
81 $this->subSection = $subSection;
82
83 return $this;
84 }
85
86 public function setElement(string $element): self
87 {
88 $this->element = $element;
89
90 return $this;
91 }
92
93 public function setType(string $type): self
94 {
95 $this->type = $type;
96
97 return $this;
98 }
99
103 public function setP1(string $p1): self
104 {
105 $this->validatePField($p1);
106 $this->p1 = $p1;
107
108 return $this;
109 }
110
114 public function setP2(string $p2): self
115 {
116 $this->validatePField($p2);
117 $this->p2 = $p2;
118
119 return $this;
120 }
121
125 public function setP3(string $p3): self
126 {
127 $this->validatePField($p3);
128 $this->p3 = $p3;
129
130 return $this;
131 }
132
136 public function setP4(string $p4): self
137 {
138 $this->validatePField($p4);
139 $this->p4 = $p4;
140
141 return $this;
142 }
143
147 public function setP5(string $p5): self
148 {
149 $this->validatePField($p5);
150 $this->p5 = $p5;
151
152 return $this;
153 }
154
155 public function setStatus(string $status): self
156 {
157 $this->status = $status;
158
159 return $this;
160 }
161
162 public function setUserAgent(string $userAgent): self
163 {
164 $this->userAgent = $userAgent;
165
166 return $this;
167 }
168
169 public function setHost(string $host): self
170 {
171 $this->host = $host;
172
173 return $this;
174 }
175
176 public function setDbName(string $dbname): self
177 {
178 $this->dbname = $dbname;
179
180 return $this;
181 }
182
183 public function markAsSuccess(): self
184 {
185 return $this->setStatus(self::STATUS_SUCCESS);
186 }
187
188 public function markAsCanceled(): self
189 {
190 return $this->setStatus(self::STATUS_CANCEL);
191 }
192
193 public function markAsError(): self
194 {
195 return $this->setStatus(self::STATUS_ERROR);
196 }
197
198 public function markAsAttempt(): self
199 {
200 return $this->setStatus(self::STATUS_ATTEMPT);
201 }
202
206 private function validatePField(string $value): void
207 {
208 $invalidValue = substr_count($value, '_') > 1;
209 if ($invalidValue)
210 {
211 $this->isInvalid = true;
212 }
213
214 if ($invalidValue && $this->isDevMode())
215 {
216 throw new ArgumentException('Value for p{1-5} field must contain a single underscore.');
217 }
218 }
219
223 private function validateRequiredFields(): void
224 {
225 if (empty($this->event) || empty($this->tool) || empty($this->category))
226 {
227 $this->isInvalid = true;
228
229 if ($this->isDevMode())
230 {
231 throw new ArgumentException('Event, tool and category fields are required and should be filled.');
232 }
233 }
234 }
235
236 private function isDevMode(): bool
237 {
238 $exceptionHandling = Configuration::getValue('exception_handling');
239
240 return !empty($exceptionHandling['debug']);
241 }
242
243 public function exportToArray(): array
244 {
245 return [
246 'event' => $this->event,
247 'tool' => $this->tool,
248 'category' => $this->category,
249 'section' => $this->section ?? null,
250 'subSection' => $this->subSection ?? null,
251 'element' => $this->element ?? null,
252 'type' => $this->type ?? null,
253 'p1' => $this->p1 ?? null,
254 'p2' => $this->p2 ?? null,
255 'p3' => $this->p3 ?? null,
256 'p4' => $this->p4 ?? null,
257 'p5' => $this->p5 ?? null,
258 'status' => $this->status ?? null,
259 'userAgent' => $this->userAgent ?? null,
260 ];
261 }
262
266 public function send(): void
267 {
268 if (!\defined('ANALYTICS_V2_FILENAME') || !is_writable(ANALYTICS_V2_FILENAME))
269 {
270 return;
271 }
272
273 $this->validateRequiredFields();
274 if ($this->isInvalid)
275 {
276 return;
277 }
278
279 if (!isset($this->userId) && !\defined('BX_CHECK_AGENT_START') && ($GLOBALS['USER'] instanceof \CUser))
280 {
281 $this->userId = (int)$GLOBALS['USER']->getId();
282 }
283
284 $data = $this->buildLogData();
285 if ($this->isDevMode())
286 {
287 $this->triggerDebugEvent($data);
288 }
289
290 $logger = new FileLogger(ANALYTICS_V2_FILENAME, 0);
291 $logger->setFormatter(new JsonLinesFormatter());
292 $logger->debug('', $data);
293 }
294
295 private function triggerDebugEvent(array $data): void
296 {
297 $event = new Event('main', 'OnAnalyticsEvent', ['analyticsEvent' => $this, 'eventData' => $data]);
298 $event->send();
299 }
300
301 private function buildLogData(): array
302 {
303 $data = [
304 'date' => date('Y-m-d H:i:s'),
305 'host' => $this->host ?? null,
306 'dbname' => $this->dbname ?? null,
307 'userId' => $this->userId ?? 0,
308 'event' => $this->exportToArray(),
309 ];
310
311 if (Loader::includeModule('bitrix24'))
312 {
313 $data['license'] = \CBitrix24::getLicenseType();
314 }
315
316 return $data;
317 }
318}
setUserAgent(string $userAgent)
Определения analyticsevent.php:162
setElement(string $element)
Определения analyticsevent.php:86
setSubSection(string $subSection)
Определения analyticsevent.php:79
setStatus(string $status)
Определения analyticsevent.php:155
__construct(private readonly string $event, private readonly string $tool, private readonly string $category,)
Определения analyticsevent.php:40
setSection(string $section)
Определения analyticsevent.php:72
setUserId(int $userId)
Определения analyticsevent.php:65
setDbName(string $dbname)
Определения analyticsevent.php:176
setHost(string $host)
Определения analyticsevent.php:169
setType(string $type)
Определения analyticsevent.php:93
Определения event.php:5
Определения loader.php:13
static includeModule($moduleName)
Определения loader.php:67
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
Определения culture.php:9
$GLOBALS['____1690880296']
Определения license.php:1
$event
Определения prolog_after.php:141