'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"; } }