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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\UserCountry\Columns;
use Piwik\Columns\DimensionSegmentFactory;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Intl\Data\Provider\RegionDataProvider;
use Piwik\Metrics\Formatter;
use Matomo\Network\IP;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugin\Segment;
use Piwik\Plugins\Provider\Provider as ProviderProvider;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Segment\SegmentsList;
use Piwik\Tracker\Visit;
use Piwik\Tracker\Visitor;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';
class Country extends Base
{
protected $columnName = 'location_country';
protected $columnType = 'CHAR(3) NULL';
protected $type = self::TYPE_TEXT;
protected $category = 'UserCountry_VisitLocation';
protected $nameSingular = 'UserCountry_Country';
protected $namePlural = 'UserCountryMap_Countries';
protected $segmentName = 'countryCode';
protected $acceptValues = 'ISO 3166-1 alpha-2 country codes (de, us, fr, in, es, etc.)';
public function configureSegments(SegmentsList $segmentsList, DimensionSegmentFactory $dimensionSegmentFactory)
{
$segment = new Segment();
$segment->setName('UserCountry_CountryCode');
$segmentsList->addSegment($dimensionSegmentFactory->createSegment($segment));
$segment = new Segment();
$segment->setSegment('countryName');
$segment->setName('UserCountry_Country');
$segment->setAcceptedValues('Germany, France, Spain, ...');
$regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider');
$countryList = $regionDataProvider->getCountryList();
array_walk($countryList, function(&$item, $key) {
$item = Piwik::translate('Intl_Country_'.strtoupper($key), [], 'en');
});
$segment->setSqlFilterValue(function ($val) use ($countryList) {
$result = array_search($val, $countryList);
if ($result === false) {
$result = 'UNK';
}
return $result;
});
$segment->setSuggestedValuesCallback(function ($idSite, $maxValuesToReturn) use ($countryList) {
return array_values($countryList + ['Unknown']);
});
$segmentsList->addSegment($dimensionSegmentFactory->createSegment($segment));
}
public function formatValue($value, $idSite, Formatter $formatter)
{
return \Piwik\Plugins\UserCountry\countryTranslate($value);
}
/**
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed
*/
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
$value = $this->getUrlOverrideValueIfAllowed('country', $request);
if ($value !== false) {
$value = substr($value, 0, 3);
return $value;
}
$userInfo = $this->getUserInfo($request, $visitor);
$country = $this->getLocationDetail($userInfo, LocationProvider::COUNTRY_CODE_KEY);
if (!empty($country) && $country != Visit::UNKNOWN_CODE) {
return strtolower($country);
}
$country = $this->getCountryUsingProviderExtensionIfValid($userInfo['ip']);
if (!empty($country)) {
return $country;
}
return Visit::UNKNOWN_CODE;
}
private function getCountryUsingProviderExtensionIfValid($ipAddress)
{
if (!Manager::getInstance()->isPluginInstalled('Provider')) {
return false;
}
$hostname = $this->getHost($ipAddress);
$hostnameExtension = ProviderProvider::getCleanHostname($hostname);
$hostnameDomain = substr($hostnameExtension, 1 + strrpos($hostnameExtension, '.'));
if ($hostnameDomain == 'uk') {
$hostnameDomain = 'gb';
}
/** @var RegionDataProvider $regionDataProvider */
$regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider');
if (array_key_exists($hostnameDomain, $regionDataProvider->getCountryList())) {
return $hostnameDomain;
}
return false;
}
/**
* Returns the hostname given the IP address string
*
* @param string $ipStr IP Address
* @return string hostname (or human-readable IP address)
*/
private function getHost($ipStr)
{
$ip = IP::fromStringIP($ipStr);
$host = $ip->getHostname();
$host = ($host === null ? $ipStr : $host);
return trim(strtolower($host));
}
/**
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return int
*/
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
return $this->getUrlOverrideValueIfAllowed('country', $request);
}
/**
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed
*/
public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
{
$country = $visitor->getVisitorColumn($this->columnName);
if (isset($country) && false !== $country) {
return $country;
}
$browserLanguage = $request->getBrowserLanguage();
$enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess'];
$locationIp = $visitor->getVisitorColumn('location_ip');
$country = Common::getCountry($browserLanguage, $enableLanguageToCountryGuess, $locationIp);
return $country;
}
}
|