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.

Metrics.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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;
  10. use Piwik\Cache\LanguageAwareStaticCache;
  11. use Piwik\Cache\PluginAwareStaticCache;
  12. require_once PIWIK_INCLUDE_PATH . "/core/Piwik.php";
  13. /**
  14. * This class contains metadata regarding core metrics and contains several
  15. * related helper functions.
  16. *
  17. * Of note are the `INDEX_...` constants. In the database, metric column names
  18. * in {@link DataTable} rows are stored as integers to save space. The integer
  19. * values used are determined by these constants.
  20. *
  21. * @api
  22. */
  23. class Metrics
  24. {
  25. /*
  26. * When saving DataTables in the DB, we replace all columns name with these IDs. This saves many bytes,
  27. * eg. INDEX_NB_UNIQ_VISITORS is an integer: 4 bytes, but 'nb_uniq_visitors' is 16 bytes at least
  28. */
  29. const INDEX_NB_UNIQ_VISITORS = 1;
  30. const INDEX_NB_VISITS = 2;
  31. const INDEX_NB_ACTIONS = 3;
  32. const INDEX_MAX_ACTIONS = 4;
  33. const INDEX_SUM_VISIT_LENGTH = 5;
  34. const INDEX_BOUNCE_COUNT = 6;
  35. const INDEX_NB_VISITS_CONVERTED = 7;
  36. const INDEX_NB_CONVERSIONS = 8;
  37. const INDEX_REVENUE = 9;
  38. const INDEX_GOALS = 10;
  39. const INDEX_SUM_DAILY_NB_UNIQ_VISITORS = 11;
  40. // Specific to the Actions reports
  41. const INDEX_PAGE_NB_HITS = 12;
  42. const INDEX_PAGE_SUM_TIME_SPENT = 13;
  43. const INDEX_PAGE_EXIT_NB_UNIQ_VISITORS = 14;
  44. const INDEX_PAGE_EXIT_NB_VISITS = 15;
  45. const INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS = 16;
  46. const INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS = 17;
  47. const INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS = 18;
  48. const INDEX_PAGE_ENTRY_NB_VISITS = 19;
  49. const INDEX_PAGE_ENTRY_NB_ACTIONS = 20;
  50. const INDEX_PAGE_ENTRY_SUM_VISIT_LENGTH = 21;
  51. const INDEX_PAGE_ENTRY_BOUNCE_COUNT = 22;
  52. // Ecommerce Items reports
  53. const INDEX_ECOMMERCE_ITEM_REVENUE = 23;
  54. const INDEX_ECOMMERCE_ITEM_QUANTITY = 24;
  55. const INDEX_ECOMMERCE_ITEM_PRICE = 25;
  56. const INDEX_ECOMMERCE_ORDERS = 26;
  57. const INDEX_ECOMMERCE_ITEM_PRICE_VIEWED = 27;
  58. // Site Search
  59. const INDEX_SITE_SEARCH_HAS_NO_RESULT = 28;
  60. const INDEX_PAGE_IS_FOLLOWING_SITE_SEARCH_NB_HITS = 29;
  61. // Performance Analytics
  62. const INDEX_PAGE_SUM_TIME_GENERATION = 30;
  63. const INDEX_PAGE_NB_HITS_WITH_TIME_GENERATION = 31;
  64. const INDEX_PAGE_MIN_TIME_GENERATION = 32;
  65. const INDEX_PAGE_MAX_TIME_GENERATION = 33;
  66. // Events
  67. const INDEX_EVENT_NB_HITS = 34;
  68. const INDEX_EVENT_SUM_EVENT_VALUE = 35;
  69. const INDEX_EVENT_MIN_EVENT_VALUE = 36;
  70. const INDEX_EVENT_MAX_EVENT_VALUE = 37;
  71. const INDEX_EVENT_NB_HITS_WITH_VALUE = 38;
  72. // Number of unique User IDs
  73. const INDEX_NB_USERS = 39;
  74. const INDEX_SUM_DAILY_NB_USERS = 40;
  75. // Contents
  76. const INDEX_CONTENT_NB_IMPRESSIONS = 41;
  77. const INDEX_CONTENT_NB_INTERACTIONS = 42;
  78. // Goal reports
  79. const INDEX_GOAL_NB_CONVERSIONS = 1;
  80. const INDEX_GOAL_REVENUE = 2;
  81. const INDEX_GOAL_NB_VISITS_CONVERTED = 3;
  82. const INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL = 4;
  83. const INDEX_GOAL_ECOMMERCE_REVENUE_TAX = 5;
  84. const INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING = 6;
  85. const INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT = 7;
  86. const INDEX_GOAL_ECOMMERCE_ITEMS = 8;
  87. public static $mappingFromIdToName = array(
  88. Metrics::INDEX_NB_UNIQ_VISITORS => 'nb_uniq_visitors',
  89. Metrics::INDEX_NB_VISITS => 'nb_visits',
  90. Metrics::INDEX_NB_ACTIONS => 'nb_actions',
  91. Metrics::INDEX_NB_USERS => 'nb_users',
  92. Metrics::INDEX_MAX_ACTIONS => 'max_actions',
  93. Metrics::INDEX_SUM_VISIT_LENGTH => 'sum_visit_length',
  94. Metrics::INDEX_BOUNCE_COUNT => 'bounce_count',
  95. Metrics::INDEX_NB_VISITS_CONVERTED => 'nb_visits_converted',
  96. Metrics::INDEX_NB_CONVERSIONS => 'nb_conversions',
  97. Metrics::INDEX_REVENUE => 'revenue',
  98. Metrics::INDEX_GOALS => 'goals',
  99. Metrics::INDEX_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_nb_uniq_visitors',
  100. Metrics::INDEX_SUM_DAILY_NB_USERS => 'sum_daily_nb_users',
  101. // Actions metrics
  102. Metrics::INDEX_PAGE_NB_HITS => 'nb_hits',
  103. Metrics::INDEX_PAGE_SUM_TIME_SPENT => 'sum_time_spent',
  104. Metrics::INDEX_PAGE_SUM_TIME_GENERATION => 'sum_time_generation',
  105. Metrics::INDEX_PAGE_NB_HITS_WITH_TIME_GENERATION => 'nb_hits_with_time_generation',
  106. Metrics::INDEX_PAGE_MIN_TIME_GENERATION => 'min_time_generation',
  107. Metrics::INDEX_PAGE_MAX_TIME_GENERATION => 'max_time_generation',
  108. Metrics::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS => 'exit_nb_uniq_visitors',
  109. Metrics::INDEX_PAGE_EXIT_NB_VISITS => 'exit_nb_visits',
  110. Metrics::INDEX_PAGE_EXIT_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_exit_nb_uniq_visitors',
  111. Metrics::INDEX_PAGE_ENTRY_NB_UNIQ_VISITORS => 'entry_nb_uniq_visitors',
  112. Metrics::INDEX_PAGE_ENTRY_SUM_DAILY_NB_UNIQ_VISITORS => 'sum_daily_entry_nb_uniq_visitors',
  113. Metrics::INDEX_PAGE_ENTRY_NB_VISITS => 'entry_nb_visits',
  114. Metrics::INDEX_PAGE_ENTRY_NB_ACTIONS => 'entry_nb_actions',
  115. Metrics::INDEX_PAGE_ENTRY_SUM_VISIT_LENGTH => 'entry_sum_visit_length',
  116. Metrics::INDEX_PAGE_ENTRY_BOUNCE_COUNT => 'entry_bounce_count',
  117. Metrics::INDEX_PAGE_IS_FOLLOWING_SITE_SEARCH_NB_HITS => 'nb_hits_following_search',
  118. // Items reports metrics
  119. Metrics::INDEX_ECOMMERCE_ITEM_REVENUE => 'revenue',
  120. Metrics::INDEX_ECOMMERCE_ITEM_QUANTITY => 'quantity',
  121. Metrics::INDEX_ECOMMERCE_ITEM_PRICE => 'price',
  122. Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED => 'price_viewed',
  123. Metrics::INDEX_ECOMMERCE_ORDERS => 'orders',
  124. // Events
  125. Metrics::INDEX_EVENT_NB_HITS => 'nb_events',
  126. Metrics::INDEX_EVENT_SUM_EVENT_VALUE => 'sum_event_value',
  127. Metrics::INDEX_EVENT_MIN_EVENT_VALUE => 'min_event_value',
  128. Metrics::INDEX_EVENT_MAX_EVENT_VALUE => 'max_event_value',
  129. Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE => 'nb_events_with_value',
  130. // Contents
  131. Metrics::INDEX_CONTENT_NB_IMPRESSIONS => 'nb_impressions',
  132. Metrics::INDEX_CONTENT_NB_INTERACTIONS => 'nb_interactions'
  133. );
  134. public static $mappingFromIdToNameGoal = array(
  135. Metrics::INDEX_GOAL_NB_CONVERSIONS => 'nb_conversions',
  136. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 'nb_visits_converted',
  137. Metrics::INDEX_GOAL_REVENUE => 'revenue',
  138. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL => 'revenue_subtotal',
  139. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX => 'revenue_tax',
  140. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING => 'revenue_shipping',
  141. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT => 'revenue_discount',
  142. Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 'items',
  143. );
  144. protected static $metricsAggregatedFromLogs = array(
  145. Metrics::INDEX_NB_UNIQ_VISITORS,
  146. Metrics::INDEX_NB_VISITS,
  147. Metrics::INDEX_NB_ACTIONS,
  148. Metrics::INDEX_NB_USERS,
  149. Metrics::INDEX_MAX_ACTIONS,
  150. Metrics::INDEX_SUM_VISIT_LENGTH,
  151. Metrics::INDEX_BOUNCE_COUNT,
  152. Metrics::INDEX_NB_VISITS_CONVERTED,
  153. );
  154. public static function getVisitsMetricNames()
  155. {
  156. $names = array();
  157. foreach (self::$metricsAggregatedFromLogs as $metricId) {
  158. $names[$metricId] = self::$mappingFromIdToName[$metricId];
  159. }
  160. return $names;
  161. }
  162. // TODO: this method is named wrong
  163. public static function getMappingFromIdToName()
  164. {
  165. $idToName = array_flip(self::$mappingFromIdToName);
  166. return $idToName;
  167. }
  168. /**
  169. * Is a lower value for a given column better?
  170. * @param $column
  171. * @return bool
  172. *
  173. * @ignore
  174. */
  175. public static function isLowerValueBetter($column)
  176. {
  177. $lowerIsBetterPatterns = array(
  178. 'bounce', 'exit'
  179. );
  180. foreach ($lowerIsBetterPatterns as $pattern) {
  181. if (strpos($column, $pattern) !== false) {
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. /**
  188. * Derive the unit name from a column name
  189. * @param $column
  190. * @param $idSite
  191. * @return string
  192. * @ignore
  193. */
  194. public static function getUnit($column, $idSite)
  195. {
  196. $nameToUnit = array(
  197. '_rate' => '%',
  198. 'revenue' => MetricsFormatter::getCurrencySymbol($idSite),
  199. '_time_' => 's'
  200. );
  201. foreach ($nameToUnit as $pattern => $type) {
  202. if (strpos($column, $pattern) !== false) {
  203. return $type;
  204. }
  205. }
  206. return '';
  207. }
  208. public static function getDefaultMetricTranslations()
  209. {
  210. $cache = new PluginAwareStaticCache('DefaultMetricTranslations');
  211. if ($cache->has()) {
  212. return $cache->get();
  213. }
  214. $translations = array(
  215. 'label' => 'General_ColumnLabel',
  216. 'date' => 'General_Date',
  217. 'avg_time_on_page' => 'General_ColumnAverageTimeOnPage',
  218. 'sum_time_spent' => 'General_ColumnSumVisitLength',
  219. 'sum_visit_length' => 'General_ColumnSumVisitLength',
  220. 'bounce_count' => 'General_ColumnBounces',
  221. 'bounce_count_returning' => 'VisitFrequency_ColumnBounceCountForReturningVisits',
  222. 'max_actions' => 'General_ColumnMaxActions',
  223. 'max_actions_returning' => 'VisitFrequency_ColumnMaxActionsInReturningVisit',
  224. 'nb_visits_converted_returning' => 'VisitFrequency_ColumnNbReturningVisitsConverted',
  225. 'sum_visit_length_returning' => 'VisitFrequency_ColumnSumVisitLengthReturning',
  226. 'nb_visits_converted' => 'General_ColumnVisitsWithConversions',
  227. 'nb_conversions' => 'Goals_ColumnConversions',
  228. 'revenue' => 'General_ColumnRevenue',
  229. 'nb_hits' => 'General_ColumnPageviews',
  230. 'entry_nb_visits' => 'General_ColumnEntrances',
  231. 'entry_nb_uniq_visitors' => 'General_ColumnUniqueEntrances',
  232. 'exit_nb_visits' => 'General_ColumnExits',
  233. 'exit_nb_uniq_visitors' => 'General_ColumnUniqueExits',
  234. 'entry_bounce_count' => 'General_ColumnBounces',
  235. 'exit_bounce_count' => 'General_ColumnBounces',
  236. 'exit_rate' => 'General_ColumnExitRate',
  237. );
  238. $dailySum = ' (' . Piwik::translate('General_DailySum') . ')';
  239. $afterEntry = ' ' . Piwik::translate('General_AfterEntry');
  240. $translations['sum_daily_nb_uniq_visitors'] = Piwik::translate('General_ColumnNbUniqVisitors') . $dailySum;
  241. $translations['sum_daily_nb_users'] = Piwik::translate('General_ColumnNbUsers') . $dailySum;
  242. $translations['sum_daily_entry_nb_uniq_visitors'] = Piwik::translate('General_ColumnUniqueEntrances') . $dailySum;
  243. $translations['sum_daily_exit_nb_uniq_visitors'] = Piwik::translate('General_ColumnUniqueExits') . $dailySum;
  244. $translations['entry_nb_actions'] = Piwik::translate('General_ColumnNbActions') . $afterEntry;
  245. $translations['entry_sum_visit_length'] = Piwik::translate('General_ColumnSumVisitLength') . $afterEntry;
  246. $translations = array_merge(self::getDefaultMetrics(), self::getDefaultProcessedMetrics(), $translations);
  247. /**
  248. * Use this event to register translations for metrics processed by your plugin.
  249. *
  250. * @param string $translations The array mapping of column_name => Plugin_TranslationForColumn
  251. */
  252. Piwik::postEvent('Metrics.getDefaultMetricTranslations', array(&$translations));
  253. $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
  254. $cache->set($translations);
  255. return $translations;
  256. }
  257. public static function getDefaultMetrics()
  258. {
  259. $cache = new LanguageAwareStaticCache('DefaultMetrics');
  260. if ($cache->has()) {
  261. return $cache->get();
  262. }
  263. $translations = array(
  264. 'nb_visits' => 'General_ColumnNbVisits',
  265. 'nb_uniq_visitors' => 'General_ColumnNbUniqVisitors',
  266. 'nb_actions' => 'General_ColumnNbActions',
  267. 'nb_users' => 'General_ColumnNbUsers',
  268. );
  269. $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
  270. $cache->set($translations);
  271. return $translations;
  272. }
  273. public static function getDefaultProcessedMetrics()
  274. {
  275. $cache = new LanguageAwareStaticCache('DefaultProcessedMetrics');
  276. if ($cache->has()) {
  277. return $cache->get();
  278. }
  279. $translations = array(
  280. // Processed in AddColumnsProcessedMetrics
  281. 'nb_actions_per_visit' => 'General_ColumnActionsPerVisit',
  282. 'avg_time_on_site' => 'General_ColumnAvgTimeOnSite',
  283. 'bounce_rate' => 'General_ColumnBounceRate',
  284. 'conversion_rate' => 'General_ColumnConversionRate',
  285. );
  286. $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
  287. $cache->set($translations);
  288. return $translations;
  289. }
  290. public static function getReadableColumnName($columnIdRaw)
  291. {
  292. $mappingIdToName = self::$mappingFromIdToName;
  293. if (array_key_exists($columnIdRaw, $mappingIdToName)) {
  294. return $mappingIdToName[$columnIdRaw];
  295. }
  296. return $columnIdRaw;
  297. }
  298. public static function getMetricIdsToProcessReportTotal()
  299. {
  300. return array(
  301. self::INDEX_NB_VISITS,
  302. self::INDEX_NB_UNIQ_VISITORS,
  303. self::INDEX_NB_ACTIONS,
  304. self::INDEX_PAGE_NB_HITS,
  305. self::INDEX_NB_VISITS_CONVERTED,
  306. self::INDEX_NB_CONVERSIONS,
  307. self::INDEX_BOUNCE_COUNT,
  308. self::INDEX_PAGE_ENTRY_BOUNCE_COUNT,
  309. self::INDEX_PAGE_ENTRY_NB_VISITS,
  310. self::INDEX_PAGE_ENTRY_NB_ACTIONS,
  311. self::INDEX_PAGE_EXIT_NB_VISITS,
  312. self::INDEX_PAGE_EXIT_NB_UNIQ_VISITORS,
  313. self::INDEX_REVENUE
  314. );
  315. }
  316. public static function getDefaultMetricsDocumentation()
  317. {
  318. $cache = new PluginAwareStaticCache('DefaultMetricsDocumentation');
  319. if ($cache->has()) {
  320. return $cache->get();
  321. }
  322. $translations = array(
  323. 'nb_visits' => 'General_ColumnNbVisitsDocumentation',
  324. 'nb_uniq_visitors' => 'General_ColumnNbUniqVisitorsDocumentation',
  325. 'nb_actions' => 'General_ColumnNbActionsDocumentation',
  326. 'nb_users' => 'General_ColumnNbUsersDocumentation',
  327. 'nb_actions_per_visit' => 'General_ColumnActionsPerVisitDocumentation',
  328. 'avg_time_on_site' => 'General_ColumnAvgTimeOnSiteDocumentation',
  329. 'bounce_rate' => 'General_ColumnBounceRateDocumentation',
  330. 'conversion_rate' => 'General_ColumnConversionRateDocumentation',
  331. 'avg_time_on_page' => 'General_ColumnAverageTimeOnPageDocumentation',
  332. 'nb_hits' => 'General_ColumnPageviewsDocumentation',
  333. 'exit_rate' => 'General_ColumnExitRateDocumentation'
  334. );
  335. /**
  336. * Use this event to register translations for metrics documentation processed by your plugin.
  337. *
  338. * @param string[] $translations The array mapping of column_name => Plugin_TranslationForColumnDocumentation
  339. */
  340. Piwik::postEvent('Metrics.getDefaultMetricDocumentationTranslations', array(&$translations));
  341. $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
  342. $cache->set($translations);
  343. return $translations;
  344. }
  345. public static function getPercentVisitColumn()
  346. {
  347. $percentVisitsLabel = str_replace(' ', '&nbsp;', Piwik::translate('General_ColumnPercentageVisits'));
  348. return $percentVisitsLabel;
  349. }
  350. }