PHP code example of malzariey / filament-ga4-analytics

1. Go to this page and download the library: Download malzariey/filament-ga4-analytics 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/ */

    

malzariey / filament-ga4-analytics example snippets


use Malzariey\FilamentGa4Analytics\Ga4AnalyticsPlugin;

$panel->plugin(
    Ga4AnalyticsPlugin::make()
        // Where the values live is up to you — DB settings, config, a vault.
        ->propertyId(fn () => app(AnalyticsSettings::class)->ga4_property_id)
        ->credentials(fn () => app(AnalyticsSettings::class)->service_account_path)
        ->navigationGroup('Reports')
        ->navigationSort(2)
        ->slug('analytics/reports')
        ->permission('View:Ga4AnalyticsPage')     // or ->authorize(fn ($user) => …)
);

use Malzariey\FilamentGa4Analytics\Reports\Breakdown;
use Malzariey\FilamentGa4Analytics\Reports\Kpi;

Ga4AnalyticsPlugin::make()
    ->kpis([Kpi::Users, Kpi::Sessions, Kpi::EngagementRate, Kpi::KeyEvents])
    ->funnel(['page_view', 'generate_lead'])          // empty hides the funnel
    ->events(['generate_lead', 'file_download'])      // null lists GA's top events
    ->eventLabels(['generate_lead' => __('analytics.lead')])
    ->breakdowns([
        Breakdown::pages(),
        Breakdown::landingPages(),
        Breakdown::devices(),
        Breakdown::make('languages')->dimension('language')->label('Languages'),
    ])
    ->linkUsing(fn (string $figure, string $from, string $until, ?string $value) => match ($figure) {
        'event:generate_lead' => LeadResource::getUrl('index', ['from' => $from, 'until' => $until]),
        default => null,
    })
    ->staffPropertyId(fn () => '987654321')           // adds a «staff» tab
    ->staffEventPrefix('staff_')                      // staff tab lists only your own events
    ->realtimeBreakdowns([                            // extra lists in the live card
        Breakdown::make('items_now')
            ->dimension('customUser:last_item_viewed') // realtime or user-scoped custom dimension
            ->hideNotSet()
            ->label('Items on screen now')
            ->emptyLabel('No items on screen now')
            // GA answers 400 until the custom dimension is registered: show
            // something derived from the live page titles meanwhile.
            ->fallbackUsing(fn (array $pages) => $myCodesFromTitles($pages)),
    ])
    ->enabled(fn () => app()->isProduction())         // off: every report says «disabled», calls nobody
    ->realtime(true, pollSeconds: 30)
    ->currency('SAR')
    ->timezone('Asia/Riyadh')
    ->firstDayOfWeek(6);                              // Saturday
bash
php artisan vendor:publish --tag=filament-ga4-analytics-config
php artisan vendor:publish --tag=filament-ga4-analytics-translations
php artisan vendor:publish --tag=filament-ga4-analytics-views