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.

DataArray.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 Exception;
  11. use Piwik\Tracker\GoalManager;
  12. /**
  13. * The DataArray is a data structure used to aggregate datasets,
  14. * ie. sum arrays made of rows made of columns,
  15. * data from the logs is stored in a DataArray before being converted in a DataTable
  16. *
  17. */
  18. class DataArray
  19. {
  20. protected $data = array();
  21. protected $dataTwoLevels = array();
  22. public function __construct($data = array(), $dataArrayByLabel = array())
  23. {
  24. $this->data = $data;
  25. $this->dataTwoLevels = $dataArrayByLabel;
  26. }
  27. /**
  28. * This returns the actual raw data array
  29. *
  30. * @return array
  31. */
  32. public function &getDataArray()
  33. {
  34. return $this->data;
  35. }
  36. public function getDataArrayWithTwoLevels()
  37. {
  38. return $this->dataTwoLevels;
  39. }
  40. public function sumMetricsVisits($label, $row)
  41. {
  42. if (!isset($this->data[$label])) {
  43. $this->data[$label] = self::makeEmptyRow();
  44. }
  45. $this->doSumVisitsMetrics($row, $this->data[$label]);
  46. }
  47. /**
  48. * Returns an empty row containing default metrics
  49. *
  50. * @return array
  51. */
  52. public static function makeEmptyRow()
  53. {
  54. return array(Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  55. Metrics::INDEX_NB_VISITS => 0,
  56. Metrics::INDEX_NB_ACTIONS => 0,
  57. Metrics::INDEX_NB_USERS => 0,
  58. Metrics::INDEX_MAX_ACTIONS => 0,
  59. Metrics::INDEX_SUM_VISIT_LENGTH => 0,
  60. Metrics::INDEX_BOUNCE_COUNT => 0,
  61. Metrics::INDEX_NB_VISITS_CONVERTED => 0,
  62. );
  63. }
  64. /**
  65. * Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference
  66. * The rows are php arrays Name => value
  67. *
  68. * @param array $newRowToAdd
  69. * @param array $oldRowToUpdate
  70. * @param bool $onlyMetricsAvailableInActionsTable
  71. *
  72. * @return void
  73. */
  74. protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate, $onlyMetricsAvailableInActionsTable = false)
  75. {
  76. // Pre 1.2 format: string indexed rows are returned from the DB
  77. // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
  78. if (!isset($newRowToAdd[Metrics::INDEX_NB_VISITS])) {
  79. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
  80. $oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
  81. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
  82. if ($onlyMetricsAvailableInActionsTable) {
  83. return;
  84. }
  85. $oldRowToUpdate[Metrics::INDEX_NB_USERS] += $newRowToAdd['nb_users'];
  86. $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS] = (float)max($newRowToAdd['max_actions'], $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS]);
  87. $oldRowToUpdate[Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd['sum_visit_length'];
  88. $oldRowToUpdate[Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd['bounce_count'];
  89. $oldRowToUpdate[Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd['nb_visits_converted'];
  90. return;
  91. }
  92. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
  93. $oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[Metrics::INDEX_NB_ACTIONS];
  94. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
  95. if ($onlyMetricsAvailableInActionsTable) {
  96. return;
  97. }
  98. // In case the existing Row had no action metrics (eg. Custom Variable XYZ with "visit" scope)
  99. // but the new Row has action metrics (eg. same Custom Variable XYZ this time with a "page" scope)
  100. if(!isset($oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS])) {
  101. $toZero = array(Metrics::INDEX_MAX_ACTIONS,
  102. Metrics::INDEX_SUM_VISIT_LENGTH,
  103. Metrics::INDEX_BOUNCE_COUNT,
  104. Metrics::INDEX_NB_VISITS_CONVERTED);
  105. foreach($toZero as $metric) {
  106. $oldRowToUpdate[$metric] = 0;
  107. }
  108. }
  109. $oldRowToUpdate[Metrics::INDEX_NB_USERS] += $newRowToAdd[Metrics::INDEX_NB_USERS];
  110. $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS] = (float)max($newRowToAdd[Metrics::INDEX_MAX_ACTIONS], $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS]);
  111. $oldRowToUpdate[Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd[Metrics::INDEX_SUM_VISIT_LENGTH];
  112. $oldRowToUpdate[Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd[Metrics::INDEX_BOUNCE_COUNT];
  113. $oldRowToUpdate[Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd[Metrics::INDEX_NB_VISITS_CONVERTED];
  114. }
  115. public function sumMetricsGoals($label, $row)
  116. {
  117. $idGoal = $row['idgoal'];
  118. if (!isset($this->data[$label][Metrics::INDEX_GOALS][$idGoal])) {
  119. $this->data[$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
  120. }
  121. $this->doSumGoalsMetrics($row, $this->data[$label][Metrics::INDEX_GOALS][$idGoal]);
  122. }
  123. /**
  124. * @param $idGoal
  125. * @return array
  126. */
  127. protected static function makeEmptyGoalRow($idGoal)
  128. {
  129. if ($idGoal > GoalManager::IDGOAL_ORDER) {
  130. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  131. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  132. Metrics::INDEX_GOAL_REVENUE => 0,
  133. );
  134. }
  135. if ($idGoal == GoalManager::IDGOAL_ORDER) {
  136. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  137. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  138. Metrics::INDEX_GOAL_REVENUE => 0,
  139. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL => 0,
  140. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX => 0,
  141. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING => 0,
  142. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT => 0,
  143. Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0,
  144. );
  145. }
  146. // idGoal == GoalManager::IDGOAL_CART
  147. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  148. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  149. Metrics::INDEX_GOAL_REVENUE => 0,
  150. Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0,
  151. );
  152. }
  153. /**
  154. *
  155. * @param $newRowToAdd
  156. * @param $oldRowToUpdate
  157. */
  158. protected function doSumGoalsMetrics($newRowToAdd, &$oldRowToUpdate)
  159. {
  160. $oldRowToUpdate[Metrics::INDEX_GOAL_NB_CONVERSIONS] += $newRowToAdd[Metrics::INDEX_GOAL_NB_CONVERSIONS];
  161. $oldRowToUpdate[Metrics::INDEX_GOAL_NB_VISITS_CONVERTED] += $newRowToAdd[Metrics::INDEX_GOAL_NB_VISITS_CONVERTED];
  162. $oldRowToUpdate[Metrics::INDEX_GOAL_REVENUE] += $newRowToAdd[Metrics::INDEX_GOAL_REVENUE];
  163. // Cart & Order
  164. if (isset($oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS])) {
  165. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS];
  166. // Order only
  167. if (isset($oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL])) {
  168. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL];
  169. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX];
  170. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING];
  171. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT];
  172. }
  173. }
  174. }
  175. public function sumMetricsActions($label, $row)
  176. {
  177. if (!isset($this->data[$label])) {
  178. $this->data[$label] = self::makeEmptyActionRow();
  179. }
  180. $this->doSumVisitsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
  181. }
  182. protected static function makeEmptyActionRow()
  183. {
  184. return array(
  185. Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  186. Metrics::INDEX_NB_VISITS => 0,
  187. Metrics::INDEX_NB_ACTIONS => 0,
  188. );
  189. }
  190. public function sumMetricsEvents($label, $row)
  191. {
  192. if (!isset($this->data[$label])) {
  193. $this->data[$label] = self::makeEmptyEventRow();
  194. }
  195. $this->doSumEventsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
  196. }
  197. protected static function makeEmptyEventRow()
  198. {
  199. return array(
  200. Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  201. Metrics::INDEX_NB_VISITS => 0,
  202. Metrics::INDEX_EVENT_NB_HITS => 0,
  203. Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE => 0,
  204. Metrics::INDEX_EVENT_SUM_EVENT_VALUE => 0,
  205. Metrics::INDEX_EVENT_MIN_EVENT_VALUE => 0,
  206. Metrics::INDEX_EVENT_MAX_EVENT_VALUE => 0,
  207. );
  208. }
  209. const EVENT_VALUE_PRECISION = 2;
  210. /**
  211. * @param array $newRowToAdd
  212. * @param array $oldRowToUpdate
  213. * @return void
  214. */
  215. protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate)
  216. {
  217. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
  218. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
  219. $oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS];
  220. $oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE];
  221. $newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
  222. $oldRowToUpdate[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE];
  223. $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = round(max($newRowToAdd[Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
  224. // Update minimum only if it is set
  225. if($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== false) {
  226. if($oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] === false) {
  227. $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
  228. } else {
  229. $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round(min($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
  230. }
  231. }
  232. }
  233. /**
  234. * Generic function that will sum all columns of the given row, at the specified label's row.
  235. *
  236. * @param $label
  237. * @param $row
  238. * @throws Exception if the the data row contains non numeric values
  239. */
  240. public function sumMetrics($label, $row)
  241. {
  242. foreach ($row as $columnName => $columnValue) {
  243. if (empty($columnValue)) {
  244. continue;
  245. }
  246. if (empty($this->data[$label][$columnName])) {
  247. $this->data[$label][$columnName] = 0;
  248. }
  249. if (!is_numeric($columnValue)) {
  250. throw new Exception("DataArray->sumMetricsPivot expects rows of numeric values, non numeric found: " . var_export($columnValue, true) . " for column $columnName");
  251. }
  252. $this->data[$label][$columnName] += $columnValue;
  253. }
  254. }
  255. public function sumMetricsVisitsPivot($parentLabel, $label, $row)
  256. {
  257. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  258. $this->dataTwoLevels[$parentLabel][$label] = self::makeEmptyRow();
  259. }
  260. $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
  261. }
  262. public function sumMetricsGoalsPivot($parentLabel, $label, $row)
  263. {
  264. $idGoal = $row['idgoal'];
  265. if (!isset($this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal])) {
  266. $this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
  267. }
  268. $this->doSumGoalsMetrics($row, $this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal]);
  269. }
  270. public function sumMetricsActionsPivot($parentLabel, $label, $row)
  271. {
  272. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  273. $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyActionRow();
  274. }
  275. $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label], $onlyMetricsAvailableInActionsTable = true);
  276. }
  277. public function sumMetricsEventsPivot($parentLabel, $label, $row)
  278. {
  279. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  280. $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyEventRow();
  281. }
  282. $this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
  283. }
  284. public function setRowColumnPivot($parentLabel, $label, $column, $value)
  285. {
  286. $this->dataTwoLevels[$parentLabel][$label][$column] = $value;
  287. }
  288. public function enrichMetricsWithConversions()
  289. {
  290. $this->enrichWithConversions($this->data);
  291. foreach ($this->dataTwoLevels as &$metricsBySubLabel) {
  292. $this->enrichWithConversions($metricsBySubLabel);
  293. }
  294. }
  295. /**
  296. * Given an array of stats, it will process the sum of goal conversions
  297. * and sum of revenue and add it in the stats array in two new fields.
  298. *
  299. * @param array $data Passed by reference, two new columns
  300. * will be added: total conversions, and total revenue, for all goals for this label/row
  301. */
  302. protected function enrichWithConversions(&$data)
  303. {
  304. foreach ($data as &$values) {
  305. if (!isset($values[Metrics::INDEX_GOALS])) {
  306. continue;
  307. }
  308. // When per goal metrics are processed, general 'visits converted' is not meaningful because
  309. // it could differ from the sum of each goal conversions
  310. unset($values[Metrics::INDEX_NB_VISITS_CONVERTED]);
  311. $revenue = $conversions = 0;
  312. foreach ($values[Metrics::INDEX_GOALS] as $idgoal => $goalValues) {
  313. // Do not sum Cart revenue since it is a lost revenue
  314. if ($idgoal >= GoalManager::IDGOAL_ORDER) {
  315. $revenue += $goalValues[Metrics::INDEX_GOAL_REVENUE];
  316. $conversions += $goalValues[Metrics::INDEX_GOAL_NB_CONVERSIONS];
  317. }
  318. }
  319. $values[Metrics::INDEX_NB_CONVERSIONS] = $conversions;
  320. // 25.00 recorded as 25
  321. if (round($revenue) == $revenue) {
  322. $revenue = round($revenue);
  323. }
  324. $values[Metrics::INDEX_REVENUE] = $revenue;
  325. // if there are no "visit" column, we force one to prevent future complications
  326. // eg. This helps the setDefaultColumnsToDisplay() call
  327. if(!isset($values[Metrics::INDEX_NB_VISITS])) {
  328. $values[Metrics::INDEX_NB_VISITS] = 0;
  329. }
  330. }
  331. }
  332. /**
  333. * Returns true if the row looks like an Action metrics row
  334. *
  335. * @param $row
  336. * @return bool
  337. */
  338. public static function isRowActions($row)
  339. {
  340. return (count($row) == count(self::makeEmptyActionRow())) && isset($row[Metrics::INDEX_NB_ACTIONS]);
  341. }
  342. /**
  343. * Converts array to a datatable
  344. *
  345. * @return \Piwik\DataTable
  346. */
  347. public function asDataTable()
  348. {
  349. $dataArray = $this->getDataArray();
  350. $dataArrayTwoLevels = $this->getDataArrayWithTwoLevels();
  351. $subtableByLabel = null;
  352. if (!empty($dataArrayTwoLevels)) {
  353. $subtableByLabel = array();
  354. foreach ($dataArrayTwoLevels as $label => $subTable) {
  355. $subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable);
  356. }
  357. }
  358. return DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);
  359. }
  360. }