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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Events;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Piwik;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugin\ReportsProvider;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable\AllColumns;
class Events extends \Piwik\Plugin
{
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return array(
'Metrics.getDefaultMetricDocumentationTranslations' => 'addMetricDocumentationTranslations',
'Metrics.getDefaultMetricTranslations' => 'addMetricTranslations',
'Metrics.getDefaultMetricSemanticTypes' => 'addMetricSemanticTypes',
'ViewDataTable.configure' => 'configureViewDataTable',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Actions.getCustomActionDimensionFieldsAndJoins' => 'provideActionDimensionFields',
);
}
public function addMetricTranslations(&$translations)
{
$translations = array_merge($translations, $this->getMetricTranslations());
}
public function addMetricDocumentationTranslations(&$translations)
{
$translations = array_merge($translations, $this->getMetricDocumentation());
}
public function addMetricSemanticTypes(array &$types): void
{
$metricTypes = array(
'nb_events' => Dimension::TYPE_NUMBER,
'sum_event_value' => Dimension::TYPE_NUMBER,
'min_event_value' => Dimension::TYPE_NUMBER,
'max_event_value' => Dimension::TYPE_NUMBER,
'nb_events_with_value' => Dimension::TYPE_NUMBER,
);
$types = array_merge($types, $metricTypes);
}
public function getMetricDocumentation()
{
$documentation = array(
'nb_events' => 'Events_TotalEventsDocumentation',
'sum_event_value' => 'Events_TotalValueDocumentation',
'min_event_value' => 'Events_MinValueDocumentation',
'max_event_value' => 'Events_MaxValueDocumentation',
'avg_event_value' => 'Events_AvgValueDocumentation',
'nb_events_with_value' => 'Events_EventsWithValueDocumentation',
);
$documentation = array_map(array('\\Piwik\\Piwik', 'translate'), $documentation);
return $documentation;
}
public function getMetricTranslations()
{
$metrics = array(
'nb_events' => 'Events_Events',
'sum_event_value' => 'Events_EventValue',
'min_event_value' => 'Events_MinValue',
'max_event_value' => 'Events_MaxValue',
'avg_event_value' => 'Events_AvgValue',
'nb_events_with_value' => 'Events_EventsWithValue',
);
$metrics = array_map(array('\\Piwik\\Piwik', 'translate'), $metrics);
return $metrics;
}
public $metadataDimensions = array(
'eventCategory' => array('Events_EventCategory', 'log_link_visit_action.idaction_event_category'),
'eventAction' => array('Events_EventAction', 'log_link_visit_action.idaction_event_action'),
'eventName' => array('Events_EventName', 'log_link_visit_action.idaction_name'),
);
public function getDimensionLabel($dimension)
{
return Piwik::translate($this->metadataDimensions[$dimension][0]);
}
/**
* @return array
*/
public static function getLabelTranslations()
{
return array(
'getCategory' => array('Events_EventCategories', 'Events_EventCategory'),
'getAction' => array('Events_EventActions', 'Events_EventAction'),
'getName' => array('Events_EventNames', 'Events_EventName'),
);
}
/**
* Given getCategory, returns "Event Categories"
*
* @param $apiMethod
* @return string
*/
public function getReportTitleTranslation($apiMethod)
{
return $this->getTranslation($apiMethod, $index = 0);
}
/**
* Given getCategory, returns "Event Category"
*
* @param $apiMethod
* @return string
*/
public function getColumnTranslation($apiMethod)
{
return $this->getTranslation($apiMethod, $index = 1);
}
protected function getTranslation($apiMethod, $index)
{
$labels = $this->getLabelTranslations();
foreach ($labels as $action => $translations) {
// Events.getActionFromCategoryId returns translation for Events.getAction
if (strpos($apiMethod, $action) === 0) {
$columnLabel = $translations[$index];
return Piwik::translate($columnLabel);
}
}
throw new \Exception("Translation not found for report $apiMethod");
}
public function configureViewDataTable(ViewDataTable $view)
{
if ($view->requestConfig->getApiModuleToRequest() != 'Events') {
return;
}
// eg. 'Events.getCategory'
$apiMethod = $view->requestConfig->getApiMethodToRequest();
$secondaryDimension = $this->getSecondaryDimensionFromRequest();
$view->config->subtable_controller_action = API::getInstance()->getActionToLoadSubtables($apiMethod, $secondaryDimension);
$pivotBy = Common::getRequestVar('pivotBy', false);
if (empty($pivotBy)) {
$view->config->columns_to_display = array('label', 'nb_events', 'sum_event_value');
}
$view->config->show_flatten_table = true;
$view->requestConfig->filter_sort_column = 'nb_events';
if ($view->isViewDataTableId(AllColumns::ID)) {
$view->config->filters[] = function (DataTable $table) use ($view) {
$columsToDisplay = array('label');
$columns = $table->getColumns();
if (in_array('nb_visits', $columns)) {
$columsToDisplay[] = 'nb_visits';
}
if (in_array('nb_uniq_visitors', $columns)) {
$columsToDisplay[] = 'nb_uniq_visitors';
}
$view->config->columns_to_display = array_merge($columsToDisplay, array('nb_events', 'sum_event_value', 'avg_event_value', 'min_event_value', 'max_event_value'));
if (!in_array($view->requestConfig->filter_sort_column, $view->config->columns_to_display)) {
$view->requestConfig->filter_sort_column = 'nb_events';
}
};
$view->config->show_pivot_by_subtable = false;
}
$labelTranslation = $this->getColumnTranslation($apiMethod);
$view->config->addTranslation('label', $labelTranslation);
$view->config->addTranslations($this->getMetricTranslations());
$this->addRelatedReports($view, $secondaryDimension);
$this->addTooltipEventValue($view);
$subtableReport = ReportsProvider::factory('Events', $view->config->subtable_controller_action);
$view->config->pivot_by_dimension = $subtableReport->getDimension()->getId();
$view->config->pivot_by_column = 'nb_events';
}
private function addRelatedReports($view, $secondaryDimension)
{
if (empty($secondaryDimension)) {
// eg. Row Evolution
return;
}
$view->config->show_related_reports = true;
$apiMethod = $view->requestConfig->getApiMethodToRequest();
$secondaryDimensions = API::getInstance()->getSecondaryDimensions($apiMethod);
if (empty($secondaryDimensions)) {
return;
}
$secondaryDimensionTranslation = $this->getDimensionLabel($secondaryDimension);
$view->config->related_reports_title =
Piwik::translate('Events_SecondaryDimension', $secondaryDimensionTranslation)
. "<br/>"
. Piwik::translate('Events_SwitchToSecondaryDimension', '');
foreach ($secondaryDimensions as $dimension) {
if ($dimension == $secondaryDimension) {
// don't show as related report the currently selected dimension
continue;
}
$dimensionTranslation = $this->getDimensionLabel($dimension);
$view->config->addRelatedReport(
$view->requestConfig->apiMethodToRequestDataTable,
$dimensionTranslation,
array('secondaryDimension' => $dimension)
);
}
}
private function addTooltipEventValue(ViewDataTable $view)
{
// Creates the tooltip message for Event Value column
$tooltipCallback = function ($hits, $min, $max, $avg) {
if (!$hits) {
return false;
}
$avg = $avg ?: 0;
$msgEventMinMax = Piwik::translate("Events_EventValueTooltip", array($hits, "<br />", $min, $max));
$msgEventAvg = Piwik::translate("Events_AvgEventValue", $avg);
return $msgEventMinMax . "<br/>" . $msgEventAvg;
};
// Add tooltip metadata column to the DataTable
$view->config->filters[] = array('ColumnCallbackAddMetadata',
array(
array(
'nb_events',
'min_event_value',
'max_event_value',
'avg_event_value',
),
'sum_event_value_tooltip',
$tooltipCallback,
),
);
}
/**
* @return mixed
*/
public function getSecondaryDimensionFromRequest()
{
return Common::getRequestVar('secondaryDimension', false, 'string');
}
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/Events/stylesheets/datatable.less";
}
public function provideActionDimensionFields(&$fields, &$joins)
{
$fields[] = 'log_action_event_category.type AS eventType';
$fields[] = 'log_action_event_category.name AS eventCategory';
$fields[] = 'log_action_event_action.name as eventAction';
$joins[] = 'LEFT JOIN ' . Common::prefixTable('log_action') . ' AS log_action_event_action
ON log_link_visit_action.idaction_event_action = log_action_event_action.idaction';
$joins[] = 'LEFT JOIN ' . Common::prefixTable('log_action') . ' AS log_action_event_category
ON log_link_visit_action.idaction_event_category = log_action_event_category.idaction';
}
}
|