Using additional Composer packages

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mehmetbkm
User
Posts: 12

Using additional Composer packages

Post by mehmetbkm »

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 ' | ';
    }
}
?>

arbei
User
Posts: 9506

Post by arbei »

Page_Foot is HTML at the end of pages with layout. You should not use:

require 'vendor/autoload.php';

and there is no need.

To add other packages, see Composer Packages.


mehmetbkm
User
Posts: 12

Post by mehmetbkm »

1) i added from tools => composer packaces => "google/apiclient" and "google/apiclient-services" and it created "google" folder into the "vendor" folder
2) i added my functions (below) to server events => golabal => all pages => global code
3) and i call my function from "site footer text" (fetchAndPrintAnalyticsData();)
4) result: Class "PHPMaker2024\BiblioAcademy1\Google\Client" not found

function fetchAndPrintAnalyticsData() {
    // OAuth 2.0 kimlik bilgileri dosyanızın yolu
    $credentialsPath = '../credentials.json';

    // Google Client oluşturma
    $client = new Google\Client();
    $client->setAuthConfig($credentialsPath);
    $client->addScope('https://www.googleapis.com/auth/analytics.readonly');

    // Google Analytics Data API hizmetini başlatma
    $analytics = new Google\Service\AnalyticsData($client);

    // Date range, dimensions ve metrics tanımlama
    $request = new Google\Service\AnalyticsData\RunReportRequest([
        'dateRanges' => [new Google\Service\AnalyticsData\DateRange(['startDate' => '2024-01-01', 'endDate' => 'today'])],
        'metrics' => [new Google\Service\AnalyticsData\Metric(['name' => 'totalUsers'])],
        'dimensions' => [new Google\Service\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;
        }
    }

    // Sonuçları ekrana yazdırma
    echo 'Toplam Ziyaretçi Sayısı: ' . $totalUsers . "\n";

    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 ' | ';
        }
    }
}

arbei
User
Posts: 9506

Post by arbei »

You need to use, e.g. \Google\Client, see Using namespaces: Basics.


Post Reply