defixit/anonlytics-lib-php
PHP library for anonymous analytics (Anonlytics). Collect lightweight, privacy-first event data without cookies or personal identifiers, and send it to an Anonlytics server/API from your PHP apps and services.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require defixit/anonlytics-lib-php:^2.0
Add the service provider to config/app.php:
'providers' => [
Defixit\Anonlytics\AnonlyticsServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Defixit\Anonlytics\AnonlyticsServiceProvider"
Update config/anonlytics.php with your API key, domain, and ensure PHP 8.3 compatibility.
First Use Case Track a page view in a Laravel controller:
use Defixit\Anonlytics\Facades\Anonlytics;
public function show()
{
Anonlytics::pageView('/dashboard');
return view('dashboard');
}
Event Tracking
// Track custom events with properties (PHP 8.3 type safety)
Anonlytics::track('user_signed_up', [
'email' => auth()->user()->email,
'plan' => auth()->user()->plan,
]);
User Identification
// Identify a user (e.g., after login)
Anonlytics::identify(auth()->user()->id, [
'name' => auth()->user()->name,
'email' => auth()->user()->email,
]);
E-Commerce Tracking
// Track purchases with improved error handling
Anonlytics::track('purchase', [
'revenue' => $order->total,
'items' => array_map(fn($item) => [
'id' => $item->id,
'price' => $item->price,
'quantity' => $item->quantity,
], $order->items),
]);
Middleware for Automatic Tracking Create middleware to track page views automatically:
// app/Http/Middleware/TrackPages.php
public function handle($request, Closure $next)
{
Anonlytics::pageView($request->path());
return $next($request);
}
Register in app/Http/Kernel.php:
'web' => [
\App\Http\Middleware\TrackPages::class,
// ...
],
Queue Events for Async Tracking Dispatch events to a queue to avoid blocking requests:
event(new \Defixit\Anonlytics\Events\TrackEvent('event_name', $properties));
Configure the queue in config/anonlytics.php:
'queue' => env('ANONLYTICS_QUEUE', 'default'),
'timeout' => 5, // New: Timeout in seconds for API calls
Laravel Scout Integration Sync Scout model updates to Anonlytics:
// In your Scout model observer
public function saved($model)
{
Anonlytics::track('model_updated', [
'model' => get_class($model),
'id' => $model->id,
]);
}
PHP 8.3 Compatibility
composer.json and server configuration accordingly.API Key Validation
ANONLYTICS_API_KEY is set in .env; the library now throws a more descriptive exception if missing.Anonlytics::getConfig(); // Verify keys are loaded
Timeouts and Rate Limiting
timeout in config/anonlytics.php to prevent hanging requests (default: 5s).try {
Anonlytics::track('event');
} catch (\Defixit\Anonlytics\Exceptions\RateLimitException $e) {
sleep($e->retryAfter);
retry();
}
Sensitive Data
Enable Logging
Set debug: true in config/anonlytics.php to log payloads:
'debug' => env('ANONLYTICS_DEBUG', false),
Mocking for Tests Use a mock client in tests:
$this->app->singleton(\Defixit\Anonlytics\Anonlytics::class, function () {
return new \Defixit\Anonlytics\Anonlytics(new \Defixit\Anonlytics\MockClient());
});
New: HTTPS for GeoIP All external API calls (e.g., GeoIP) now use HTTPS by default for security.
Custom Payload Transformers Override the default payload structure:
// app/Providers/AnonlyticsServiceProvider.php
public function boot()
{
Anonlytics::extend(function ($payload) {
$payload['custom_field'] = 'value';
return $payload;
});
}
Batch Processing For high-volume tracking, implement batching:
Anonlytics::flush(); // Force-send queued events
Webhook Fallback If Anonlytics is down, log events to a fallback (e.g., database):
Anonlytics::setFallback(function ($event) {
\App\Models\AnonlyticsEvent::create($event);
});
New: Timeout Configuration
Customize API call timeouts in config/anonlytics.php:
'timeout' => 10, // Increase timeout for slower connections
Error Handling Leverage improved exceptions for better debugging:
try {
Anonlytics::track('event');
} catch (\Defixit\Anonlytics\Exceptions\ApiException $e) {
report($e); // Use Laravel's error reporting
}
NO_UPDATE_NEEDED was not applicable due to breaking changes and new features in 2.0.0.
How can I help you explore Laravel packages today?