i trying write my first Matomo plugi n(Location provider) that will determine user's location based on custom dimension column.
So far I have came up with this code:
<?php
namespace Piwik\Plugins\LocationProviderCustom\LocationProvider;
use Piwik\Plugins\UserCountry\LocationProvider;
class CountryProvider extends LocationProvider {
public function getLocation($info) {
// custom_dimension_1 should be accessible here
$location = array(
self::COUNTRY_CODE_KEY => 'us'
);
self::completeLocationResult($location);
return $location;
}
public function isWorking() {
return true;
}
public function isAvailable() {
return true;
}
public function getSupportedLocationInfo() {
return array(
self::COUNTRY_CODE_KEY => true
);
}
public function getInfo() {
return array(
'id' => 'locationProviderCustom',
'title' => 'Location Provide',
'description' => '',
'order' => 5
);
}
}
So in getLocation($info) I should determine the country code. $info is holding just IP address and browser language. All of location provider plugings that I saw used one of those two properties to determine user's country.
Is it possible to get visit details and especially custom visit dimension values into the Location provide? Or should I approach it some other way?
Thanks
Use Common::getrequestVar() to get the request values.
Here's an example where I was using custom variable rather than custom dimensions:
public function getLocation($info)
{
$country_code = 'us';
$_cvar = Common::getRequestVar('_cvar', '{}');
//in my case $_cvar string looks something like '{"1":["COUNTRY","gb"]}'
$_cvar = html_entity_decode($_cvar);
$decoded_cvars = json_decode($_cvar, true);
if ($decoded_cvars !== NULL) {
$custom_var = array();
foreach($decoded_cvars as $var) {
$custom_var[$var[0]] = $var[1];
}
if (array_key_exists('COUNTRY', $custom_var)) {
$country_code = $custom_var['COUNTRY'];
}
}
$location = array(
self::COUNTRY_CODE_KEY => $country_code
);
self::completeLocationResult($location);
return $location;
}