Giter Club home page Giter Club logo

laravel-referer's Introduction

Remember a visitor's original referer

Latest Version on Packagist Software License run-tests Total Downloads

Remember a visitor's original referer in session. The referer is (highest priority first):

  • The utm_source query parameter
  • The domain from the request's Referer header if there's an external host in the URL
  • Empty

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-referer

The package will automatically register itself in Laravel 5.5. In Laravel 5.4. you'll manually need to register the Spatie\Referer\RefererServiceProvider service provider in config/app.php.

You can publish the config file with:

php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"

Publishing the config file is necessary if you want to change the key in which the referer is stored in the session or if you want to disable a referer source.

return [

    /*
     * The key that will be used to remember the referer in the session.
     */
    'session_key' => 'referer',

    /*
     * The sources used to determine the referer.
     */
    'sources' => [
        Spatie\Referer\Sources\UtmSource::class,
        Spatie\Referer\Sources\RequestHeader::class,
    ],
];

Usage

To capture the referer, all you need to do is add the Spatie\Referer\CaptureReferer middleware to your middleware stack. In most configuration's, you'll only want to capture the referer in "web" requests, so it makes sense to register it in the web stack. Make sure it comes after Laravel's StartSession middleware!

// app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        // ...
        \Illuminate\Session\Middleware\StartSession::class,
        // ...
        \Spatie\Referer\CaptureReferer::class,
        // ...
    ],
    // ...
];

The easiest way to retrieve the referer is by just resolving it out of the container:

use Spatie\Referer\Referer;

$referer = app(Referer::class)->get(); // 'google.com'

Or you could opt to use Laravel's automatic facades:

use Facades\Spatie\Referer\Referer;

$referer = Referer::get(); // 'google.com'

The captured referer is (from high to low priority):

  • The utm_source query parameter, or:
  • The domain from the request's Referer header if there's an external host in the URL, or:
  • Empty

An empty referer will never overwrite an exisiting referer. So if a visitor comes from google.com and visits a few pages on your site, those pages won't affect the referer since local hosts are ignored.

Forgetting or manually setting the referer

The Referer class provides dedicated methods to forget, or manually set the referer.

use Facades\Spatie\Referer\Referer;

Referer::put('google.com');
Referer::get(); // 'google.com'
Referer::forget();
Referer::get(); // ''

Changing the way the referer is determined

The referer is determined by doing checks on various sources, which are defined in the configuration.

return [
    // ...
    'sources' => [
        Spatie\Referer\Sources\UtmSource::class,
        Spatie\Referer\Sources\RequestHeader::class,
    ],
];

A source implements the Source interface, and requires one method, getReferer. If a source is able to determine a referer, other sources will be ignored. In other words, the sources array is ordered by priority.

In the next example, we'll add a source that can use a ?ref query parameter to determine the referer. Additionally, we'll ignore ?utm_source parameters.

First, create the source implementations:

namespace App\Referer;

use Illuminate\Http\Request;
use Spatie\Referer\Source;

class RefParameter implements Source
{
    public function getReferer(Request $request): string
    {
        return $request->get('ref', ''); 
    }
}

Then register your source in the sources array. We'll also disable the utm_source while we're at it.

return [
    // ...
    'sources' => [
        App\Referer\RefParameter::class,
        Spatie\Referer\Sources\RequestHeader::class,
    ],
];

That's it! Source implementations can be this simple, or more advanced if necessary.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-referer's People

Contributors

adrianmrn avatar akoepcke avatar alexvanderbist avatar bouhnosaure avatar brendt avatar dakira avatar dmyers avatar freekmurze avatar laravel-shift avatar lasserafn avatar m1guelpf avatar mstrokin avatar nesk avatar nick322 avatar patinthehat avatar riasvdv avatar richardkeep avatar sebastiandedeyne avatar vurpa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-referer's Issues

laravel-referer 1.7.2 on Laravel 7.30.4 โ€” composer error on upgrade

  - Upgrading spatie/laravel-referer (1.7.1 => 1.7.2): Extracting archive
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover

   Error 

  Call to undefined method Illuminate\Container\ContextualBindingBuilder::giveConfig()

  at vendor/spatie/laravel-referer/src/RefererServiceProvider.php:28
    24|         $this->mergeConfigFrom(__DIR__.'/../config/referer.php', 'referer');
    25| 
    26|         $this->app->when(Referer::class)
    27|             ->needs('$sessionKey')
  > 28|             ->giveConfig('referer.session_key');
    29| 
    30|         $this->app->when(Referer::class)
    31|             ->needs('$sources')
    32|             ->giveConfig('referer.sources');

      +7 vendor frames 
  8   artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
