With the following code, I can view my visitor numbers and visiting countries with the "Google Analytics Data API" without any problem. When I add this code to the "Server Events / Global / All Pages / Page_Foot" section, I get the error "\views\layout.php(345): syntax error, unexpected token use". I also changed the folder path to "require '../vendor/autoload.php'", but still no luck
<?php
require 'vendor/autoload.php';
use Google\Client;
use Google\Service\AnalyticsData;
// OAuth 2.0 kimlik bilgileri dosyanızın yolu
$credentialsPath = 'credentials.json';
// Google Client oluşturma
$client = new Client();
$client->setAuthConfig($credentialsPath);
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
// Google Analytics Data API hizmetini başlatma
$analytics = new AnalyticsData($client);
// Date range, dimensions ve metrics tanımlama
$request = new AnalyticsData\RunReportRequest([
'dateRanges' => [new AnalyticsData\DateRange(['startDate' => '2024-01-01', 'endDate' => 'today'])],
'metrics' => [new AnalyticsData\Metric(['name' => 'totalUsers'])],
'dimensions' => [new AnalyticsData\Dimension(['name' => 'country'])]
]);
// Analytics veri sorgusu
$response = $analytics->properties->runReport('properties/431261028', $request);
// Toplam ziyaretçi sayısını çekme ve ülkelere göre gruplama
$totalUsers = 0;
$countryData = [];
foreach ($response->getRows() as $row) {
$country = $row->getDimensionValues()[0]->getValue();
$users = $row->getMetricValues()[0]->getValue();
$totalUsers += $users;
if (isset($countryData[$country])) {
$countryData[$country] += $users;
} else {
$countryData[$country] = $users;
}
}
// Toplam ziyaretçi sayısını gösterme
echo 'Toplam Ziyaretçi Sayısı: ' . $totalUsers . "\n";
// Ülkelere göre ziyaretçi sayılarını gösterme
echo "<br>Ülkelere Göre Ziyaretçi Sayıları:\n";
$countryCount = count($countryData);
$currentCount = 0;
foreach ($countryData as $country => $users) {
echo $country . ': ' . $users;
$currentCount++;
if ($currentCount < $countryCount) {
echo ' | ';
}
}
?>