- How do I install and use this package in Laravel?
- Run `composer require matomo/device-detector`, then instantiate the detector with `new DeviceDetector($userAgent, $clientHints)`. For Laravel, inject `Request` to access `HTTP_USER_AGENT` and optionally `Accept-CH` headers. Wrap logic in a service or middleware for reuse.
- Does this work with Laravel’s caching system?
- Yes. The package supports PSR-6/PSR-16 caches (e.g., Redis, APCu) via Laravel’s cache drivers. Configure it with `new CacheAdapter($cacheDriver)` for persistent storage. Defaults to in-memory caching for single requests.
- What Laravel versions does this package support?
- The package works with **PHP 8.1+** and **Laravel 9+** (tested with Symfony components). Check `composer.json` for exact constraints. No known conflicts with Laravel’s ecosystem, but test thoroughly in your stack.
- Can I use this for bot detection in Laravel?
- Absolutely. The library identifies bots/spiders (e.g., Googlebot, Scrapy) via User-Agent parsing. Combine with IP-based bot lists (e.g., Spamhaus) for higher accuracy. Use middleware to block or log suspicious agents globally.
- How accurate is device detection compared to alternatives?
- This library achieves **~95%+ accuracy** for modern devices (tested against UAParser benchmarks). For edge cases, audit false positives/negatives with real user agents. Fallback to regex if needed, but caching improves performance.
- Should I cache results in production?
- Yes. Use **Redis or APCu** for high-traffic apps (e.g., 10K+ RPS) with a short TTL (e.g., 1 hour). Cache warming or invalidation may be needed for dynamic user agents. Avoid filesystem caching in shared environments.
- How do I integrate this with Laravel middleware?
- Create a middleware (e.g., `DeviceDetectorMiddleware`) that parses `HTTP_USER_AGENT` on every request. Store results in the request object or session. Example: `public function handle(Request $request, Closure $next) { $detector = new DeviceDetector($request->userAgent()); $request->merge(['device' => $detector->getDevice()]); return $next($request); }`
- Are there performance concerns for high-traffic Laravel apps?
- The package is lightweight (~10ms per request without caching). For high throughput, enable **Redis caching** and skip bot detection on non-critical paths. Benchmark with your traffic load to optimize TTL or cache strategies.
- Can I customize device/OS/browser rules?
- Yes. The library uses YAML-based rules (extendable via `DeviceDetector::setRules()`). Fork the [rules repository](https://github.com/piwik/device-detector/tree/master/rules) to add custom devices or override defaults. Test changes with real user agents.
- Does this comply with GDPR/CCPA for storing device data?
- The package itself doesn’t store data—it parses User-Agent strings. If you log device info, ensure compliance via **consent management systems** (e.g., Laravel’s `laravel-analytics`). Anonymize or encrypt sensitive data before storage.