Git Product home page Git Product logo

laravel-visits's Introduction

Laravel Visits

aravel-visits

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

Introduction

Laravel Visits is a counter that can be attached to any model to track its visits with useful features like IP-protection and lists caching.

Table of Contents

Features

  • A model item can have many types of recorded visits (using tags).
  • It's not limited to one type of Model (like some packages that allow only User model).
  • Record per visitors and not by vistis using IP detecting, so even with refresh, visit won't duplicate (can be changed from config).
  • Get Top/Lowest visits per a model.
  • Get most visited countries, refs, OSes, and languages.
  • Get visits per a period of time like a month of a year of an item or model.
  • Supports multiple data engines: Redis or database (any SQL engine that Eloquent supports).

Install

To get started with Laravel Visits, use Composer to add the package to your project's dependencies:

composer require awssat/laravel-visits

Requirement

  • Laravel 5.5+
  • PHP 7.2+
  • Data engines options (can be configured from config/visits.php):
    • Redis: make sure that Redis is configured and ready. (see Laravel Redis Configuration)
    • Database: publish migration file: php artisan vendor:publish --provider="Awssat\Visits\VisitsServiceProvider" --tag="migrations" then migrate.

Config

To adjust the package to your needs, you can publish the config file to your project's config folder using:

php artisan vendor:publish --provider="awssat\Visits\VisitsServiceProvider"

Note : Redis Database Name

  • By default laravel-visits doesn't use the default laravel redis configuration (see issue #5)

To prevent any data loss add a new connection on config/database.php

'laravel-visits' => [
    'host' => env('REDIS_HOST', '127.0.0.1'),
    'password' => env('REDIS_PASSWORD', null),
    'port' => env('REDIS_PORT', 6379),
    'database' => 3, // anything from 1 to 15, except 0 (or what is set in default)
],

and you can define your redis connection name on config/visits.php

'connection' => 'default' // to 'laravel-visits'

Usage

It's simple. Using visits helper as:

visits($model)->{method}()

Where:

  • $model: is any Eloquent model from your project.
  • {method}: any method that is supported by this library, and they are documented below.

Tags

  • You can track multiple kinds of visits to a single model using the tags as visits($model,'tag1')->increment()

Increments and Decrements

Increment

One
visits($post)->increment();
More than one
visits($post)->increment(10);

Decrement

One
visits($post)->decrement();
More than one
visits($post)->decrement(10);

Only increment/decrement once during x seconds (based on visitor's IP)

visits($post)->seconds(30)->increment()

Note: this will override default config setting (once each 15 minutes per IP).

Force increment/decrement

visits($post)->forceIncrement();
visits($post)->forceDecrement();
  • This will ignore IP limitation and increment/decrement every visit.

An item visits

All visits of an item

visits($post)->count();

Note: $post is a row of a model, i.e. $post = Post::find(22);

Item's visits by a period

visits($post)->period('day')->count();

A model class visits

All visits of a model type

visits('App\Post')->count();

Visits of a model type in period

visits('App\Post')->period('day')->count();

Countries of visitors

visits($post)->countries();

Referers of visitors

visits($post)->refs();

Operating Systems of visitors

visits($post)->operatingSystems();

Languages of visitors

visits($post)->languages();

Top or Lowest list per model type

Top/Lowest 10

visits('App\Post')->top(10);
visits('App\Post')->low(10);

Uncached list

visits('App\Post')->fresh()->top(10);

Note: you can always get uncached list by enabling alwaysFresh from package config.

By a period of time

visits('App\Post')->period('month')->top(10);

Reset and clear values

Clear an item visits

visits($post)->reset();

Clear an item visits of specific period

visits($post)->period('year')->reset();

Clear recorded visitors' IPs

visits($post)->reset('ips');
visits($post)->reset('ips','127.0.0.1');

Periods options

  • minute
  • hour
  • xhours [1hours ... to 12hours]
  • day
  • week
  • month
  • year
  • quarter
  • decade
  • century

you can also make your custom period by adding a carbon marco in AppServiceProvider:

Carbon::macro('endOf...', function () {
    //
});

Other

//clear all visits of the given model and its items
visits('App\Post')->reset();
//clear all cache of the top/lowest list
visits('App\Post')->reset('lists');
//clear visits from all items of the given model in a period
visits('App\Post')->period('year')->reset();
//...?
visits('App\Post')->reset('factory');
//increment/decrement methods offer ignore parameter to stop recording any items of ('country', 'refer', 'periods', 'operatingSystem', 'language')
visits('App\Post')->increment(1, false, ['country']);

Integration with Eloquent

You can add a visits method to your model class:

public function visits()
{
    return visits($this);
}

Then you can use it as:

$post = Post::find(1);
$post->visits()->increment();
$post->visits()->count();

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Credits

Todo

  • An export command to save visits of any periods to a table on the database.

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

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

laravel-visits's People

Contributors

bsn4 avatar abdumu avatar realrashid avatar monkeywithacupcake avatar ahoiroman avatar vrajroham avatar

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.