thujohn/analytics
Laravel 4 package providing a simple facade/service to query the Google Analytics Core Reporting API. Configure your GA credentials, get a site ID by URL, and fetch metrics like visits and pageviews over custom date ranges.
Installation
composer require thujohn/analytics
Add the service provider to config/app.php:
'providers' => [
// ...
Thujohn\Analytics\AnalyticsServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Thujohn\Analytics\AnalyticsServiceProvider"
Update config/analytics.php with your Google Analytics Tracking ID (e.g., UA-XXXXXX-X).
First Use Case Track a page view in a controller or blade template:
use Thujohn\Analytics\Facades\Analytics;
// Track a page view
Analytics::trackPageView('/dashboard');
// Or in Blade:
@analytics->trackPageView('/user/profile');
Tracking Events Log custom events (e.g., button clicks, form submissions):
Analytics::trackEvent('User', 'Signed Up', 'Dashboard');
E-Commerce Tracking Track transactions and product views:
Analytics::trackTransaction('12345', 99.99, 'USD');
Analytics::trackProductView('P123', 'Product Name', 19.99);
Middleware Integration Auto-track routes via middleware:
namespace App\Http\Middleware;
use Thujohn\Analytics\Facades\Analytics;
use Closure;
class TrackPageViews
{
public function handle($request, Closure $next)
{
Analytics::trackPageView($request->path());
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
// ...
\App\Http\Middleware\TrackPageViews::class,
];
Blade Directives
Use @analytics directives for quick tracking:
@analytics->trackEvent('User', 'Clicked', 'CTA Button')
Queueing Events
Offload tracking to a queue (requires queue config):
Analytics::trackEvent('User', 'Action', 'Data', true); // 4th param = queue
Tracking ID Validation
config/analytics.php has a valid tracking_id (e.g., UA-XXXXXX-X).if (!Analytics::isConfigured()) {
throw new \RuntimeException('Analytics not configured!');
}
Queue Configuration
analytics.queue is set to true in config and the analytics queue exists:
php artisan queue:work --queue=analytics
HTTPS/Protocol Issues
https://www.google-analytics.com. If your app uses a custom domain (e.g., analytics.yourdomain.com), override the URL in config:
'url' => 'https://analytics.yourdomain.com/collect',
Debugging
'debug' => env('APP_DEBUG', false),
Rate Limiting
Environment-Specific Tracking
Disable tracking in local environments:
if (app()->environment('local')) {
Analytics::disable();
}
Custom Dimensions Extend tracking with custom dimensions (requires GA setup):
Analytics::setCustomDimension(1, 'User Role', auth()->user()->role);
Batch Processing For high-traffic apps, batch events to reduce API calls:
Analytics::flush(); // Force send pending events
Testing
Use the Analytics::shouldTrack() method to conditionally disable tracking:
if (Analytics::shouldTrack()) {
Analytics::trackEvent('Test', 'Event');
}
Fallback for GA Outages Implement a fallback (e.g., log to a database) if GA is unreachable:
Analytics::setFallback(function ($payload) {
\Log::info('GA Fallback:', $payload);
});
How can I help you explore Laravel packages today?