- Can I use thujohn/analytics with Laravel 5, 6, 7, 8, or 9+? The package is for Laravel 4 only.
- No, this package is explicitly built for Laravel 4 and won’t work out-of-the-box in newer versions. However, you can port it by replacing Laravel 4 facades (e.g., `Event`, `Input`) with their Laravel 5+ equivalents and updating the service provider binding logic. The core event-driven architecture remains relevant, so the effort is moderate for experienced developers.
- Does thujohn/analytics support Google Analytics 4 (GA4)? I need to migrate from Universal Analytics.
- No, this package only supports the deprecated Google Analytics v3 (Universal Analytics) API. GA4 requires a different payload structure and API endpoint. If you need GA4, you’ll either need to fork the package and refactor it to use the `google/analytics-data` PHP client or consider alternatives like `spatie/analytics` that support GA4 natively.
- How do I track custom events like form submissions or API calls with this package?
- You can dispatch custom events using Laravel’s event system. For example, fire an `OrderPlaced` event in your controller, then listen for it in the package’s event handlers. The package’s `config/analytics.php` allows you to define which events should trigger GA tracking. Ensure your event payload includes the necessary data (e.g., event category, action, label) for GA.
- Is thujohn/analytics GDPR-compliant? How do I handle user opt-outs or data anonymization?
- The package itself doesn’t enforce GDPR compliance, but you can configure it to respect user preferences. Use middleware to check for a `do_not_track` cookie or session flag before sending data to GA. For anonymization, manually modify the payload in your event listeners to exclude sensitive data (e.g., PII) before dispatching events to the package.
- How do I install and configure thujohn/analytics in my Laravel 4 project?
- Run `composer require thujohn/analytics`, publish the config with `php artisan config:publish thujohn/analytics`, and update your `config/analytics.php` with your GA tracking ID and other settings. Register the service provider in `app/config/app.php` under `providers`, then add the middleware to your routes or HTTP kernel to enable global tracking.
- Will thujohn/analytics work with Laravel’s queue system for async tracking?
- No, this package doesn’t natively support Laravel queues. However, you can manually dispatch GA events to a queue by extending the package or wrapping its HTTP calls in a job. For high-traffic sites, consider using Laravel Horizon or a similar queue worker to batch and delay GA API calls, reducing latency and potential throttling.
- Are there alternatives to thujohn/analytics for Laravel that support GA4 or modern analytics?
- Yes, alternatives include `spatie/analytics` (supports GA4 and other providers), direct integration with the `google/analytics-data` PHP client, or frontend-only solutions like Google Tag Manager. If you’re on Laravel 5+, `spatie/analytics` is a more future-proof choice with built-in GA4 support and better Laravel compatibility.
- How do I test thujohn/analytics in my Laravel app? I need to mock GA API calls.
- Use Mockery or Laravel’s HTTP testing helpers to stub the GA API endpoint (e.g., `https://www.google-analytics.com/collect`). In your tests, override the package’s HTTP client (e.g., Guzzle) to return mock responses. For event-based tracking, assert that events are dispatched correctly and verify the payload structure matches GA’s expected format.
- Does thujohn/analytics track page views automatically, or do I need to add code to every route?
- The package provides middleware to track page views automatically when added to your routes or HTTP kernel. No manual code is needed in controllers—just include the middleware in your route definitions or global middleware stack. For dynamic routes, ensure the middleware captures the full URL and other relevant context.
- What’s the performance impact of using thujohn/analytics? Will it slow down my Laravel app?
- The package adds minimal overhead since it uses middleware and events, but GA API calls are synchronous by default. For high-traffic sites, consider batching events or offloading them to a queue to avoid delays. The package doesn’t cache responses, so frequent API calls may hit Google’s rate limits—monitor your quota usage and implement retries or exponential backoff if needed.