Script @php artisan package:discover handling the post-autoload-dump event returned with error code 1

laravel 5.3

why not compatibility with laravel 5.3? :(

file_put_contents in multi tenant laravel

file_put_contents(/var/www/html/storage/tenant588eb4e1-1b9f-46e8-b82c-e2eed95d1df7/framework/cache/facade-ee040a0164bd556e8fd5adcc54cffcaf2cbb8205.php): Failed to open stream: No such file or directory"

Error encountered when running following command: php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"

After running the following command I encountered the following error.
Command: php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"
ERROR:

Uncaught Error: Call to undefined function Symfony\Component\Debug\Exception\get_debug_type() in C:\\...\....\....\....\vendor\symfony\debug\Exception\FatalThrowableError.php:29                                        

spatie/laravel-referer version: 1.3.3
Laravel version: 5.7
PHP version: 7.3
Laravel spark version: 7

Let me know if you need more information, Thanks

Spatie\Referer\Exceptions\InvalidConfiguration: `referer.session_key` can't be empty

I'm getting these errors on a health-check route.

Spatie\Referer\Exceptions\InvalidConfiguration: `referer.session_key` can't be empty
  File "/public/index.php", line 55
    $request = Illuminate\Http\Request::capture()

My config file:

return [

    /*
     * The key that will be used to remember the referer in the session.
     */
    'session_key' => 'referer',

    /*
     * The sources used to determine the referer.
     */
    'sources' => [
        Spatie\Referer\Sources\UtmSource::class,
        Spatie\Referer\Sources\RequestHeader::class,
    ],
];

Class 'App\Referer\RefParameter' not found

Laravel 7 gives the error Class 'App\Referer\RefParameter' not found

I am trying to capture

public function getReferer(Request $request): string
{
return $request->get('ref', '');
}

The error above is using up without even calling this package in a controller.

empty utm_source creates type error

If utm_source is empty (http://test.de/?utm_source) the method getReferer in UtmSource creates a type error:

Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Return value of Spatie\Referer\Sources\UtmSource::getReferer() must be of the type string, null returned

I think this is because Laravel converts empty strings to null by default.

Domain rather than full url?

I noticed the referring domain comes through rather than the full url. Is that intentional? Is there a way to get the full url?

referer value is <mail>

We were looking at the data collected using this package and found out that some referers are mail. What does if refer to?

Get full original url instead of one parameter

Hello

Thanks for great package. I can get utm_source as referer now. Is it possible if we can get the full URL which contains utm_source, utm_medium, utm_campaign, gclid etc. as referer instead ?

Thank you

Not compatible with Laravel 5.5.45?

I find composer dependency resolution explanations quite confusing, am I reading this right?

Using version ^1.4 for spatie/laravel-referer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for spatie/laravel-referer ^1.4 -> satisfiable by spatie/laravel-referer[1.4.0].
    - Conclusion: remove laravel/framework v5.5.45
    - Conclusion: don't install laravel/framework v5.5.45
    - spatie/laravel-referer 1.4.0 requires illuminate/http ~5.8.0 -> satisfiable by illuminate/http[v5.8.0, v5.8.2, v5.8.3].
    - don't install illuminate/http v5.8.0|don't install laravel/framework v5.5.45
    - don't install illuminate/http v5.8.2|don't install laravel/framework v5.5.45
    - don't install illuminate/http v5.8.3|don't install laravel/framework v5.5.45
    - Installation request for laravel/framework (locked at v5.5.45, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.45].


Installation failed, reverting ./composer.json to its original content.```

Can't locate path: ...referer.php

You can publish the config file with:
php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"
Error:
Can't locate path: </home/vagrant/code/homestead/vendor/spatie/laravel-referer/src/../resources/config/referer.php>
And:
Class 'App\Spatie\Referer\Referer' not found
And:
use Referer;
Class 'Referer' not found

In Laravel 5.4 with php 7.1

Not working on Lumen

I tried installing it on lumen 7.0

It gives the following error
Call to undefined function Spatie\Referer\config_path() (500 Internal Server Error)
image

Is there a fix for it ?

Update composer.json in order to support PHP 8.0

Hi,

I was updating my app to support PHP 8.0 runtime and i've got this issue on composer install :

Problem 1
    - spatie/laravel-referer is locked to version 1.7.0 and an update of this package was not requested.
    - spatie/laravel-referer 1.7.0 requires php ^7.2 -> your php version (8.0.0) does not satisfy that requirement.

I'll make a pull request in order to support PHP 8.0

Thanks and have a nice day !

Referrer from paid or organic

When a user comes to our website via Google, this user may have clicked on the paid advertisement of that website or may have come as a result of organic clicks. Can we distinguish these two situations with this library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.