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

Laravel Referer Laravel Package

spatie/laravel-referer

Store a visitor’s original referrer in the Laravel session. Detects from utm_source first, then the external domain from the Referer header, otherwise empty. Configurable session key and referrer sources via published config.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Lightweight & Non-Intrusive: The package is designed as a minimal middleware solution, making it a low-overhead addition to Laravel applications. It aligns well with Laravel’s middleware stack and does not introduce significant architectural complexity.
  • Session-Dependent: Leverages Laravel’s built-in session system, ensuring compatibility with Laravel’s core features (e.g., session drivers, encryption).
  • Priority-Based Logic: The referer resolution logic (UTM parameters → external Referer header → empty) is flexible and can be customized via configuration, allowing for alignment with business requirements (e.g., marketing attribution vs. general tracking).
  • Stateless Middleware: The package does not persist data beyond the session, reducing database or external dependency risks.

Integration Feasibility

  • Laravel Native: Built for Laravel 10+ (as of 2026), with no external dependencies beyond Laravel’s core. Integration requires minimal setup (publish config, register middleware).
  • Middleware Hooks: Can be inserted into Laravel’s middleware pipeline at any point (e.g., web group for web routes, or globally via AppServiceProvider). No route or controller modifications are needed.
  • Configuration Overrides: Supports customization via config/laravel-referer.php, allowing for adjustments like:
    • Whitelisting/blacklisting domains.
    • Modifying priority logic (e.g., prioritize Referer over UTM).
    • Session key overrides.

Technical Risk

  • Session Driver Dependencies:
    • Risk: Performance or reliability issues if the session driver (e.g., Redis, database) is misconfigured or overloaded.
    • Mitigation: Ensure session driver is optimized (e.g., Redis with proper eviction policies, database with indexing).
  • Referer Header Reliability:
    • Risk: Referer headers may be stripped (e.g., HTTPS → HTTP, privacy tools like uBlock), leading to incomplete data.
    • Mitigation: Monitor data quality post-deployment and supplement with fallback strategies (e.g., JavaScript-based tracking).
  • UTM Parameter Parsing:
    • Risk: Malformed UTM parameters (e.g., utm_source=) could cause edge cases.
    • Mitigation: Validate input in custom middleware if extending functionality.
  • Backward Compatibility:
    • Risk: Laravel version upgrades may require package updates (though Spatie maintains compatibility).
    • Mitigation: Test against Laravel’s LTS branches and monitor Spatie’s release notes.

Key Questions

  1. Use Case Clarity:
    • Is this for marketing attribution (UTM tracking), user journey analysis (referer paths), or another purpose? This dictates priority logic and success metrics.
  2. Data Sensitivity:
    • Does the referer data contain PII (e.g., internal URLs with user IDs)? If so, ensure compliance with GDPR/CCPA (e.g., anonymization, consent management).
  3. Performance Impact:
    • Will the middleware run on high-traffic routes? Benchmark session write operations under load.
  4. Analytics Integration:
    • How will this data be used downstream (e.g., Google Analytics, custom dashboards)? Ensure compatibility with existing pipelines.
  5. Fallback Strategy:
    • What happens if both Referer and UTM are unavailable? Should a default value (e.g., "direct") be set?
  6. Testing Scope:
    • Are there edge cases to test (e.g., subdomains, IP addresses, malformed headers)? Unit tests for middleware logic are recommended.

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Core Compatibility: Works seamlessly with Laravel’s session system, middleware stack, and routing. No conflicts expected with other Spatie packages or Laravel’s built-in features.
    • Session Drivers: Supports all Laravel session drivers (file, database, Redis, Memcached). Performance may vary; prefer Redis/Memcached for high-scale apps.
    • Caching: If using cached views or responses, ensure the middleware runs before caching layers (e.g., place it early in the web group).
  • PHP Version: Requires PHP 8.1+ (as of 2026), aligning with Laravel 10+.
  • Dependencies: Zero external dependencies beyond Laravel, reducing attack surface.

