canabelle/cms-analytics-bundle
Installation:
composer require canabelle/cms-analytics-bundle
Add to config/bundles.php:
Canabelle\CMSAnalyticsBundle\CanabelleCMSAnalyticsBundle::class => ['all' => true],
Configuration:
Place your Google Analytics authentication JSON key file in config/packages/cms_analytics.json.
Define your GA profile ID in config/packages/cms_analytics.yml:
cms_analytics:
profile_id: "UA-XXXXXXXX-X"
First Use Case: Inject the service in a controller to fetch basic analytics data:
use Canabelle\CMSAnalyticsBundle\Service\AnalyticsService;
class DashboardController extends Controller
{
public function show(AnalyticsService $analytics)
{
$data = $analytics->getData();
return view('dashboard', compact('data'));
}
}
Extend SonataAdmin to include analytics in the dashboard:
# config/packages/sonata_admin.yml
sonata_admin:
dashboard:
groups:
analytics:
label: Analytics
label_catalogue: sonata_admin
icon: '<i class="fa fa-bar-chart"></i>'
items:
- Canabelle\CMSAnalyticsBundle\Admin\AnalyticsAdmin
Fetching Reports:
Use the AnalyticsService to retrieve metrics like:
$service->getVisitors(); // Total visitors
$service->getPageViews(); // Page views
$service->getTrafficSources(); // Traffic sources breakdown
Custom Reports: Extend the service to fetch custom dimensions/metrics:
$service->getCustomReport('ga:sessions', [
'dimensions' => 'ga:date,ga:pagePath',
'metrics' => 'ga:sessions,ga:pageviews',
]);
Caching: Cache responses for performance (e.g., using Symfony Cache component):
$cache = $this->container->get('cache.app');
$key = 'analytics_data_' . $profileId;
$data = $cache->get($key, function() use ($analytics) {
return $analytics->getData();
});
Event Listeners: Trigger analytics updates via events (e.g., after content is published):
// src/EventListener/AnalyticsListener.php
public function onContentPublish(ContentPublishEvent $event)
{
$analytics = $this->container->get('analytics');
$analytics->trackEvent('content', 'publish', $event->getContent()->getSlug());
}
Authentication:
cms_analytics.json) has the correct permissions (chmod 600).config/packages/ or configured in config/packages/cms_analytics.yml:
cms_analytics:
auth_key_path: "%kernel.project_dir%/config/packages/custom_ga_key.json"
Deprecated API:
Invalid Credentials may appear if the key file is misconfigured or revoked.Profile ID:
profile_id in cms_analytics.yml (e.g., UA-XXXXXXXX-X). Using a view ID (e.g., 12345678) will fail.Rate Limits:
Enable Debugging:
Set debug: true in cms_analytics.yml to log API responses:
cms_analytics:
debug: true
Check logs in var/log/dev.log.
API Errors: Handle exceptions gracefully:
try {
$data = $analytics->getData();
} catch (\Google\ApiCore\ApiException $e) {
$this->addFlash('error', 'Analytics API error: ' . $e->getMessage());
return $this->redirectToRoute('dashboard');
}
Custom Metrics:
Extend the AnalyticsService to add methods for niche use cases:
// src/Service/ExtendedAnalyticsService.php
class ExtendedAnalyticsService extends AnalyticsService
{
public function getConversionRate()
{
$goals = $this->getGoals();
$sessions = $this->getSessions();
return $goals / $sessions * 100;
}
}
Register as a service in config/services.yaml:
services:
App\Service\ExtendedAnalyticsService:
decorates: 'analytics'
arguments: ['@.inner']
Multi-Property Support: Override the service to support multiple GA profiles:
public function getDataForProfile($profileId)
{
$this->profileId = $profileId;
return $this->fetchData();
}
Sonata Dashboard Widget: Create a custom widget for SonataAdmin:
// src/Admin/AnalyticsDashboardWidget.php
class AnalyticsDashboardWidget extends AbstractDashboardWidget
{
public function execute(AnalyticsService $analytics)
{
$this->data = $analytics->getVisitors();
}
public function getTemplate()
{
return '@CanabelleCMSAnalytics/analytics_widget.html.twig';
}
}
Register in sonata_admin.yml:
dashboard:
widgets:
- { service: 'analytics_dashboard_widget', positions: ['right'] }
How can I help you explore Laravel packages today?