Register for Anonlytics
Create a free account at anonlytics.eu to obtain your client_token and site_token.
Install the Bundle
composer require defixit/anonlytics-bundle
Configure the Bundle
Add the bundle to config/bundles.php:
DeFixIT\AnonlyticsBundle\AnonlyticsBundle::class => ['all' => true],
Set Environment Variables
Add to .env:
ANONLYTICS_CLIENT_TOKEN=your_client_token_here
ANONLYTICS_SITE_TOKEN=your_site_token_here
Create Configuration File
Create config/packages/anonlytics.yaml:
anonlytics:
client_token: '%env(resolve:ANONLYTICS_CLIENT_TOKEN)%'
site_token: '%env(resolve:ANONLYTICS_SITE_TOKEN)%'
First Event Tracking
Inject the AnonlyticsClient service and track a page view:
use DeFixIT\AnonlyticsBundle\Client\AnonlyticsClient;
public function someAction(AnonlyticsClient $anonlytics)
{
$anonlytics->trackPageView('/home');
}
Track page views with optional metadata:
$anonlytics->trackPageView('/dashboard', [
'user_id' => $user->id,
'referrer' => $request->headers->get('referer'),
]);
Log custom events (e.g., button clicks, form submissions):
$anonlytics->trackEvent('checkout_started', [
'product_id' => $product->id,
'value' => $product->price,
]);
Anonymously associate events with a user session (no PII stored):
$anonlytics->identifyUser($user->id); // Uses a hashed/salted identifier
Listen to kernel events (e.g., kernel.request) to auto-track routes:
// src/EventListener/AnonlyticsListener.php
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$anonlytics->trackPageView($request->getPathInfo());
}
For performance, batch events before sending:
$anonlytics->addEvent(['type' => 'page_view', 'path' => '/contact']);
$anonlytics->addEvent(['type' => 'event', 'name' => 'form_submitted']);
// Later, flush all events:
$anonlytics->flush();
Override tokens per environment (e.g., staging/production):
# config/packages/dev/anonlytics.yaml
anonlytics:
client_token: '%env(resolve:ANONLYTICS_CLIENT_TOKEN_DEV)%'
site_token: '%env(resolve:ANONLYTICS_SITE_TOKEN_DEV)%'
Create middleware to track all requests:
// src/Middleware/AnonlyticsMiddleware.php
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$this->anonlytics->trackPageView($request->getPathInfo());
return $response;
}
Extend tracking with custom dimensions (e.g., user segments):
$anonlytics->setUserProperties([
'segment' => 'premium',
'plan' => 'annual',
]);
Token Validation
client_token/site_token are invalid.try {
$anonlytics->trackEvent('test');
} catch (\RuntimeException $e) {
$this->logger->error('Anonlytics failed: '.$e->getMessage());
}
Rate Limiting
addEvent() + flush()) or throttle calls.GDPR Compliance
Configuration Overrides
.env changes.php bin/console cache:clear) or use cache:pool:clear anonlytics.Enable Debug Logging
Add to config/packages/dev/anonlytics.yaml:
anonlytics:
debug: true
Check logs for failed requests.
Test Locally with Mock Tokens
Use a test account’s tokens in .env to verify events are sent:
ANONLYTICS_CLIENT_TOKEN=test_client_token
ANONLYTics_SITE_TOKEN=test_site_token
Validate Events Use the Anonlytics Dashboard to confirm events appear in real-time.
Custom Event Types Extend the client to support custom event schemas:
// src/Service/ExtendedAnonlyticsClient.php
public function trackCustomEvent(string $type, array $data): void
{
$this->client->addEvent([
'type' => 'custom_'.$type,
'data' => $data,
]);
}
Event Transformers Transform data before sending (e.g., sanitize or normalize):
$anonlytics->setEventTransformer(function (array $event) {
return array_map('strtolower', $event);
});
Async Processing Use Symfony Messenger to queue events for background processing:
$message = new AnonlyticsEvent($eventData);
$this->messageBus->dispatch($message);
%kernel.environment% in anonlytics.yaml to switch tokens:
anonlytics:
client_token: '%env(resolve:ANONLYTICS_TOKEN_%kernel.environment%)%'
How can I help you explore Laravel packages today?