- How do I install and set up baks-dev/products-viewed in a Laravel project?
- Run `composer require baks-dev/products-viewed` to install. Then execute `php bin/console baks:assets:install` for asset setup and `php bin/console doctrine:migrations:diff` followed by `migrate` to apply database schema changes. Ensure your routes include the `TrackProductViewed` middleware.
- Does this package work with Laravel’s Eloquent ORM, or is Doctrine required?
- This package requires Doctrine for migrations and entities. If your project uses Eloquent, you’ll need to refactor or fork the package to create a compatible repository adapter or modify the storage logic to work with Eloquent models.
- Can I use this package with Blade templates instead of Twig?
- The package assumes Twig for rendering via `render_products_viewed()`. For Blade, you’ll need to create a custom helper or directive (e.g., `@viewTracker($productId)`) to replicate the functionality, which may take 1–2 hours of development time.
- What Laravel and PHP versions are supported by this package?
- The package requires **PHP 8.4+** and is designed for **Laravel 10+**. If your project uses older versions (e.g., PHP 8.2 or Laravel 9), you may encounter compatibility issues or need to upgrade dependencies, which could introduce breaking changes.
- How do I configure the package to track product variants or SKUs?
- The current schema may not natively support variants/SKUs. You’ll need to extend the `ProductView` entity or migration to include additional fields (e.g., `variant_id` or `sku`). Check the Doctrine entity structure and modify it to fit your product model.
- Is there built-in support for purging old product view records?
- No, the package lacks a built-in TTL (time-to-live) mechanism. You can implement retention policies using Laravel’s scheduler or cron jobs to run a custom query (e.g., `DELETE FROM product_views WHERE created_at < NOW() - INTERVAL '90 DAY'`).
- Can I integrate this with Laravel’s event system for analytics or recommendations?
- While the package isn’t event-driven by default, you can manually dispatch events (e.g., `ProductViewed`) after tracking a view. This allows downstream services like recommendation engines or analytics pipelines to react to views without tight coupling to the package’s logic.
- What performance considerations should I keep in mind for high-traffic sites?
- For high-traffic scenarios (e.g., 10K+ views/minute), consider caching rendered product views in Redis or Memcached to reduce database load. Offload analytics processing to Laravel queues if real-time updates aren’t critical, and ensure proper indexing on `user_id`, `product_id`, and `created_at`.
- Are there alternatives to this package if I need GDPR compliance or opt-out features?
- This package doesn’t include GDPR-specific features like opt-out or data anonymization. To comply, extend the package to mask `user_id` or `IP` for anonymous users and add a `consent` flag to view records. Alternatively, consider packages like `spatie/activitylog` with custom filters or build a custom solution.
- How can I customize or extend the package for non-product entities (e.g., articles, listings)?
- The package is product-focused, but you can fork it or create a base `Viewable` trait to generalize the tracking logic for other entities. Override the `ProductView` entity and migration to support additional models, then update the Twig/Blade rendering logic to accept generic IDs.