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