PHP code example of rs / matomo-google-charts

1. Go to this page and download the library: Download rs/matomo-google-charts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rs / matomo-google-charts example snippets


use RedSnapper\MatomoCharts\Analytics\Models\AnalyticsChart;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartComponent;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartStyle;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartType;

class BrowserChart extends AnalyticsChart
{
    public string $title = 'Browsers';
    public array $columns = ['Browser', 'Visits'];
    public array $properties = ['label', 'nb_visits'];
    public AnalyticsChartComponent $component = AnalyticsChartComponent::GOOGLE_CHART;
    public AnalyticsChartType $type = AnalyticsChartType::PIE;
    public AnalyticsChartStyle $style = AnalyticsChartStyle::STANDARD;

    public function query(): void
    {
        $this->matomoAPI
            ->setSiteId($this->matomoSiteId)
            ->setDate($this->startDate, $this->endDate);

        $this->data = $this->matomoAPI->fetchBrowserData()->get();
    }

    public function getTextView(): ?View
    {
        return null;
    }
}

use RedSnapper\MatomoCharts\Analytics\Models\AnalyticsMiniChart;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartComponent;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartStyle;
use RedSnapper\MatomoCharts\Analytics\Enums\AnalyticsChartType;

class VisitorsMiniChart extends AnalyticsMiniChart
{
    public string $title = 'Total Visitors';
    public array $columns = ['Week', 'Visitors'];
    public array $properties = ['label', 'nb_uniq_visitors'];
    public AnalyticsChartComponent $component = AnalyticsChartComponent::GOOGLE_CHART;
    public AnalyticsChartType $type = AnalyticsChartType::LINE;
    public AnalyticsChartStyle $style = AnalyticsChartStyle::MINI;
    public bool $hasValueComparison = true;

    public function query(): void
    {
        $this->matomoAPI
            ->setSiteId($this->matomoSiteId)
            ->setDate($this->startDate, $this->endDate);

        $this->data = $this->matomoAPI->fetchWeeklyData()->get();

        // Fetch previous period data for comparison
        $this->matomoAPI->setDate($this->preStartDate, $this->preEndDate);
        $this->preData = $this->matomoAPI->fetchWeeklyData()->get();
    }

    public function getTextView(): ?View
    {
        return null;
    }

    public function getStatIcon(): ?string
    {
        return self::ICON_VISITORS;
    }

    public function getComparisonData(): array
    {
        return [
            'value' => $this->data->sum('nb_uniq_visitors'),
            'preValue' => $this->preData->sum('nb_uniq_visitors'),
            'label' => 'visitors',
        ];
    }
}

use RedSnapper\MatomoCharts\Facades\MatomoChart;

// Get a chart model instance
$chart = MatomoChart::makeChart(BrowserChart::class, $matomoSiteId);

// Get a JSON resource (for API responses)
$resource = MatomoChart::makeChartResource(BrowserChart::class, $matomoSiteId);

// Mini charts
$miniChart = MatomoChart::makeMiniChart(VisitorsMiniChart::class, $matomoSiteId);
$miniResource = MatomoChart::makeMiniChartResource(VisitorsMiniChart::class, $matomoSiteId);
bash
php artisan vendor:publish --provider="RedSnapper\MatomoCharts\MatomoChartServiceProvider" --tag=config