1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\EventListener; |
10
|
|
|
|
11
|
|
|
use Joomla\Application\ApplicationEvents; |
12
|
|
|
use Joomla\Application\Event\ApplicationEvent; |
13
|
|
|
use Joomla\Event\SubscriberInterface; |
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
15
|
|
|
use Psr\Log\LoggerAwareTrait; |
16
|
|
|
use Ramsey\Uuid\Uuid; |
17
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Analytics; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Analytics handling event subscriber |
21
|
|
|
*/ |
22
|
|
|
class AnalyticsSubscriber implements SubscriberInterface, LoggerAwareInterface |
23
|
|
|
{ |
24
|
|
|
use LoggerAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Application analytics object. |
28
|
|
|
* |
29
|
|
|
* @var Analytics |
30
|
|
|
*/ |
31
|
|
|
private $analytics; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param Analytics $analytics Application analytics object. |
37
|
|
|
*/ |
38
|
12 |
|
public function __construct(Analytics $analytics) |
39
|
|
|
{ |
40
|
12 |
|
$this->analytics = $analytics; |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns an array of events this subscriber will listen to. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
9 |
|
public static function getSubscribedEvents(): array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
9 |
|
ApplicationEvents::BEFORE_EXECUTE => 'onBeforeExecute', |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Logs the visit to analytics if able. |
57
|
|
|
* |
58
|
|
|
* @param ApplicationEvent $event Event object |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
3 |
|
public function onBeforeExecute(ApplicationEvent $event): void |
63
|
|
|
{ |
64
|
3 |
|
$app = $event->getApplication(); |
65
|
|
|
|
66
|
|
|
// On a GET request to the live domain, submit analytics data |
67
|
3 |
|
if ($app->input->getMethod() !== 'GET' |
68
|
3 |
|
|| strpos($app->input->server->getString('HTTP_HOST', ''), 'developer.joomla.org') !== 0) |
69
|
|
|
{ |
70
|
1 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$this->analytics->setAsyncRequest(true) |
74
|
2 |
|
->setProtocolVersion('1') |
75
|
2 |
|
->setTrackingId('UA-544070-16') |
76
|
2 |
|
->setClientId(Uuid::uuid4()->toString()) |
77
|
2 |
|
->setDocumentPath($app->get('uri.base.path')) |
78
|
2 |
|
->setIpOverride($app->input->server->getString('REMOTE_ADDR', '127.0.0.1')) |
79
|
2 |
|
->setUserAgentOverride($app->input->server->getString('HTTP_USER_AGENT', 'JoomlaStats/1.0')); |
80
|
|
|
|
81
|
|
|
// Don't allow sending Analytics data to cause a failure |
82
|
|
|
try |
83
|
|
|
{ |
84
|
2 |
|
$this->analytics->sendPageview(); |
85
|
|
|
} |
86
|
1 |
|
catch (\Exception $e) |
87
|
|
|
{ |
88
|
|
|
// Log the error for reference |
89
|
1 |
|
$this->logger->error( |
90
|
1 |
|
'Error sending analytics data.', |
91
|
1 |
|
['exception' => $e] |
92
|
|
|
); |
93
|
|
|
} |
94
|
2 |
|
} |
95
|
|
|
} |
96
|
|
|
|