-
Notifications
You must be signed in to change notification settings - Fork 12
/
AnonymousPiwikUsageMeasurement.php
139 lines (115 loc) · 4.57 KB
/
AnonymousPiwikUsageMeasurement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\AnonymousPiwikUsageMeasurement;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker\Profiles;
class AnonymousPiwikUsageMeasurement extends \Piwik\Plugin
{
public const TRACKING_DOMAIN = 'https://demo-anonymous.matomo.org';
public const EXAMPLE_DOMAIN = 'http://example.com';
private $profilingStack = array();
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return array(
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'Template.jsGlobalVariables' => 'addMatomoClientTracking',
'API.Request.dispatch' => 'logStartTimeOfApiCall',
'API.Request.dispatch.end' => 'trackApiCall',
'Db.getTablesInstalled' => 'getTablesInstalled'
);
}
public function install()
{
$dao = new Profiles();
$dao->install();
}
public function uninstall()
{
$dao = new Profiles();
$dao->uninstall();
}
/**
* Register the new tables, so Matomo knows about them.
*
* @param array $allTablesInstalled
*/
public function getTablesInstalled(&$allTablesInstalled)
{
$allTablesInstalled[] = Common::prefixTable('usage_measurement_profiles');
}
public function logStartTimeOfApiCall(&$finalParameters, $pluginName, $methodName)
{
// API methods can call other API methods...
$this->profilingStack[] = array(
'method' => $pluginName . '.' . $methodName,
'time' => microtime(true)
);
}
public function trackApiCall(&$return, $endHookParams)
{
$endTime = microtime(true);
$name = $endHookParams['module'];
$method = $name . '.' . $endHookParams['action'];
$neededTimeInMs = 0;
do {
$call = array_pop($this->profilingStack);
// we need to make sure the call was actually for this method to not send wrong data.
if ($method === $call['method']) {
$neededTimeInMs = ceil(($endTime - $call['time']) * 1000);
break;
}
} while (!empty($this->profilingStack));
if (empty($neededTimeInMs)) {
return;
}
$profiles = StaticContainer::get('Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker\Profiles');
$now = Date::now()->getDatetime();
$category = 'API';
$profiles->pushProfile($now, $category, $name, $method, $count = 1, (int) $neededTimeInMs);
}
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = 'matomo.js';
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/javascripts/url.js';
$jsFiles[] = 'plugins/AnonymousPiwikUsageMeasurement/javascripts/tracking.js';
}
public function addMatomoClientTracking(&$out)
{
$settings = StaticContainer::get(SystemSettings::class);
$userSettings = StaticContainer::get(UserSettings::class);
$config = array(
'targets' => array(),
'visitorCustomVariables' => array(),
'trackingDomain' => self::TRACKING_DOMAIN,
'exampleDomain' => self::EXAMPLE_DOMAIN,
'userId' => Piwik::getCurrentUserLogin()
);
if (
Piwik::isUserIsAnonymous()
|| !$settings->canUserOptOut->getValue()
|| $userSettings->userTrackingEnabled->getValue()
) {
// an anonymous user is currently always tracked, an anonymous user would not have permission to read
// this user setting. The `isUserIsAnonymous()` check is not needed but there to improve performance
// in case user is anonymous. Then we avoid checking whether user has access to any sites which can be slow
// a user not having any view permission is also always tracked so far as such a user is not allowed to read
// this setting
$targets = StaticContainer::get('Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker\Targets');
$customVars = StaticContainer::get('Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker\CustomVariables');
$config['targets'] = $targets->getTargets();
$config['visitorCustomVariables'] = $customVars->getClientVisitCustomVariables();
}
$out .= "\nvar piwikUsageTracking = " . json_encode($config) . ";\n";
}
}