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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\UserCountry;
use Piwik\ArchiveProcessor;
use Piwik\Common;
use Piwik\Config;
use Piwik\IP;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
use Piwik\Url;
/**
* @see plugins/UserCountry/GeoIPAutoUpdater.php
*/
require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/GeoIPAutoUpdater.php';
/**
*
*/
class UserCountry extends \Piwik\Plugin
{
/**
* @see Piwik\Plugin::getListHooksRegistered
*/
public function getListHooksRegistered()
{
$hooks = array(
'Goals.getReportsWithGoalMetrics' => 'getReportsWithGoalMetrics',
'API.getReportMetadata' => 'getReportMetadata',
'API.getSegmentDimensionMetadata' => 'getSegmentsMetadata',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'Tracker.newVisitorInformation' => 'enrichVisitWithLocation',
'ViewDataTable.configure' => 'configureViewDataTable',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'Tracker.setTrackerCacheGeneral' => 'setTrackerCacheGeneral',
'Insights.addReportToOverview' => 'addReportToInsightsOverview'
);
return $hooks;
}
public function addReportToInsightsOverview(&$reports)
{
$reports['UserCountry_getCountry'] = array();
}
public function setTrackerCacheGeneral(&$cache)
{
$cache['currentLocationProviderId'] = LocationProvider::getCurrentProviderId();
}
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/UserCountry/stylesheets/userCountry.less";
}
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = "plugins/UserCountry/javascripts/userCountry.js";
}
public function enrichVisitWithLocation(&$visitorInfo, \Piwik\Tracker\Request $request)
{
require_once PIWIK_INCLUDE_PATH . "/plugins/UserCountry/LocationProvider.php";
$privacyConfig = new PrivacyManagerConfig();
$ipAddress = IP::N2P($privacyConfig->useAnonymizedIpForVisitEnrichment ? $visitorInfo['location_ip'] : $request->getIp());
$userInfo = array(
'lang' => $visitorInfo['location_browser_lang'],
'ip' => $ipAddress
);
$id = Common::getCurrentLocationProviderId();
$provider = LocationProvider::getProviderById($id);
if ($provider === false) {
$id = DefaultProvider::ID;
$provider = LocationProvider::getProviderById($id);
Common::printDebug("GEO: no current location provider sent, falling back to default '$id' one.");
}
$location = $provider->getLocation($userInfo);
// if we can't find a location, use default provider
if ($location === false) {
$defaultId = DefaultProvider::ID;
$provider = LocationProvider::getProviderById($defaultId);
$location = $provider->getLocation($userInfo);
Common::printDebug("GEO: couldn't find a location with Geo Module '$id', using Default '$defaultId' provider as fallback...");
$id = $defaultId;
}
Common::printDebug("GEO: Found IP $ipAddress location (provider '" . $id . "'): " . var_export($location, true));
if (empty($location['country_code'])) { // sanity check
$location['country_code'] = \Piwik\Tracker\Visit::UNKNOWN_CODE;
}
// add optional location components
$this->updateVisitInfoWithLocation($visitorInfo, $location);
}
/**
* Sets visitor info array with location info.
*
* @param array $visitorInfo
* @param array $location See LocationProvider::getLocation for more info.
*/
private function updateVisitInfoWithLocation(&$visitorInfo, $location)
{
static $logVisitToLowerLocationMapping = array(
'location_country' => LocationProvider::COUNTRY_CODE_KEY,
);
static $logVisitToLocationMapping = array(
'location_region' => LocationProvider::REGION_CODE_KEY,
'location_city' => LocationProvider::CITY_NAME_KEY,
'location_latitude' => LocationProvider::LATITUDE_KEY,
'location_longitude' => LocationProvider::LONGITUDE_KEY,
);
foreach ($logVisitToLowerLocationMapping as $column => $locationKey) {
if (!empty($location[$locationKey])) {
$visitorInfo[$column] = strtolower($location[$locationKey]);
}
}
foreach ($logVisitToLocationMapping as $column => $locationKey) {
if (!empty($location[$locationKey])) {
$visitorInfo[$column] = $location[$locationKey];
}
}
// if the location has provider/organization info, set it
if (!empty($location[LocationProvider::ISP_KEY])) {
$providerValue = $location[LocationProvider::ISP_KEY];
// if the org is set and not the same as the isp, add it to the provider value
if (!empty($location[LocationProvider::ORG_KEY])
&& $location[LocationProvider::ORG_KEY] != $providerValue
) {
$providerValue .= ' - ' . $location[LocationProvider::ORG_KEY];
}
} else if (!empty($location[LocationProvider::ORG_KEY])) {
$providerValue = $location[LocationProvider::ORG_KEY];
}
if (isset($providerValue)
&& Manager::getInstance()->isPluginInstalled('Provider')) {
$visitorInfo['location_provider'] = $providerValue;
}
}
public function getSegmentsMetadata(&$segments)
{
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_Country'),
'segment' => 'countryCode',
'sqlSegment' => 'log_visit.location_country',
'acceptedValues' => 'de, us, fr, in, es, etc.',
);
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_Continent'),
'segment' => 'continentCode',
'sqlSegment' => 'log_visit.location_country',
'acceptedValues' => 'eur, asi, amc, amn, ams, afr, ant, oce',
'sqlFilter' => __NAMESPACE__ . '\UserCountry::getCountriesForContinent',
);
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_Region'),
'segment' => 'regionCode',
'sqlSegment' => 'log_visit.location_region',
'acceptedValues' => '01 02, OR, P8, etc.<br/>eg. region=A1;country=fr',
);
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_City'),
'segment' => 'city',
'sqlSegment' => 'log_visit.location_city',
'acceptedValues' => 'Sydney, Sao Paolo, Rome, etc.',
);
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_Latitude'),
'segment' => 'latitude',
'sqlSegment' => 'log_visit.location_latitude',
'acceptedValues' => '-33.578, 40.830, etc.<br/>You can select visitors within a lat/long range using &segment=lat>X;lat<Y;long>M;long<N.',
);
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit Location',
'name' => Piwik::translate('UserCountry_Longitude'),
'segment' => 'longitude',
'sqlSegment' => 'log_visit.location_longitude',
'acceptedValues' => '-70.664, 14.326, etc.',
);
}
public function getReportMetadata(&$reports)
{
$metrics = array(
'nb_visits' => Piwik::translate('General_ColumnNbVisits'),
'nb_uniq_visitors' => Piwik::translate('General_ColumnNbUniqVisitors'),
'nb_actions' => Piwik::translate('General_ColumnNbActions'),
);
$reports[] = array(
'category' => Piwik::translate('General_Visitors'),
'name' => Piwik::translate('UserCountry_Country'),
'module' => 'UserCountry',
'action' => 'getCountry',
'dimension' => Piwik::translate('UserCountry_Country'),
'metrics' => $metrics,
'order' => 5,
);
$reports[] = array(
'category' => Piwik::translate('General_Visitors'),
'name' => Piwik::translate('UserCountry_Continent'),
'module' => 'UserCountry',
'action' => 'getContinent',
'dimension' => Piwik::translate('UserCountry_Continent'),
'metrics' => $metrics,
'order' => 6,
);
$reports[] = array(
'category' => Piwik::translate('General_Visitors'),
'name' => Piwik::translate('UserCountry_Region'),
'module' => 'UserCountry',
'action' => 'getRegion',
'dimension' => Piwik::translate('UserCountry_Region'),
'metrics' => $metrics,
'order' => 7,
);
$reports[] = array(
'category' => Piwik::translate('General_Visitors'),
'name' => Piwik::translate('UserCountry_City'),
'module' => 'UserCountry',
'action' => 'getCity',
'dimension' => Piwik::translate('UserCountry_City'),
'metrics' => $metrics,
'order' => 8,
);
}
public function getReportsWithGoalMetrics(&$dimensions)
{
$dimensions = array_merge($dimensions, array(
array('category' => Piwik::translate('General_Visit'),
'name' => Piwik::translate('UserCountry_Country'),
'module' => 'UserCountry',
'action' => 'getCountry',
),
array('category' => Piwik::translate('General_Visit'),
'name' => Piwik::translate('UserCountry_Continent'),
'module' => 'UserCountry',
'action' => 'getContinent',
),
array('category' => Piwik::translate('General_Visit'),
'name' => Piwik::translate('UserCountry_Region'),
'module' => 'UserCountry',
'action' => 'getRegion'),
array('category' => Piwik::translate('General_Visit'),
'name' => Piwik::translate('UserCountry_City'),
'module' => 'UserCountry',
'action' => 'getCity'),
));
}
/**
* Returns a list of country codes for a given continent code.
*
* @param string $continent The continent code.
* @return array
*/
public static function getCountriesForContinent($continent)
{
$result = array();
$continent = strtolower($continent);
foreach (Common::getCountriesList() as $countryCode => $continentCode) {
if ($continent == $continentCode) {
$result[] = $countryCode;
}
}
return array('SQL' => "'" . implode("', '", $result) . "', ?",
'bind' => '-'); // HACK: SegmentExpression requires a $bind, even if there's nothing to bind
}
public function configureViewDataTable(ViewDataTable $view)
{
switch ($view->requestConfig->apiMethodToRequestDataTable) {
case 'UserCountry.getCountry':
$this->configureViewForGetCountry($view);
break;
case 'UserCountry.getContinent':
$this->configureViewForGetContinent($view);
break;
case 'UserCountry.getRegion':
$this->configureViewForGetRegion($view);
break;
case 'UserCountry.getCity':
$this->configureViewForGetCity($view);
break;
}
}
private function configureViewForGetCountry(ViewDataTable $view)
{
$view->config->show_goals = true;
$view->config->show_exclude_low_population = false;
$view->config->addTranslation('label', Piwik::translate('UserCountry_Country'));
$view->config->documentation = Piwik::translate('UserCountry_getCountryDocumentation');
$view->requestConfig->filter_limit = 5;
if (LocationProvider::getCurrentProviderId() == DefaultProvider::ID) {
// if we're using the default location provider, add a note explaining how it works
$footerMessage = Piwik::translate("General_Note") . ': '
. Piwik::translate('UserCountry_DefaultLocationProviderExplanation',
array('<a target="_blank" href="http://piwik.org/docs/geo-locate/">', '</a>'));
$view->config->show_footer_message = $footerMessage;
}
}
private function configureViewForGetContinent(ViewDataTable $view)
{
$view->config->show_exclude_low_population = false;
$view->config->show_goals = true;
$view->config->show_search = false;
$view->config->show_offset_information = false;
$view->config->show_pagination_control = false;
$view->config->show_limit_control = false;
$view->config->documentation = Piwik::translate('UserCountry_getContinentDocumentation');
$view->config->addTranslation('label', Piwik::translate('UserCountry_Continent'));
}
private function configureViewForGetRegion(ViewDataTable $view)
{
$view->config->show_exclude_low_population = false;
$view->config->show_goals = true;
$view->config->documentation = Piwik::translate('UserCountry_getRegionDocumentation') . '<br/>' . $this->getGeoIPReportDocSuffix();
$view->config->addTranslation('label', Piwik::translate('UserCountry_Region'));
$view->requestConfig->filter_limit = 5;
$this->checkIfNoDataForGeoIpReport($view);
}
private function configureViewForGetCity(ViewDataTable $view)
{
$view->config->show_exclude_low_population = false;
$view->config->show_goals = true;
$view->config->documentation = Piwik::translate('UserCountry_getCityDocumentation') . '<br/>' . $this->getGeoIPReportDocSuffix();
$view->config->addTranslation('label', Piwik::translate('UserCountry_City'));
$view->requestConfig->filter_limit = 5;
$this->checkIfNoDataForGeoIpReport($view);
}
private function getGeoIPReportDocSuffix()
{
return Piwik::translate('UserCountry_GeoIPDocumentationSuffix',
array('<a target="_blank" href="http://www.maxmind.com/?rId=piwik">',
'</a>',
'<a target="_blank" href="http://www.maxmind.com/en/city_accuracy?rId=piwik">',
'</a>')
);
}
/**
* Checks if a datatable for a view is empty and if so, displays a message in the footer
* telling users to configure GeoIP.
*/
private function checkIfNoDataForGeoIpReport(ViewDataTable $view)
{
$self = $this;
$view->config->filters[] = function ($dataTable) use ($self, $view) {
// if there's only one row whose label is 'Unknown', display a message saying there's no data
if ($dataTable->getRowsCount() == 1
&& $dataTable->getFirstRow()->getColumn('label') == Piwik::translate('General_Unknown')
) {
$footerMessage = Piwik::translate('UserCountry_NoDataForGeoIPReport1');
// if GeoIP is working, don't display this part of the message
if (!$self->isGeoIPWorking()) {
$params = array('module' => 'UserCountry', 'action' => 'adminIndex');
$footerMessage .= ' ' . Piwik::translate('UserCountry_NoDataForGeoIPReport2',
array('<a target="_blank" href="' . Url::getCurrentQueryStringWithParametersModified($params) . '">',
'</a>',
'<a target="_blank" href="http://dev.maxmind.com/geoip/geolite?rId=piwik">',
'</a>'));
} else {
$footerMessage .= ' ' . Piwik::translate('UserCountry_ToGeolocateOldVisits',
array('<a target="_blank" href="http://piwik.org/faq/how-to/#faq_167">', '</a>'));
}
$view->config->show_footer_message = $footerMessage;
}
};
}
/**
* Returns true if a GeoIP provider is installed & working, false if otherwise.
*
* @return bool
*/
public function isGeoIPWorking()
{
$provider = LocationProvider::getCurrentProvider();
return $provider instanceof GeoIp
&& $provider->isAvailable() === true
&& $provider->isWorking() === true;
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = "UserCountry_FatalErrorDuringDownload";
$translationKeys[] = "UserCountry_SetupAutomaticUpdatesOfGeoIP";
$translationKeys[] = "General_Done";
}
public static function isGeoLocationAdminEnabled()
{
return (bool) Config::getInstance()->General['enable_geolocation_admin'];
}
}
|