Migration Path

  1. Pre-Integration:
    • Review existing referer tracking (if any) to avoid duplication.
    • Audit session driver configuration for performance/latency.
  2. Installation:
    composer require spatie/laravel-referer
    php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"
    
  3. Configuration:
    • Publish and customize config/laravel-referer.php (e.g., adjust priorities, whitelists).
    • Example: Disable Referer header checks for internal domains:
      'ignore_domains' => [
          'app.example.com',
          '*.internal.example.com',
      ],
      
  4. Middleware Registration:
    • Add to app/Http/Kernel.php:
      protected $middlewareGroups = [
          'web' => [
              // ...
              \Spatie\Referer\Middleware\StoreReferer::class,
          ],
      ];
      
    • For API routes, consider conditional registration (e.g., only for authenticated users).
  5. Data Access:
    • Retrieve referer via:
      $referer = session('referer');
      
    • Integrate with analytics (e.g., pass to a service layer or log to a database).

Compatibility

  • Laravel Versions: Tested against Laravel 10+; may require adjustments for older versions.
  • Custom Middleware: Extend StoreReferer middleware if additional logic is needed (e.g., logging, enrichment).
  • Third-Party Packages:
    • Conflicts unlikely, but test with packages that modify sessions (e.g., laravel-session, spatie/laravel-activitylog).
    • If using spatie/laravel-utm, ensure UTM parsing logic aligns with referer priorities.

Sequencing

  1. Order in Middleware Pipeline:
    • Place StoreReferer early in the web group to capture referer data before other middleware (e.g., auth, CORS) might alter the request.
    • Example pipeline order:
      TrustProxies
      StoreReferer          <-- Add here
      StartSession
      Authenticate
      ...
      
  2. Conditional Execution:
    • Skip for non-HTTP requests (e.g., API calls without Referer headers) using middleware conditions:
      public function handle(Request $request, Closure $next) {
          if (!$request->is('web/*')) return $next($request);
          // Store referer logic
      }
      
  3. Post-Deployment:
    • Validate data in staging (e.g., compare Referer headers vs. stored session values).
    • Monitor for missing referer data (e.g., alerts if session('referer') is null >5% of requests).

Operational Impact

Maintenance

  • Package Updates:
    • Low maintenance burden; Spatie provides minor updates for Laravel compatibility.
    • Monitor release notes for breaking changes.
  • Configuration Drift:
    • Centralized config (config/laravel-referer.php) reduces risk of environment-specific issues.
    • Use environment variables for sensitive settings (e.g., ignored domains).
  • Deprecation:
    • MIT license ensures no forced vendor lock-in. Easy to fork or replace if needed.

Support

  • Troubleshooting:
    • Common issues:
      • Missing referer data: Check Referer headers (use dd($request->header('Referer')) in middleware).
      • Session not persisting: Verify session driver and encryption settings.
    • Debugging tools:
      • Laravel’s session() helper to inspect stored data.
      • telescope/laravel-telescope for middleware execution logs.
  • Documentation:
    • Comprehensive README with examples. Changelog tracks features/bugfixes.
    • Community support via GitHub issues (527 stars indicate active usage).

Scaling

  • Performance:
    • Session Writes: Each request writes to session storage. For high traffic:
      • Use Redis/Memcached for session driver.
      • Consider batching or async writes if referer data is only needed periodically.
    • Middleware Overhead: Negligible (~1–5ms per request; benchmark in staging).
  • Data Volume:
    • Referer data is ephemeral (session-bound). No long-term storage impact.
    • If archiving referer data, implement a separate job (e.g., spatie/laravel-queueable).
  • Horizontal Scaling:
    • Stateless middleware; scales with Laravel’s horizontal scaling (e.g., queue workers, load balancers).

Failure Modes

Failure Scenario Impact Mitigation
Session driver failure (e.g., Redis down) Lost referer data for session duration Use fallback session driver (
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
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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