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

API.php « VisitsSummary « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cdd688d275d766f18a2f8a8e4ed4c90d488c8b19 (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
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
<?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_Plugins
 * @package Piwik_VisitsSummary
 */

/**
 * VisitsSummary API lets you access the core web analytics metrics (visits, unique visitors,
 * count of actions (page views & downloads & clicks on outlinks), time on site, bounces and converted visits.
 *
 * @package Piwik_VisitsSummary
 */
class Piwik_VisitsSummary_API
{
	static private $instance = null;
	/**
	 * @return Piwik_VisitsSummary_API
	 */
	static public function getInstance()
	{
		if (self::$instance == null)
		{
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function get( $idSite, $period, $date, $segment = false, $columns = false)
	{
		Piwik::checkUserHasViewAccess( $idSite );
		$archive = Piwik_Archive::build($idSite, $period, $date, $segment );
	
		// array values are comma separated
		$columns = Piwik::getArrayFromApiParameter($columns);
		$countColumnsRequested = count($columns);
		
		$allColumns = true;
		$bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = false;
		if(!empty($columns))
		{
			$allColumns = false;
			if(($bounceRateRequested = array_search('bounce_rate', $columns)) !== false)
			{
				$columns = array('nb_visits', 'bounce_count');
			}
			elseif(($actionsPerVisitRequested = array_search('nb_actions_per_visit', $columns)) !== false)
			{
				$columns = array('nb_actions', 'nb_visits');
			}
			elseif(($averageVisitDurationRequested = array_search('avg_time_on_site', $columns)) !== false)
			{
				$columns = array('sum_visit_length', 'nb_visits');
			}
		}
		else
		{
    		$bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = true;
			$columns = array(
								'nb_visits',
								'nb_uniq_visitors',
								'nb_actions',
								'nb_visits_converted',
								'bounce_count',
								'sum_visit_length',
								'max_actions'
							);
			if(!Piwik::isUniqueVisitorsEnabled($period))
			{
				unset($columns[array_search('nb_uniq_visitors', $columns)]);
			}
			// Force reindex from 0 to N otherwise the SQL bind will fail
			$columns = array_values($columns);
		}

		$dataTable = $archive->getDataTableFromNumeric($columns);
		
		// Process ratio metrics from base metrics, when requested
		if($bounceRateRequested !== false)
		{
			$dataTable->filter('ColumnCallbackAddColumnPercentage', array('bounce_rate', 'bounce_count', 'nb_visits', 0));
		}
		if($actionsPerVisitRequested !== false)
		{
			$dataTable->filter('ColumnCallbackAddColumnQuotient', array('nb_actions_per_visit', 'nb_actions', 'nb_visits', 1));
		}
		if($averageVisitDurationRequested !== false)
		{
			$dataTable->filter('ColumnCallbackAddColumnQuotient', array('avg_time_on_site', 'sum_visit_length', 'nb_visits', 0));
		}
		
		// include action counts from actions plugin
		$actionCounts = Piwik_API_Proxy::getInstance()->call('Piwik_Actions_API', 'getActionCounts', compact('idSite', 'period', 'date', 'segment'));
		$this->mergeDataTables($dataTable, $actionCounts);
		
		// If only a computed metrics was requested, we delete other metrics
		// that we selected only to process this one metric
		if($countColumnsRequested == 1
			&& ($bounceRateRequested || $actionsPerVisitRequested || $averageVisitDurationRequested)
			)
		{
			$dataTable->deleteColumns($columns);
		}
		return $dataTable;
	}
	
	/**
	 * merge the columns of two data tables
	 * used to add the action counts to the visits summary
	 * manipulates the first table
	 */ 
	private function mergeDataTables($table1, $table2, $allColumns=true, $columns=array())
	{
		// handle table arrays
		if ($table1 instanceof Piwik_DataTable_Array && $table2 instanceof Piwik_DataTable_Array)
		{
			$subTables2 = $table2->getArray();
			foreach ($table1->getArray() as $index => $subTable1)
			{
				$subTable2 = $subTables2[$index];
				$this->mergeDataTables($subTable1, $subTable2);
			}
			return;
		}
		
		$firstRow2 = $table2->getFirstRow();
		if (!$firstRow2)
		{
			// nothing to add
			return;
		}
		
		$firstRow1 = $table1->getFirstRow();
		if (!$firstRow1)
		{
			// first table has no row yet
			$firstRow1 = new Piwik_DataTable_Row;
			$table1->addRow($firstRow1);
		}
		
		foreach ($firstRow2->getColumns() as $metric => $value)
		{
			if (!$allColumns && !in_array($metric, $columns))
			{
				// only add the columns that have been requested
				continue;
			}
			$firstRow1->setColumn($metric, $value);
		}
	}
	
	protected function getNumeric( $idSite, $period, $date, $segment, $toFetch )
	{
		Piwik::checkUserHasViewAccess( $idSite );
		$archive = Piwik_Archive::build($idSite, $period, $date, $segment );
		$dataTable = $archive->getNumeric($toFetch);
		return $dataTable;
	}

	public function getVisits( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'nb_visits');
	}
	
	public function getUniqueVisitors( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'nb_uniq_visitors');
	}
	
	public function getActions( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'nb_actions');
	}
	
	public function getMaxActions( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'max_actions');
	}
	
	public function getBounceCount( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'bounce_count');
	}
	
	public function getVisitsConverted( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'nb_visits_converted');
	}
	
	public function getSumVisitsLength( $idSite, $period, $date, $segment = false )
	{
		return $this->getNumeric( $idSite, $period, $date, $segment, 'sum_visit_length');
	}
	
	public function getSumVisitsLengthPretty( $idSite, $period, $date, $segment = false )
	{
		$table = $this->getSumVisitsLength( $idSite, $period, $date, $segment );
		if($table instanceof Piwik_DataTable_Array) {
			$table->filter('ColumnCallbackReplace', array(0, array('Piwik', 'getPrettyTimeFromSeconds')));
		} else {
			$table = Piwik::getPrettyTimeFromSeconds($table);
		}
		return $table;
	}
}