Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

# 5.1.2 - 2025-03-17
- Started importing region data as the region dimension is now available in the API request

# 5.1.1 - 2025-02-03
- Handled an edge case to delete the import if website is deleted

Expand Down
37 changes: 22 additions & 15 deletions Importers/UserCountry/RecordImporterGA4.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Date;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use Piwik\Plugins\UserCountry\Archiver;

class RecordImporterGA4 extends \Piwik\Plugins\GoogleAnalyticsImporter\RecordImporterGA4
Expand Down Expand Up @@ -40,23 +41,31 @@ private function queryDimension(Date $day, $dimension, $recordName)
private function queryRegionsAndCities(Date $day)
{
$cities = new DataTable();
// $regions = new DataTable(); // Not Available in GA4
$regions = new DataTable();
$gaQuery = $this->getGaClient();
$table = $gaQuery->query($day, ['countryId', 'city'], $this->getConversionAwareVisitMetrics());
$table = $gaQuery->query($day, ['countryId', 'region', 'city'], $this->getConversionAwareVisitMetrics());
$regionsList = \Piwik\Plugin\Manager::getInstance()->isPluginActivated('GeoIp2') ? GeoIp2::getRegionNames() : [];
foreach ($table->getRows() as $row) {
$country = strtolower($row->getMetadata('countryId'));
// $region = $row->getMetadata('ga:regionIsoCode'); // Not Available in GA4
$region = $row->getMetadata('region');
if ($country && $region && $regionsList) {
$listToSearch = !empty($regionsList[strtoupper($country)]) ? $regionsList[strtoupper($country)] : [];
if (!empty($listToSearch)) {
foreach ($listToSearch as $listKey => $list) {
// Need to use iconv as we store region as Île-de-France and GA4 returns as Ile-de-France
if (mb_strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $region)) === mb_strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $list))) {
$region = $listKey;
break;
}
}
}
}
$city = $row->getMetadata('city');
// $lat = $row->getMetadata('ga:latitude'); // Not Available in GA4
// $long = $row->getMetadata('ga:longitude'); // Not Available in GA4
/** Not available in GA4
// GA returns region as COUNTRY-REGION, we only want the last part here
$regionParts = explode('-', $region);
$region = end($regionParts);

$locationRegion = $region . Archiver::LOCATION_SEPARATOR . $country;
$locationCity = $city . Archiver::LOCATION_SEPARATOR . $locationRegion;
*/
$locationRegion = $region . Archiver::LOCATION_SEPARATOR . $country;
$locationCity = $city . Archiver::LOCATION_SEPARATOR . $locationRegion;
if (!empty($city)) {
$topLevelRowCity = $this->addRowToTable($cities, $row, $city);
}
Expand All @@ -72,14 +81,12 @@ private function queryRegionsAndCities(Date $day)
$topLevelRowCity->setMetadata('long', $long);
}

$this->addRowToTable($regions, $row, $locationRegion);
*/
$this->addRowToTable($regions, $row, $locationRegion);
}
$this->insertRecord(Archiver::CITY_RECORD_NAME, $cities);
Common::destroy($cities);
/** Not available in GA4
$this->insertRecord(Archiver::REGION_RECORD_NAME, $regions);
Common::destroy($regions);
*/
$this->insertRecord(Archiver::REGION_RECORD_NAME, $regions);
Common::destroy($regions);
}
}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "GoogleAnalyticsImporter",
"description": "Import reports from a Google Analytics account into Matomo.",
"version": "5.1.1",
"version": "5.1.2",
"theme": false,
"require": {
"matomo": ">=5.0.0-rc5,<6.0.0-b1"
Expand Down
3 changes: 3 additions & 0 deletions tests/System/CheckDirectDependencyUseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function testCommand()
],
'Twig' => [
'GoogleAnalyticsImporter/Controller.php'
],
'GeoIp2' => [
'GoogleAnalyticsImporter/Importers/UserCountry/RecordImporterGA4.php'
]
], $checkDirectDependencyUse->usesFoundList[$pluginName]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/capturedresponses-ga4.log
Git LFS file not shown