bezhansalleh/filament-google-analytics
Installation
composer require bezhansalleh/filament-google-analytics
Publish the config file:
php artisan vendor:publish --provider="BezhanSalleh\FilamentGoogleAnalytics\FilamentGoogleAnalyticsServiceProvider" --tag="filament-google-analytics-config"
Configuration
Edit .env or config/filament-google-analytics.php:
FILAMENT_GOOGLE_ANALYTICS_VIEW_ID=GA4_MEASUREMENT_ID
FILAMENT_GOOGLE_ANALYTICS_TRACKING_ID=UA-XXXXXX-Y
For GA4, use the Measurement ID (e.g., G-XXXXXXXXXX). For Universal Analytics, use the Tracking ID (e.g., UA-XXXXXX-Y).
Enable in Filament Panel
Add to your PanelProvider (e.g., app/Providers/Filament/AdminPanelProvider.php):
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->googleAnalytics();
}
First Use Case Immediately start tracking admin panel usage. No additional code needed—just refresh your Filament dashboard.
Tracking Page Views Automatically logs:
/admin/resources/posts)Custom Events Manually trigger events for critical actions:
use BezhanSalleh\FilamentGoogleAnalytics\Facades\FilamentGoogleAnalytics;
FilamentGoogleAnalytics::trackEvent(
title: 'Post Published',
category: 'Content',
action: 'Publish',
label: 'Post ID: 123',
value: 1
);
Best Practice: Use in:
store(), update())Integration with Filament Features
Resources: Track CRUD actions via resource hooks:
public static function getPages(): array
{
return [
'index' => Pages\ListPosts::route('/'),
'create' => Pages\CreatePost::route('/create'),
'edit' => Pages\EditPost::route('/{record}/edit'),
];
}
The package auto-detects these routes for tracking.
Widgets: Log dashboard widget usage:
FilamentGoogleAnalytics::trackScreen('Dashboard', 'StatsWidget');
Multi-Panel Support Configure separate tracking IDs for different Filament panels:
->panel()
->id('admin')
->googleAnalytics(viewId: 'GA4_ADMIN_ID')
->panel()
->id('tenant')
->googleAnalytics(viewId: 'GA4_TENANT_ID')
Dynamic Tracking IDs Use environment variables or database-driven config:
config([
'filament-google-analytics.view_id' => env('FILAMENT_GA_VIEW_ID', fn() => DB::table('settings')->value('ga_view_id')),
]);
Excluding Pages Opt out specific routes (e.g., login pages):
FilamentGoogleAnalytics::ignoreRoutes(['/admin/auth/login']);
Custom Dimensions Pass user metadata (e.g., roles, subscriptions):
FilamentGoogleAnalytics::setUserProperties([
'user_id' => auth()->id(),
'role' => auth()->user()->role,
]);
Server-Side Event Validation Validate events before sending (e.g., in a middleware):
public function handle(Request $request, Closure $next)
{
if (!FilamentGoogleAnalytics::isValidEvent($request->input())) {
abort(403);
}
return $next($request);
}
GA4 vs. Universal Analytics
viewId (e.g., G-XXXXXXXXXX).trackingId (e.g., UA-XXXXXX-Y).CORS/HTTPS Issues
proxy_set_header X-Forwarded-Proto $scheme;
Rate Limiting
FilamentGoogleAnalytics::flush() manually in high-traffic areas.Debugging
'debug' => env('FILAMENT_GA_DEBUG', false),
tail -f storage/logs/laravel.log | grep "GoogleAnalytics"
Filament Widget Conflicts
filament/spatie-laravel-analytics, disable its auto-tracking to avoid duplicates:
Spatie\Analytics\Analytics::ignoreRoutes(['/admin/*']);
Segmentation Use custom dimensions to segment data by:
user_role)tenant_id)feature_x_enabled)Performance Optimization
FilamentGoogleAnalytics::disable();
if (app()->environment('production')) {
FilamentGoogleAnalytics::enable();
}
Testing
FilamentGoogleAnalytics::shouldReceive('trackScreen')->once();
Extension Points
BezhanSalleh\FilamentGoogleAnalytics\Contracts\Tracker for alternative providers (e.g., Matomo).FilamentGoogleAnalyticsServiceProvider to modify events:
public function boot()
{
FilamentGoogleAnalytics::macro('logSensitiveData', function ($event) {
if (Str::contains($event['label'], 'password')) {
return false;
}
return true;
});
}
Data Retention
FilamentGoogleAnalytics::setLogger(new DatabaseLogger());
How can I help you explore Laravel packages today?