Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Events.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\Events;
  10. use Piwik\Common;
  11. use Piwik\Piwik;
  12. use Piwik\Plugin\Report;
  13. use Piwik\Plugin\ViewDataTable;
  14. class Events extends \Piwik\Plugin
  15. {
  16. /**
  17. * @see Piwik\Plugin::getListHooksRegistered
  18. */
  19. public function getListHooksRegistered()
  20. {
  21. return array(
  22. 'Metrics.getDefaultMetricDocumentationTranslations' => 'addMetricDocumentationTranslations',
  23. 'Metrics.getDefaultMetricTranslations' => 'addMetricTranslations',
  24. 'ViewDataTable.configure' => 'configureViewDataTable',
  25. 'Live.getAllVisitorDetails' => 'extendVisitorDetails'
  26. );
  27. }
  28. public function extendVisitorDetails(&$visitor, $details)
  29. {
  30. $visitor['events'] = $details['visit_total_events'];
  31. }
  32. public function addMetricTranslations(&$translations)
  33. {
  34. $translations = array_merge($translations, $this->getMetricTranslations());
  35. }
  36. public function addMetricDocumentationTranslations(&$translations)
  37. {
  38. $translations = array_merge($translations, $this->getMetricDocumentation());
  39. }
  40. public function getMetricDocumentation()
  41. {
  42. $documentation = array(
  43. 'nb_events' => 'Events_TotalEventsDocumentation',
  44. 'sum_event_value' => 'Events_TotalValueDocumentation',
  45. 'min_event_value' => 'Events_MinValueDocumentation',
  46. 'max_event_value' => 'Events_MaxValueDocumentation',
  47. 'avg_event_value' => 'Events_AvgValueDocumentation',
  48. 'nb_events_with_value' => 'Events_EventsWithValueDocumentation',
  49. );
  50. $documentation = array_map(array('\\Piwik\\Piwik', 'translate'), $documentation);
  51. return $documentation;
  52. }
  53. public function getMetricTranslations()
  54. {
  55. $metrics = array(
  56. 'nb_events' => 'Events_TotalEvents',
  57. 'sum_event_value' => 'Events_TotalValue',
  58. 'min_event_value' => 'Events_MinValue',
  59. 'max_event_value' => 'Events_MaxValue',
  60. 'avg_event_value' => 'Events_AvgValue',
  61. 'nb_events_with_value' => 'Events_EventsWithValue',
  62. );
  63. $metrics = array_map(array('\\Piwik\\Piwik', 'translate'), $metrics);
  64. return $metrics;
  65. }
  66. public $metadataDimensions = array(
  67. 'eventCategory' => array('Events_EventCategory', 'log_link_visit_action.idaction_event_category'),
  68. 'eventAction' => array('Events_EventAction', 'log_link_visit_action.idaction_event_action'),
  69. 'eventName' => array('Events_EventName', 'log_link_visit_action.idaction_name'),
  70. );
  71. public function getDimensionLabel($dimension)
  72. {
  73. return Piwik::translate($this->metadataDimensions[$dimension][0]);
  74. }
  75. /**
  76. * @return array
  77. */
  78. public static function getLabelTranslations()
  79. {
  80. return array(
  81. 'getCategory' => array('Events_EventCategories', 'Events_EventCategory'),
  82. 'getAction' => array('Events_EventActions', 'Events_EventAction'),
  83. 'getName' => array('Events_EventNames', 'Events_EventName'),
  84. );
  85. }
  86. public function getSegmentsMetadata(&$segments)
  87. {
  88. // $segments[] = array(
  89. // 'type' => 'metric',
  90. // 'category' => 'Events_Events',
  91. // 'name' => 'Events_EventValue',
  92. // 'segment' => 'eventValue',
  93. // 'sqlSegment' => 'log_link_visit_action.custom_float',
  94. // 'sqlFilter' => '\\Piwik\\Plugins\\Events\\Events::getSegmentEventValue'
  95. // );
  96. }
  97. //
  98. // public static function getSegmentEventValue($valueToMatch, $sqlField, $matchType, $segmentName)
  99. // {
  100. // $andActionisNotEvent = \Piwik\Plugins\Actions\Archiver::getWhereClauseActionIsNotEvent();
  101. // $andActionisEvent = str_replace("IS NULL", "IS NOT NULL", $andActionisNotEvent);
  102. //
  103. // return array(
  104. // 'extraWhere' => $andActionisEvent,
  105. // 'bind' => $valueToMatch
  106. // );
  107. // }
  108. /**
  109. * Given getCategory, returns "Event Categories"
  110. *
  111. * @param $apiMethod
  112. * @return string
  113. */
  114. public function getReportTitleTranslation($apiMethod)
  115. {
  116. return $this->getTranslation($apiMethod, $index = 0);
  117. }
  118. /**
  119. * Given getCategory, returns "Event Category"
  120. *
  121. * @param $apiMethod
  122. * @return string
  123. */
  124. public function getColumnTranslation($apiMethod)
  125. {
  126. return $this->getTranslation($apiMethod, $index = 1);
  127. }
  128. protected function getTranslation($apiMethod, $index)
  129. {
  130. $labels = $this->getLabelTranslations();
  131. foreach ($labels as $action => $translations) {
  132. // Events.getActionFromCategoryId returns translation for Events.getAction
  133. if (strpos($apiMethod, $action) === 0) {
  134. $columnLabel = $translations[$index];
  135. return Piwik::translate($columnLabel);
  136. }
  137. }
  138. throw new \Exception("Translation not found for report $apiMethod");
  139. }
  140. public function configureViewDataTable(ViewDataTable $view)
  141. {
  142. if ($view->requestConfig->getApiModuleToRequest() != 'Events') {
  143. return;
  144. }
  145. // eg. 'Events.getCategory'
  146. $apiMethod = $view->requestConfig->getApiMethodToRequest();
  147. $secondaryDimension = $this->getSecondaryDimensionFromRequest();
  148. $view->config->subtable_controller_action = API::getInstance()->getActionToLoadSubtables($apiMethod, $secondaryDimension);
  149. if (Common::getRequestVar('pivotBy', false) === false) {
  150. $view->config->columns_to_display = array('label', 'nb_events', 'sum_event_value');
  151. }
  152. $view->config->show_flatten_table = true;
  153. $view->config->show_table_all_columns = false;
  154. $view->requestConfig->filter_sort_column = 'nb_events';
  155. $labelTranslation = $this->getColumnTranslation($apiMethod);
  156. $view->config->addTranslation('label', $labelTranslation);
  157. $view->config->addTranslations($this->getMetricTranslations());
  158. $this->addRelatedReports($view, $secondaryDimension);
  159. $this->addTooltipEventValue($view);
  160. $subtableReport = Report::factory('Events', $view->config->subtable_controller_action);
  161. $view->config->pivot_by_dimension = $subtableReport->getDimension()->getId();
  162. $view->config->pivot_by_column = 'nb_events';
  163. }
  164. private function addRelatedReports($view, $secondaryDimension)
  165. {
  166. if(empty($secondaryDimension)) {
  167. // eg. Row Evolution
  168. return;
  169. }
  170. $view->config->show_related_reports = true;
  171. $apiMethod = $view->requestConfig->getApiMethodToRequest();
  172. $secondaryDimensions = API::getInstance()->getSecondaryDimensions($apiMethod);
  173. if(empty($secondaryDimensions)) {
  174. return;
  175. }
  176. $secondaryDimensionTranslation = $this->getDimensionLabel($secondaryDimension);
  177. $view->config->related_reports_title =
  178. Piwik::translate('Events_SecondaryDimension', $secondaryDimensionTranslation)
  179. . "<br/>"
  180. . Piwik::translate('Events_SwitchToSecondaryDimension', '');
  181. foreach($secondaryDimensions as $dimension) {
  182. if($dimension == $secondaryDimension) {
  183. // don't show as related report the currently selected dimension
  184. continue;
  185. }
  186. $dimensionTranslation = $this->getDimensionLabel($dimension);
  187. $view->config->addRelatedReport(
  188. $view->requestConfig->apiMethodToRequestDataTable,
  189. $dimensionTranslation,
  190. array('secondaryDimension' => $dimension)
  191. );
  192. }
  193. }
  194. private function addTooltipEventValue($view)
  195. {
  196. // Creates the tooltip message for Event Value column
  197. $tooltipCallback = function ($hits, $min, $max, $avg) {
  198. if (!$hits) {
  199. return false;
  200. }
  201. $msgEventMinMax = Piwik::translate("Events_EventValueTooltip", array($hits, "<br />", $min, $max));
  202. $msgEventAvg = Piwik::translate("Events_AvgEventValue", $avg);
  203. return $msgEventMinMax . "<br/>" . $msgEventAvg;
  204. };
  205. // Add tooltip metadata column to the DataTable
  206. $view->config->filters[] = array('ColumnCallbackAddMetadata',
  207. array(
  208. array(
  209. 'nb_events',
  210. 'min_event_value',
  211. 'max_event_value',
  212. 'avg_event_value'
  213. ),
  214. 'sum_event_value_tooltip',
  215. $tooltipCallback
  216. )
  217. );
  218. }
  219. /**
  220. * @return mixed
  221. */
  222. public function getSecondaryDimensionFromRequest()
  223. {
  224. return Common::getRequestVar('secondaryDimension', false, 'string');
  225. }
  226. }