Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UpdateColumnsWhenShowAllGoals.php « Filter « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26e296e2e23d726a6bb357cdb64a5dcff7d2c038 (plain)
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
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 * 
 * @category Piwik
 * @package Piwik
 */

/**
 * @package Piwik
 * @subpackage Piwik_DataTable
 */
class Piwik_DataTable_Filter_UpdateColumnsWhenShowAllGoals extends Piwik_DataTable_Filter
{
	const GOALS_OVERVIEW = -1;
	const GOALS_FULL_TABLE = 0;
	
	protected $mappingIdToNameGoal;
	
	/**
	 * @param $table
	 * @param $enable Automatically set to true when filter_update_columns_when_show_all_goals is found in the API request
	 * @param $processOnlyIdGoal
	 * @return unknown_type
	 */
	public function __construct( $table, $enable = true, $processOnlyIdGoal )
	{
		parent::__construct($table);
		$this->mappingIdToNameGoal = Piwik_Archive::$mappingFromIdToNameGoal;
		$this->processOnlyIdGoal = $processOnlyIdGoal;
		$this->filter();
	}
	
	protected function filter()
	{
		$invalidDivision = 'N/A';
		$roundingPrecision = 2;
		$expectedColumns = array();
		foreach($this->table->getRows() as $key => $row)
		{
			$currentColumns = $row->getColumns();
			$newColumns = array();
			
			$nbVisits = 0;
			// visits could be undefined when there is a conversion but no visit
			if(isset($currentColumns[Piwik_Archive::INDEX_NB_VISITS]))
			{
				$nbVisits = $currentColumns[Piwik_Archive::INDEX_NB_VISITS];
			}
			$newColumns['nb_visits'] = $nbVisits;
			$newColumns['label'] = $currentColumns['label'];
			if(isset($currentColumns[Piwik_Archive::INDEX_GOALS]))
			{
				$nbVisitsConverted = $revenue = 0;
				if(isset($currentColumns[Piwik_Archive::INDEX_NB_VISITS_CONVERTED]))
				{
					$nbVisitsConverted = $currentColumns[Piwik_Archive::INDEX_NB_VISITS_CONVERTED];
					$revenue = $currentColumns[Piwik_Archive::INDEX_REVENUE];
				}
	
				if($nbVisitsConverted == 0)
				{
					$conversionRate = $invalidDivision;
				}
				else
				{
					$conversionRate = round(100 * $nbVisitsConverted / $nbVisits, $roundingPrecision);
				}
				$newColumns['goals_conversion_rate'] = $conversionRate;
				
				if($nbVisits == 0)
				{
					$revenuePerVisit = $invalidDivision;
				}
				else
				{
					$revenuePerVisit = round( $revenue / $nbVisits, $roundingPrecision );
				}
				$newColumns['revenue_per_visit'] = $revenuePerVisit;
				foreach($currentColumns[Piwik_Archive::INDEX_GOALS] as $goalId => $columnValue)
				{
					if($this->processOnlyIdGoal > self::GOALS_FULL_TABLE
						&& $this->processOnlyIdGoal != $goalId)
					{
						continue;
					}
					
					// Goal Conversion rate
					$name = 'goal_' . $goalId . '_conversion_rate';
					if($nbVisits == 0)
					{
						$value = $invalidDivision;
					}
					else
					{
						$value = round(100 * $columnValue[Piwik_Archive::INDEX_GOAL_NB_CONVERSIONS] / $nbVisits, $roundingPrecision);
					}
					$newColumns[$name] = $value;
					$expectedColumns[$name] = true;
					
					// When the table is displayed by clicking on the flag icon, we only display the columns
					// Visits, Conversions, Per goal conversion rate, Revenue
					if($this->processOnlyIdGoal == self::GOALS_OVERVIEW)
					{
						continue;
					}
					
					// Goal Conversions
					$name = 'goal_' . $goalId . '_nb_conversions';
					$newColumns[$name] = $columnValue[Piwik_Archive::INDEX_GOAL_NB_CONVERSIONS];
					$expectedColumns[$name] = true;
					
					// Goal Revenue per visit
					$name = 'goal_' . $goalId . '_revenue_per_visit';
					if($nbVisits == 0)
					{
						$value = $invalidDivision;
					}
					else
					{
						$revenuePerVisit = round( $columnValue[Piwik_Archive::INDEX_GOAL_REVENUE] / $nbVisits, $roundingPrecision );
					}
					$newColumns[$name] = $revenuePerVisit;
					$expectedColumns[$name] = true;
					
				}
			}
			
			$row->setColumns($newColumns);
		}
		$expectedColumns['revenue_per_visit'] = true;
		$expectedColumns['goals_conversion_rate'] = true;
		
		// make sure all goals values are set, 0 by default
		// if no value then sorting would put at the end
		$expectedColumns = array_keys($expectedColumns);
		$rows = $this->table->getRows();
		foreach($rows as &$row)
		{
			foreach($expectedColumns as $name)
			{
				if(false === $row->getColumn($name))
				{
					$row->addColumn($name, 0);
				}
			}
		}
	}
}