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

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

/**
 * @see plugins/UserCountry/functions.php
 */
require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';

/**
 * The UserCountry API lets you access reports about your visitors' Countries and Continents.
 * @package Piwik_UserCountry
 */
class Piwik_UserCountry_API 
{
	static private $instance = null;
	static public function getInstance()
	{
		if (self::$instance == null)
		{
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function getCountry( $idSite, $period, $date, $segment = false )
	{
		$recordName = Piwik_UserCountry::VISITS_BY_COUNTRY_RECORD_NAME;
		$dataTable = $this->getDataTable($recordName, $idSite, $period, $date, $segment);
		
		// apply filter on the whole datatable in order the inline search to work (searches
		// are done on "beautiful" label)
		$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'code'));
		$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', 'Piwik_getFlagFromCode'));
		$dataTable->filter('ColumnCallbackReplace', array('label', 'Piwik_CountryTranslate'));
		$dataTable->queueFilter('AddConstantMetadata', array('logoWidth', 16));
		$dataTable->queueFilter('AddConstantMetadata', array('logoHeight', 11));
		
		return $dataTable;
	}
	
	public function getContinent( $idSite, $period, $date, $segment = false )
	{
		$recordName = Piwik_UserCountry::VISITS_BY_COUNTRY_RECORD_NAME;
		$dataTable = $this->getDataTable($recordName, $idSite, $period, $date, $segment);
		
		$getContinent = array('Piwik_Common', 'getContinent');
		$dataTable->filter('GroupBy', array('label', $getContinent));
		
		$dataTable->filter('ColumnCallbackReplace', array('label', 'Piwik_ContinentTranslate'));
		$dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'code'));
		
		return $dataTable;
	}
	
	/**
	 * Returns visit information for every region with at least one visit.
	 * 
	 * @param int|string $idSite
	 * @param string $period
	 * @param string $date
	 * @param string|bool $segment
	 * @return Piwik_DataTable
	 */
	public function getRegion( $idSite, $period, $date, $segment = false )
	{
		$recordName = Piwik_UserCountry::VISITS_BY_REGION_RECORD_NAME;
		$dataTable = $this->getDataTable($recordName, $idSite, $period, $date, $segment);
		
		$separator = Piwik_UserCountry::LOCATION_SEPARATOR;
		$unk = Piwik_Tracker_Visit::UNKNOWN_CODE;
		
		// split the label and put the elements into the 'region' and 'country' metadata fields
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'region', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 0, $unk)));
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'country', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 1, $unk)));
		
		// add country name metadata
		$dataTable->filter('MetadataCallbackAddMetadata',
			array('country', 'country_name', 'Piwik_CountryTranslate', $applyToSummaryRow = false));
		
		// get the region name of each row and put it into the 'region_name' metadata
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'region_name', 'Piwik_UserCountry_getRegionName', $params = null,
				  $applyToSummaryRow = false));
		
		// add the country flag as a url to the 'logo' metadata field
		$dataTable->filter('MetadataCallbackAddMetadata', array('country', 'logo', 'Piwik_getFlagFromCode'));
		
		// prettify the region label
		$dataTable->filter('ColumnCallbackReplace', array('label', 'Piwik_UserCountry_getPrettyRegionName'));
		
		$dataTable->queueFilter('ReplaceSummaryRowLabel');
		
		return $dataTable;
	}
	
	/**
	 * Returns visit information for every city with at least one visit.
	 * 
	 * @param int|string $idSite
	 * @param string $period
	 * @param string $date
	 * @param string|bool $segment
	 * @return Piwik_DataTable
	 */
	public function getCity( $idSite, $period, $date, $segment = false )
	{
		$recordName = Piwik_UserCountry::VISITS_BY_CITY_RECORD_NAME;
		$dataTable = $this->getDataTable($recordName, $idSite, $period, $date, $segment);
		
		$separator = Piwik_UserCountry::LOCATION_SEPARATOR;
		$unk = Piwik_Tracker_Visit::UNKNOWN_CODE;
		
		// split the label and put the elements into the 'city_name', 'region', 'country',
		// 'lat' & 'long' metadata fields
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'city_name', 'Piwik_UserCountry_getElementFromStringArray',
				  array($separator, 0, Piwik_Translate('General_Unknown')) ));
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'region', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 1, $unk)));
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'country', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 2, $unk)));
		
		// backwards compatibility: for reports that have lat|long in label
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'lat', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 3, false)));
		$dataTable->filter('ColumnCallbackAddMetadata',
			array('label', 'long', 'Piwik_UserCountry_getElementFromStringArray', array($separator, 4, false)));
		
		// add country name & region name metadata
		$dataTable->filter('MetadataCallbackAddMetadata',
			array('country', 'country_name', 'Piwik_CountryTranslate', $applyToSummaryRow = false));
		
		$getRegionName = array('Piwik_UserCountry_LocationProvider_GeoIp', 'getRegionNameFromCodes');
		$dataTable->filter('MetadataCallbackAddMetadata', array(
			array('country', 'region'), 'region_name', $getRegionName, $applyToSummaryRow = false));
		
		// add the country flag as a url to the 'logo' metadata field
		$dataTable->filter('MetadataCallbackAddMetadata', array('country', 'logo', 'Piwik_getFlagFromCode'));
		
		// prettify the label
		$dataTable->filter('ColumnCallbackReplace', array('label', 'Piwik_UserCountry_getPrettyCityName'));
		
		$dataTable->queueFilter('ReplaceSummaryRowLabel');
		
		return $dataTable;
	}
	
	protected function getDataTable($name, $idSite, $period, $date, $segment)
	{
		Piwik::checkUserHasViewAccess( $idSite );
		$archive = Piwik_Archive::build($idSite, $period, $date, $segment );
		$dataTable = $archive->getDataTable($name);
		$dataTable->filter('Sort', array(Piwik_Archive::INDEX_NB_VISITS));
		$dataTable->queueFilter('ReplaceColumnNames');
		return $dataTable;
	}
	
	public function getNumberOfDistinctCountries($idSite, $period, $date, $segment = false)
	{
		Piwik::checkUserHasViewAccess( $idSite );
		$archive = Piwik_Archive::build($idSite, $period, $date, $segment );
		return $archive->getDataTableFromNumeric('UserCountry_distinctCountries');
	}
}