Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Visitor Laravel Package

shetabit/visitor

Track and log Laravel visitors: browser, platform, device, IP, languages, user agent, and request data. Use visitor() helper or $request->visitor(). Record visits for any model via Visitable trait, count views/unique visitors, and detect online users.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Visitor Tracking as a Service Layer: The package abstracts visitor data collection (IP, browser, device) and visit logging into a dedicated service layer, aligning with Laravel’s service container pattern. This fits well in architectures where analytics are a cross-cutting concern (e.g., e-commerce, SaaS, or content platforms).
  • Trait-Based Extensibility: The Visitable and Visitor traits enable model-agnostic visit tracking, reducing boilerplate for domain-specific logic (e.g., tracking visits to Post, Product, or User models). This is ideal for domain-driven design (DDD) where entities need lightweight observability.
  • Middleware for Automatic Logging: The LogVisits middleware provides a declarative approach to visit tracking, leveraging Laravel’s middleware pipeline. This is a clean fit for HTTP-driven applications where visits are tied to routes/models (e.g., Route::model('post', Post::class)).
  • Online User Detection: The package’s onlineVisitors() and isOnline() methods offer a lightweight alternative to WebSockets for real-time presence, suitable for collaborative tools or admin dashboards where live user counts are needed.

Integration Feasibility

  • Laravel Native: Designed for Laravel’s ecosystem (facades, service providers, traits), with zero configuration for Laravel 5.5+. Integration risk is low for teams already using Laravel.
  • Database Agnostic: Relies on Laravel’s Eloquent ORM, so it works with MySQL, PostgreSQL, SQLite, etc., without vendor lock-in.
  • User-Agent Parsing: Uses UAParser (included via Composer), eliminating the need for external APIs like Google’s User-Agent library. This reduces latency and dependency complexity.
  • Migration-First: Requires publishing migrations for the visits table, which is a standard Laravel pattern (e.g., php artisan vendor:publish --provider="Shetabit\Visitor\Provider\VisitorServiceProvider"). Teams familiar with Laravel migrations will find this straightforward.

Technical Risk

  • Middleware Scope: The LogVisits middleware automatically logs visits for all routes with model binding. This could lead to unintended logging (e.g., API endpoints, admin routes) if not configured carefully. Mitigation: Exclude sensitive routes via middleware configuration (e.g., except in LogVisits).
  • Performance Overhead: Logging visits for every request adds database writes. For high-traffic apps (e.g., 10K+ RPS), this could impact performance. Mitigation:
    • Use queue-based logging (e.g., visit() writes to a queue instead of DB directly).
    • Implement rate limiting (e.g., log only every 5th visit for anonymous users).
    • Offload analytics to a separate service (e.g., Redis for real-time, then batch to DB).
  • IP-Based Deduplication: The package uses IP addresses to count unique visitors, which may not align with privacy regulations (e.g., GDPR’s "right to be forgotten") or use cases requiring user-level granularity. Mitigation:
    • Supplement with session-based tracking for authenticated users.
    • Add a purgeLogs() method to comply with data retention policies.
  • User-Agent Parsing Accuracy: Relies on UAParser, which may misclassify devices/browsers for edge cases (e.g., custom user agents). Mitigation: Test with real-world traffic or integrate a fallback service (e.g., WURFL).
  • Laravel Version Support: Actively maintained for Laravel 11–13, but Laravel 10 support is deprecated. Risk for teams on older versions. Mitigation: Fork or pin to a stable version.

Key Questions for Stakeholders

  1. Analytics Goals:
    • Are you tracking visits for user behavior analysis (e.g., funnel optimization) or operational visibility (e.g., live user counts)?
    • Do you need real-time analytics (e.g., "X users online now") or batch processing (e.g., weekly reports)?
  2. Data Privacy:
    • Are you compliant with GDPR/CCPA? How will you handle IP-based deduplication and log retention?
    • Do you need to exclude sensitive routes (e.g., /admin, /api) from logging?
  3. Performance:
    • What is your expected request volume? Will database writes for visits scale?
    • Can you tolerate millisecond latency for visit logging, or do you need asynchronous processing?
  4. Extensibility:
    • Do you need to customize visit logs (e.g., add metadata like referrer, campaign_id)?
    • Will you integrate with third-party tools (e.g., Google Analytics, Mixpanel) for advanced segmentation?
  5. Alternatives:
    • Have you considered Laravel Scout (for search-driven analytics) or Spatie’s Analytics (for event tracking)?
    • Is real-time presence a hard requirement, or would polling (e.g., "last seen 5 mins ago") suffice?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Perfect fit for Laravel applications, especially those using Eloquent models, middleware, and facades. The package leverages Laravel’s:
    • Service Container: For dependency injection (e.g., Visitor facade).
    • Eloquent ORM: For visit log storage and querying.
    • Middleware Pipeline: For automatic visit logging.
    • Traits: For reusable visit tracking logic across models.
  • PHP 8.1+: Supports modern PHP features like nullable return types and nullsafe operator, reducing compatibility issues.
  • Composer Dependency: Zero external API calls (unlike Google Analytics), improving offline functionality and data sovereignty.

Migration Path

  1. Assessment Phase:
    • Audit existing visitor tracking (e.g., custom logs, third-party scripts).
    • Identify models needing visit tracking (e.g., Post, Product, User).
    • Define exclusion routes (e.g., /admin, /api/webhooks).
  2. Installation:
    composer require shetabit/visitor
    php artisan vendor:publish --provider="Shetabit\Visitor\Provider\VisitorServiceProvider"
    php artisan migrate
    
  3. Configuration:
    • Register the service provider and facade (auto-registered in Laravel 5.5+).
    • Configure middleware in app/Http/Kernel.php:
      protected $middleware = [
          \Shetabit\Visitor\Middlewares\LogVisits::class,
      ];
      
    • Exclude routes in middleware:
      public function handle($request, Closure $next)
      {
          if ($request->is('admin/*') || $request->is('api/*')) {
              return $next($request);
          }
          // ... rest of logic
      }
      
  4. Model Integration:
    • Add Visitable trait to models for visit tracking:
      use Shetabit\Visitor\Traits\Visitable;
      
      class Post extends Model
      {
          use Visitable;
      }
      
    • Add Visitor trait to User model for online detection:
      use Shetabit\Visitor\Traits\Visitor;
      
      class User extends Authenticatable
      {
          use Visitor;
      }
      
  5. Testing:
    • Verify visitor data (e.g., $request->visitor()->browser()).
    • Test visit logging (e.g., visitor()->visit($post)).
    • Validate online user detection (e.g., User::online()->count()).

Compatibility

  • Laravel Versions: Officially supports 11–13; Laravel 10 is deprecated. Mitigation: Pin to v4.5.4 if using Laravel 10.
  • PHP Versions: Requires PHP 8.1+ (due to nullable types). Mitigation: Use a PHP 8.1+ runtime (e.g., Laravel Sail, Heroku PHP 8.2).
  • Database: Works with MySQL, PostgreSQL, SQLite. No schema changes needed beyond the visits table.
  • Third-Party Tools: Can coexist with:
    • Google Analytics: Use this for server-side tracking and GA for client-side.
    • PostHog: Use this for visit logs and PostHog for event tracking.
    • Redis: Cache online user lists to reduce DB queries.

Sequencing

  1. Phase 1: Core Integration (1–2 weeks):
    • Install and configure the package.
    • Add Visitable trait to 2–3 critical models (e.g., Post, Product).
    • Implement LogVisits middleware with route exclusions.
  2. Phase 2: Online User Features (1 week):
    • Add `
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation