zfhassaan / genlytics
Genlytics is a powerful Google Analytics Composer package that allows for easy data collection, analysis, and visualization. Optimized for SEO, it helps improve website performance and drive more organic traffic.
Installs: 113
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 2
Open Issues: 0
pkg:composer/zfhassaan/genlytics
Requires
- php: ^8.1
- google/analytics-data: ^0.9.0
- google/apiclient: ^2.12
- google/auth: ^v1.24.0
- google/cloud-core: ^v1.48.1
- google/common-protos: ^1.3.1||^2.0||^3.0
- google/gax: ^1.18
- google/grpc-gcp: ^0.2.1
- google/longrunning: ^0.2
- google/protobuf: ^3.21
- grpc/grpc: ^1.13
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0
README
Disclaimer
The Google UA Property is going away from July 2023. Google is implementing the Google Analytics V4. Universal Analytics will no longer process new data in standard properties beginning on 1 July 2023. Prepare now by setting up and switching over to a Google Analytics 4 property. Learn More
The Google Analytics data will only be available through the GCP API.
About
Genlytics is a powerful Google Analytics 4 (GA4) package for Laravel, completely rewritten with Repository pattern, Event-driven architecture, and performance optimizations. The package integrates seamlessly with your Laravel application, providing intelligent caching, background job processing, and real-time analytics updates.
Key Features
- Repository Pattern - Abstracted data access layer for easy testing and extension
- Event-Driven - Decoupled event system for extensibility
- Intelligent Caching - Automatic cache management with configurable TTL (90%+ API call reduction)
- Background Jobs - Non-blocking analytics queries for better performance
- Real-Time Updates - Automatic refresh scheduling for live dashboards
- Type Safety - Full type hints and comprehensive PHPDoc
- Error Handling - Robust error handling with logging
- Comprehensive Tests - 52 test cases with 83+ assertions
- Backward Compatible - Existing code works without changes
Architecture Highlights
- Single Responsibility - Each class has one clear purpose
- Dependency Inversion - Dependencies on abstractions, not concretions
- Interface Segregation - Focused, specific interfaces
- Open/Closed - Extensible via interfaces without modification
Requirements
- PHP 8.1 or higher
- Laravel 9.0 or higher
- Google Analytics 4 (GA4) property
- Google Cloud Platform project with Analytics Data API enabled
- Service account with Analytics access
Installation
Install the package via Composer:
composer require zfhassaan/genlytics
Auto-Discovery
The package supports Laravel's auto-discovery, so the service provider and facade will be automatically registered. No manual configuration needed!
Manual Registration (Optional)
If you prefer manual registration, add to config/app.php:
'providers' => [ // ... zfhassaan\genlytics\provider\AnalyticsServiceProvider::class, ], 'aliases' => [ // ... 'Genlytics' => zfhassaan\genlytics\facades\AnalyticsFacade::class, ],
Configuration
Publish Configuration
php artisan vendor:publish --tag=genlytics-config
Or publish everything:
php artisan vendor:publish --provider="zfhassaan\genlytics\provider\AnalyticsServiceProvider"
Environment Variables
Add to your .env file:
# Required GENLYTICS_PROPERTY_ID=your-property-id-here GENLYTICS_CREDENTIALS=storage/app/analytics/service-account.json # Optional - Cache Configuration GENLYTICS_ENABLE_CACHE=true GENLYTICS_CACHE_LIFETIME=1440 # Optional - Background Jobs GENLYTICS_USE_BACKGROUND_JOBS=true GENLYTICS_QUEUE_CONNECTION=redis GENLYTICS_QUEUE_NAME=default # Optional - Real-Time Updates GENLYTICS_ENABLE_REALTIME_UPDATES=true GENLYTICS_REALTIME_CACHE_LIFETIME=30
Google Analytics Setup
- Create GA4 Property - Create a Google Analytics 4 property
- Enable Analytics Data API - Enable in Google Cloud Console
- Create Service Account - Create service account in GCP
- Generate JSON Key - Download service account JSON key
- Grant Analytics Access - Add service account email to GA4 property with Viewer/Analyst role
See Installation Guide for detailed steps.
Usage
Basic Usage
use zfhassaan\genlytics\Genlytics; $analytics = new Genlytics();
Or use the facade:
use Genlytics; $result = Genlytics::runReports(...);
Or dependency injection:
use zfhassaan\genlytics\Genlytics; class AnalyticsController extends Controller { public function __construct( protected Genlytics $analytics ) {} }
Running Reports
Basic Report
$period = [ 'start_date' => '7daysAgo', 'end_date' => 'today' ]; $dimensions = [['name' => 'country']]; $metrics = [['name' => 'activeUsers']]; $result = $analytics->runReports($period, $dimensions, $metrics);
Multiple Dimensions and Metrics
$dimensions = [ ['name' => 'country'], ['name' => 'city'], ['name' => 'deviceCategory'], ]; $metrics = [ ['name' => 'activeUsers'], ['name' => 'sessions'], ['name' => 'bounceRate'], ]; $result = $analytics->runReports($period, $dimensions, $metrics);
With Options
$options = [ 'limit' => 100, 'offset' => 0, 'orderBys' => [ [ 'metric' => [ 'metricName' => 'activeUsers' ], 'desc' => true ] ] ]; $result = $analytics->runReports($period, $dimensions, $metrics, $options);
Force Refresh (Bypass Cache)
$result = $analytics->runReports( $period, $dimensions, $metrics, [], true // Force refresh from API );
Real-Time Reports
$dimensions = [['name' => 'country']]; $metrics = [['name' => 'activeUsers']]; $result = $analytics->runRealTime($dimensions, $metrics);
Dimension Reports
$period = [ 'start_date' => '30daysAgo', 'end_date' => 'today' ]; // Single dimension $result = $analytics->runDimensionReport($period, 'country'); // Multiple dimensions $result = $analytics->runDimensionReport($period, ['country', 'city']);
Response Format
{
"data": [
{
"dimensions": {
"country": "United States",
"city": "New York"
},
"metrics": {
"activeUsers": "1250",
"sessions": "1500"
}
}
],
"metadata": {
"count": 1,
"timestamp": "2024-01-01T00:00:00Z"
}
}
Advanced Features
Caching
Genlytics automatically caches analytics data to reduce API calls:
// Cache is enabled by default // Configure in config/analytics.php or .env GENLYTICS_ENABLE_CACHE=true GENLYTICS_CACHE_LIFETIME=1440 // 24 hours in minutes
Benefits:
- 90%+ reduction in API calls
- Faster response times
- Lower API quota usage
Background Jobs
Process analytics queries in the background:
// Enable in config GENLYTICS_USE_BACKGROUND_JOBS=true // Requires queue worker php artisan queue:work
Benefits:
- Non-blocking requests
- Better user experience
- Automatic cache refresh
Real-Time Updates
Automatic real-time data refresh:
// Enable in config GENLYTICS_ENABLE_REALTIME_UPDATES=true GENLYTICS_REALTIME_CACHE_LIFETIME=30 // seconds
Events
Listen to analytics events:
use zfhassaan\genlytics\Events\AnalyticsDataFetched; use Illuminate\Support\Facades\Event; Event::listen(AnalyticsDataFetched::class, function ($event) { Log::info('Analytics data fetched', [ 'type' => $event->reportType, 'from_cache' => $event->fromCache, 'data_count' => count($event->data), ]); });
Available Events:
AnalyticsDataRequested- Fired when data is requestedAnalyticsDataFetched- Fired when data is successfully fetchedAnalyticsCacheUpdated- Fired when cache is updatedAnalyticsQueryFailed- Fired when query fails
Using Repository Directly
For advanced usage, access the repository:
use zfhassaan\genlytics\Contracts\AnalyticsRepositoryInterface; $repository = app(AnalyticsRepositoryInterface::class); $response = $repository->runReport( $dateRange, $dimensions, $metrics, $options );
Publishing Test Cases
Publish test cases to your application for customization:
php artisan vendor:publish --tag=genlytics-tests
This publishes:
tests/Genlytics/TestCase.php- Base test casetests/Genlytics/Unit/- Unit teststests/Genlytics/Feature/- Feature teststests/Genlytics/Integration/- Integration tests
See PUBLISHING.md for details.
Testing
Run the test suite:
composer test
Or with coverage:
composer test-coverage
Test Coverage:
- 52 test cases
- 83+ assertions
- All major components tested
- Mocks for external dependencies (no real API calls needed)
Cache Management
Clear Cache
php artisan genlytics:refresh-cache --clear
Refresh Specific Cache
php artisan genlytics:refresh-cache --type=report php artisan genlytics:refresh-cache --type=realtime
Programmatic Cache Management
use zfhassaan\genlytics\Contracts\CacheManagerInterface; $cacheManager = app(CacheManagerInterface::class); // Clear specific cache $cacheManager->forget('report:abc123'); // Clear all cache $cacheManager->clear();
Common Use Cases
Dashboard Widget
class DashboardController extends Controller { public function analytics(Genlytics $analytics) { $period = [ 'start_date' => '30daysAgo', 'end_date' => 'today' ]; $metrics = [['name' => 'activeUsers']]; $users = $analytics->runReports($period, [], $metrics); return view('dashboard.analytics', [ 'users' => $users->getData(true) ]); } }
Real-Time Visitor Count
public function liveVisitors(Genlytics $analytics) { $metrics = [['name' => 'activeUsers']]; $result = $analytics->runRealTime([], $metrics); return response()->json([ 'visitors' => $result->getData(true)['data'][0]['metrics']['activeUsers'] ?? 0 ]); }
Top Countries Report
public function topCountries(Genlytics $analytics) { $period = ['start_date' => '30daysAgo', 'end_date' => 'today']; $dimensions = [['name' => 'country']]; $metrics = [['name' => 'activeUsers']]; $options = [ 'orderBys' => [ [ 'metric' => ['metricName' => 'activeUsers'], 'desc' => true ] ], 'limit' => 10 ]; return $analytics->runReports($period, $dimensions, $metrics, $options); }
Error Handling
try { $result = $analytics->runReports($period, $dimensions, $metrics); } catch (\Exception $e) { Log::error('Analytics error', [ 'error' => $e->getMessage(), ]); return response()->json([ 'error' => 'Failed to fetch analytics data' ], 500); }
Performance Optimizations
- Intelligent Caching - Reduces API calls by 90%+
- Background Processing - Non-blocking requests
- Query Optimization - Efficient parameter handling
- Real-Time Scheduling - Automatic refresh management
Documentation
- Installation Guide - Step-by-step setup
- Configuration Guide - All configuration options
- Usage Guide - Usage examples and best practices
- Troubleshooting - Common issues and solutions
- How to Release - Release process
- Contributing - Contribution guidelines
- Architecture - Architecture documentation
- Migration Guide - Migration from v1.x to v2.0
Changelog
See CHANGELOG.md for all changes.
v2.0.0 - Major Rewrite
- Complete rewrite with modern architecture
- Repository pattern implementation
- Event-driven architecture
- Intelligent caching system
- Background job processing
- Real-time updates
- Comprehensive test suite
- Enhanced error handling
- Full backward compatibility
Support
License
The MIT License (MIT). Please see License File for more information.
Author
- Author: Hassaan Ali
- Email: zfhassaan@gmail.com
Made with ❤️ for the Laravel community
