PHPackages                             elegantly/laravel-referrer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. elegantly/laravel-referrer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

elegantly/laravel-referrer
==========================

Remember User Origin

v2.0.5(1y ago)122.8k↓16.7%[4 PRs](https://github.com/ElegantEngineeringTech/laravel-referrer/pulls)MITPHPPHP ^8.2CI passing

Since Aug 30Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ElegantEngineeringTech/laravel-referrer)[ Packagist](https://packagist.org/packages/elegantly/laravel-referrer)[ Docs](https://github.com/ElegantEngineeringTech/laravel-referrer)[ GitHub Sponsors](https://github.com/ElegantEngineeringTech)[ RSS](/packages/elegantly-laravel-referrer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (17)Used By (0)

Remember the Visitor Referrer Across Requests
=============================================

[](#remember-the-visitor-referrer-across-requests)

[![Latest Version on Packagist](https://camo.githubusercontent.com/214440bd20a827caa7b095dacb3ab85965fa0c5e9279baf60d5279a5252f07b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6567616e746c792f6c61726176656c2d72656665727265722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elegantly/laravel-referrer)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b70cd376e2724226a49a7aece567fb025c1a4e9d4d4246721e09ff6d28f33ceb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f456c6567616e74456e67696e656572696e67546563682f6c61726176656c2d72656665727265722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/ElegantEngineeringTech/laravel-referrer/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b264e70ee2acf423d99fcf917fbbdde9a186525d2313510b564bc2ad79f43175/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f456c6567616e74456e67696e656572696e67546563682f6c61726176656c2d72656665727265722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/ElegantEngineeringTech/laravel-referrer/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/e73cb8aa973bc8168738b0827fd89d2920e085ba69a47750ee1516e068c75f02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6567616e746c792f6c61726176656c2d72656665727265722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elegantly/laravel-referrer)

[![laravel-referrer](https://repository-images.githubusercontent.com/849508341/dce87156-6746-4177-8d49-f29e6be7de99)](https://repository-images.githubusercontent.com/849508341/dce87156-6746-4177-8d49-f29e6be7de99)

This package allows you to detect and store visitor referrers so that you can access them later. A typical use case would be saving the referrer in a database when a visitor registers in your app.

Out of the box, it captures:

- Google Click ID (gclid)
- Meta Click ID (fbclid)
- TikTok Click ID (ttclid)
- Header referrer value
- All URL UTM values
- Or add your own source

The package is designed to be highly flexible regarding:

- How you want to detect the referrer
- How you want to store the referrer value

Out of the box, it can store referrer values in:

- Laravel Context
- Laravel Session
- A Cookie
- Or add your own driver

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require elegantly/laravel-referrer
```

Usage
-----

[](#usage)

First, publish the config file with:

```
php artisan vendor:publish --tag="referrer-config"
```

### Configuration

[](#configuration)

Then configure your sources and drivers:

```
use Elegantly\Referrer\Enums\Strategy;

return [
    /*
    |--------------------------------------------------------------------------
    | Referrer Sources
    |--------------------------------------------------------------------------
    |
    | These classes contain the logic to detect the visitor's referrer.
    | You can disable specific sources or add as many as needed.
    | Regardless of the number of sources defined, all values will be stored.
    |
    */
    'sources' => [
        \Elegantly\Referrer\Sources\UtmReferrerSource::class,
        \Elegantly\Referrer\Sources\RequestHeaderSource::class,
        \Elegantly\Referrer\Sources\GoogleClickIdSource::class,
        \Elegantly\Referrer\Sources\MetaClickIdSource::class,
        \Elegantly\Referrer\Sources\TikTokClickIdSource::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Referrer Strategy
    |--------------------------------------------------------------------------
    |
    | Define the strategy for storing referrer values. You can also customize
    | the strategy for each source.
    |
    | 'last'  : Stores only the last captured value, ignoring previous ones.
    | 'first' : Stores only the first captured value, ignoring subsequent ones.
    | 'all'   : Stores all captured values.
    |
    */
    'strategy' => Strategy::All,

    /*
    |--------------------------------------------------------------------------
    | Referrer Drivers
    |--------------------------------------------------------------------------
    |
    | These classes contain the logic to store the visitor's referrer.
    | By default, all drivers are disabled. To enable a driver, uncomment it
    | or add a custom driver.
    |
    | Regardless of the number of drivers defined, they will all store
    | referrer sources. When accessing referrer data, drivers may be merged,
    | with the last driver overwriting the others.
    |
    | It is recommended to enable at least the Cookie driver for long-term
    | storage and the Session driver for immediate storage.
    |
    */
    'drivers' => [
        // \Elegantly\Referrer\Drivers\CookieDriver::class => [
        //     'name' => Str::slug(env('APP_NAME', 'laravel'), '_') . '_referrer',
        //     /**
        //      * Lifetime in seconds.
        //      */
        //     'lifetime' => 60 * 60 * 24 * 365,
        // ],
        // \Elegantly\Referrer\Drivers\SessionDriver::class => [
        //     'key' => 'referrer',
        // ],
        // \Elegantly\Referrer\Drivers\ContextDriver::class => [
        //     'key' => 'referrer',
        // ],
    ],
];
```

In the configuration file, enable one or more drivers by uncommenting them.

### Middleware Configuration

[](#middleware-configuration)

Next, add the `CaptureReferrerMiddleware` to your route:

[Using Laravel v11](https://laravel.com/docs/11.x/middleware#registering-middleware):

```
use Elegantly\Referrer\CaptureReferrerMiddleware;

->withMiddleware(function (Middleware $middleware) {
     $middleware->append(CaptureReferrerMiddleware::class);
})
```

[Using Laravel v10](https://laravel.com/docs/10.x/middleware#global-middleware):

```
use Elegantly\Referrer\CaptureReferrerMiddleware;

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middlewareGroups = [
        'web' => [
            // ...
            \Elegantly\Referrer\CaptureReferrerMiddleware::class,
        ],
    ];
}
```

Once configured, your referrer sources will be automatically captured.

### Choosing a Strategy

[](#choosing-a-strategy)

What happens when a user encounters multiple referrers during their navigation? For example, a user might click on multiple Google Ads campaigns or links with different UTM parameters.

This package supports 3 capture strategies:

- First: Only capture the first referrer of each source
- Last: Only capture the last referrer of each source
- All: Capture all referrers

From the config, you can define a strategy globally using:

```
use Elegantly\Referrer\Enums\Strategy;

return [
    //...
    'strategy' => Strategy::Last,
    //...
]
```

You can also define the strategy per source like this:

```
use Elegantly\Referrer\Enums\Strategy;

return [
    //...
    'strategy' => Strategy::Last, // Global strategy

    'sources' => [
        \Elegantly\Referrer\Sources\UtmReferrerSource::class => [
            'strategy' => Strategy::First, // Custom strategy for UTM referrers
        ],
        \Elegantly\Referrer\Sources\RequestHeaderSource::class,
        \Elegantly\Referrer\Sources\GoogleClickIdSource::class,
        \Elegantly\Referrer\Sources\MetaClickIdSource::class,
        \Elegantly\Referrer\Sources\TikTokClickIdSource::class,
    ],
    //...
]
```

### Retrieving the Visitor Referrer

[](#retrieving-the-visitor-referrer)

You can retrieve the referrer sources using the facade:

```
use \Elegantly\Referrer\Facades\Referrer;

Referrer::getSources(); // Merges all drivers together, with the last one having priority
Referrer::getSourcesByDriver(); // Retrieves all driver values
```

Retrieve the first or last referrer value:

```
use \Elegantly\Referrer\Sources\UtmReferrerSource;
use \Elegantly\Referrer\Sources\GoogleClickIdSource;
use \Elegantly\Referrer\Facades\Referrer;

Referrer::getSources()->getFirst(UtmReferrerSource::class);
// Returns an instance of UtmReferrerSource

Referrer::getSources()->getLast(GoogleClickIdSource::class);
// Returns an instance of GoogleClickIdSource
```

Retrieve the oldest or latest referrer value from any source:

```
use \Elegantly\Referrer\Facades\Referrer;

Referrer::getSources()->getOldest();
// Returns an instance of ReferrerSource

Referrer::getSources()->getLatest();
// Returns an instance of ReferrerSource
```

Here is a complete example inside a controller:

```
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\RegisterRequest;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Elegantly\Referrer\Facades\Referrer;
use \Elegantly\Referrer\Sources\UtmReferrerSource;

class RegisteredUserController extends Controller
{
    public function store(RegisterRequest $request)
    {
        $validated = $request->validated();

        $user = new User($validated);

        /**
         * The value returned will be the latest referrer value captured among all sources
         */
        $user->referrer = (string) Referrer::getSources()->getLatest();

        /**
         * The value returned will be the first UTM captured
         */
        $user->utm = (string) Referrer::getSources()->getFirst(UtmReferrerSource::class);

        $user->save();

        Auth::login($user);

        return redirect("/");
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) for information on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Quentin Gabriele](https://github.com/QuentinGab)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see the [License File](LICENSE.md) for more information.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance72

Regular maintenance activity

Popularity28

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 79.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~20 days

Recently: every ~36 days

Total

12

Last Release

402d ago

Major Versions

v1.3.0 → v2.0.02024-11-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/d769563728757858e249f266ac2555f28db5640124df92821ed17d93ee76fc7b?d=identicon)[QuentinGab](/maintainers/QuentinGab)

---

Top Contributors

[![QuentinGab](https://avatars.githubusercontent.com/u/40128136?v=4)](https://github.com/QuentinGab "QuentinGab (38 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

laravellaravel-packagephpreferrerreferrer-trackinglaravelElegantlylaravel-referrer

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/elegantly-laravel-referrer/health.svg)

```
[![Health](https://phpackages.com/badges/elegantly-laravel-referrer/health.svg)](https://phpackages.com/packages/elegantly-laravel-referrer)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
