Install the Bundle
composer require aldaflux/matomo-bundle
Ensure Aldaflux\MatomoBundle\AldafluxMatomoBundle is registered in config/bundles.php.
Configure the Bundle
Add the required settings to config/packages/aldaflux_matomo.yaml:
aldaflux_matomo:
default:
site: 'https://matomo.mysite.org/' # Your Matomo instance URL
token_auth: 'YOUR_MATOMO_TOKEN' # Auth token from Matomo (User > Login > API)
First Use Case: Track a Page View
Inject the MatomoTracker service in a controller or service:
use Aldaflux\MatomoBundle\Service\MatomoTracker;
public function index(MatomoTracker $tracker)
{
$tracker->trackPageView('/home'); // Track a page view
}
Tracking Events
Use MatomoTracker to log custom events (e.g., button clicks, form submissions):
$tracker->trackEvent('Category', 'Action', 'Name', 1); // Value is optional
Tracking E-Commerce
For e-commerce tracking, use the trackEcommerce method:
$tracker->trackEcommerce(
'purchase',
[
['sku' => '123', 'name' => 'Product A', 'price' => 19.99, 'quantity' => 2],
]
);
Dynamic Configuration Override the default Matomo site/token per environment or route:
# config/packages/aldaflux_matomo.yaml
aldaflux_matomo:
sites:
analytics:
site: 'https://analytics.mysite.org/'
token_auth: 'TOKEN_A'
staging:
site: 'https://staging.matomo.org/'
token_auth: 'TOKEN_B'
Then inject the MatomoTracker with a specific site:
$tracker = $container->get('aldaflux_matomo.tracker.analytics');
Middleware for Automatic Tracking Create a middleware to track page views automatically:
use Aldaflux\MatomoBundle\Service\MatomoTracker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
public function handle(Request $request, Response $response, callable $next): Response
{
$tracker = $this->container->get(MatomoTracker::class);
$tracker->trackPageView($request->getUri());
return $next($request);
}
Token Security
token_auth to version control. Use environment variables or Symfony’s %env%:
token_auth: '%env(MATOMO_TOKEN)%'
View access only).Rate Limiting
sleep(1) after bulk tracking in tests/dev.Asynchronous Tracking
symfony/messenger).Dependency Conflicts
symfony/ux-chartjs (unrelated to Matomo). Ensure your project doesn’t conflict with its dependencies (e.g., Chart.js versions).Enable Debug Logging
Add to config/packages/dev/aldaflux_matomo.yaml:
aldaflux_matomo:
debug: true
Logs will appear in var/log/dev.log.
Validate Token/API Access If tracking fails, verify:
https://matomo.mysite.org/ vs. https://matomo.mysite.org/matomo).HTTP Errors
Check the bundle’s MatomoClient for HTTP errors (e.g., 403 Forbidden). Wrap calls in a try-catch:
try {
$tracker->trackPageView('/page');
} catch (\Exception $e) {
// Log or handle gracefully
}
Custom Tracker Services
Extend the MatomoTracker to add domain-specific methods:
use Aldaflux\MatomoBundle\Service\MatomoTracker;
class CustomMatomoTracker extends MatomoTracker
{
public function trackUserSignup(string $email): void
{
$this->trackEvent('User', 'Signup', $email);
}
}
Register it as a service in config/services.yaml:
services:
App\Service\CustomMatomoTracker:
arguments:
$client: '@aldaflux_matomo.client.default'
Event Listeners
Trigger tracking on Symfony events (e.g., KernelExceptionEvent for errors):
use Aldaflux\MatomoBundle\Service\MatomoTracker;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
public function onKernelException(ExceptionEvent $event, MatomoTracker $tracker)
{
$tracker->trackEvent('Error', 'Exception', $event->getThrowable()->getMessage());
}
Testing
Mock the MatomoTracker in tests to avoid real API calls:
$this->mockBuilder()
->getContainer()
->andReturnSelf()
->get('aldaflux_matomo.tracker.default')
->andReturn($this->createMock(MatomoTracker::class));
How can I help you explore Laravel packages today?