Download the PHP package cyrildewit/laravel-page-visits-counter without Composer

On this page you can find all versions of the php package cyrildewit/laravel-page-visits-counter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-page-visits-counter

Eloquent Viewable

Packagist run-tests StyleCI Codecov branch Total Downloads license

This Laravel >= 6.0 package allows you to associate views with Eloquent models.

Once installed you can do stuff like this:

Overview

Sometimes you don't want to pull in a third-party service like Google Analytics to track your application's page views. Then this package comes in handy. Eloquent Viewable allows you to easiliy associate views with Eloquent models. It's designed with simplicity in mind.

This package stores each view record individually in the database. The advantage of this is that it allows us to make very specific counts. For example, if we want to know how many people has viewed a specific post between January 10 and February 17 in 2018, we can do the following: views($post)->period(Period::create('10-01-2018', '17-02-2018'))->count();. The disadvantage of this is that your database can grow rapidly in size depending on the amount of visitors your application has.

Features

Here are some of the main features:

Documentation

In this documentation, you will find some helpful information about the use of this Laravel package.

Table of contents

  1. Getting Started
    • Requirements
    • Installation
  2. Usage
    • Preparing your model
    • Recording views
    • Setting a cooldown
    • Retrieving views counts
      • Get total views count
      • Get views count of a specific period
      • Get total unique views count
    • Order models by views count
      • Order by views count
      • Order by unique views count
      • Order by views count within the specified period
      • Order by views count within the specified collection
    • Get views count of viewable type
    • View collections
    • Remove views on delete
    • Caching view counts
  3. Optimizing
    • Database indexes
    • Caching
  4. Extending
    • Custom information about visitor
    • Using your own Views Eloquent model
    • Using your own View Eloquent model
    • Using a custom crawler detector
    • Adding macros to the Views class

Getting Started

Requirements

This package requires PHP 7.4+ and Laravel 6+.

Support for Lumen is not maintained.

Version information

Version Illuminate Status PHP Version
^7.0 6.x.x - 11.x.x Active support by contributions >= 7.4.0

Installation

First, you need to install the package via Composer:

Secondly, you can publish the migrations with:

Finally, you need to run the migrate command:

You can optionally publish the config file with:

Register service provider manually

If you prefer to register packages manually, you can add the following provider to your application's providers list.

Usage

Preparing your model

To associate views with a model, the model must implement the following interface and trait:

Example:

Recording views

To make a view record, you can call the record method on the fluent Views instance.

The best place where you should record a visitors's view would be inside your controller. For example:

Note: This package filters out crawlers by default. Be aware of this when testing, because Postman is for example also a crawler.

Setting a cooldown

You may use the cooldown method on the Views instance to add a cooldown between view records. When you set a cooldown, you need to specify the number of minutes.

Instead of passing the number of minutes as an integer, you can also pass a DateTimeInterface instance.

How it works

When recording a view with a session delay, this package will also save a snapshot of the view in the visitor's session with an expiration datetime. Whenever the visitor views the item again, this package will checks his session and decide if the view should be saved in the database or not.

Retrieving views counts

Get total views count

Get views count of a specific period

The Period class that comes with this package provides many handy features. The API of the Period class looks as follows:

Between two datetimes
Since a datetime
Upto a datetime
Since past

Uses Carbon::today() as start datetime minus the given unit.

Since sub

Uses Carbon::now() as start datetime minus the given unit.

Get total unique views count

If you only want to retrieve the unique views count, you can simply add the unique method to the chain.

Order models by views count

The Viewable trait adds two scopes to your model: orderByViews and orderByUniqueViews.

Order by views count

Order by unique views count

Order by views count within the specified period

And of course, it's also possible with the unique views variant:

Order by views count within the specified collection

Get views count of viewable type

If you want to know how many views a specific viewable type has, you need to pass an empty Eloquent model to the views() helper like so:

You can also pass a fully qualified class name. The package will then resolve an instance from the application container.

View collections

If you have different types of views for the same viewable type, you may want to store them in their own collection.

To retrieve the views count in a specific collection, you can reuse the same collection() method.

Remove views on delete

To automatically delete all views of an viewable Eloquent model on delete, you can enable it by setting the removeViewsOnDelete property to true in your model definition.

Caching view counts

Caching the views count can be challenging in some scenarios. The period can be for example dynamic which makes caching not possible. That's why you can make use of the in-built caching functionality.

To cache the views count, simply add the remember() method to the chain. The default lifetime is forever.

Examples:

Optimizing

Database indexes

The default views table migration file has already two indexes for viewable_id and viewable_type.

If you have enough storage available, you can add another index for the visitor column. Depending on the amount of views, this may speed up your queries in some cases.

Caching

Caching views counts can have a big impact on the performance of your application. You can read the documentation about caching the views count here

Using the remember() method will only cache view counts made by the count() method. The orderByViews and orderByUnique query scopes aren't using these values because they only add something to the query builder. To optimize these queries, you can add an extra column or multiple columns to your viewable database table with these counts.

Example: we want to order our blog posts by unique views count. The first thing that may come to your mind is to use the orderByUniqueViews query scope.

This query is quite slow when you have a lot of views stored. To speed things up, you can add for example a unique_views_count column to your posts table. We will have to update this column periodically with the unique views count. This can easily be achieved using a schedued Laravel command.

There may be a faster way to do this, but such command can be like:

Extending

If you want to extend or replace one of the core classes with your own implementations, you can override them:

Note: Don't forget that all custom classes must implement their original interfaces

Custom information about visitor

The Visitor class is responsible for providing the Views builder information about the current visitor. The following information is provided:

The default Visitor class gets its information from the request. Therefore, you may experience some issues when using the Views builder via a RESTful API. To solve this, you will need to provide your own data about the visitor.

You can override the Visitor class globally or locally.

Create your own Visitor class

Create you own Visitor class in your Laravel application and implement the CyrildeWit\EloquentViewable\Contracts\Visitor interface. Create the required methods by the interface.

Alternatively, you can extend the default Visitor class that comes with this package.

Globally

Simply bind your custom Visitor implementation to the CyrildeWit\EloquentViewable\Contracts\Visitor contract.

Locally

You can also set the visitor instance using the useVisitor setter method on the Views builder.

Using your own Views Eloquent model

Bind your custom Views implementation to the \CyrildeWit\EloquentViewable\Contracts\Views.

Change the following code snippet and place it in the register method in a service provider (for example AppServiceProvider).

Using your own View Eloquent model

Bind your custom View implementation to the \CyrildeWit\EloquentViewable\Contracts\View.

Change the following code snippet and place it in the register method in a service provider (for example AppServiceProvider).

Using a custom crawler detector

Bind your custom CrawlerDetector implementation to the \CyrildeWit\EloquentViewable\Contracts\CrawlerDetector.

Change the following code snippet and place it in the register method in a service provider (for example AppServiceProvider).

Adding macros to the Views class

Now you're able to use this shorthand like this:

Upgrading

Please see UPGRADING for detailed upgrade guide.

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

See also the list of contributors who participated in this project.

Helpful Resources:

Alternatives

Feel free to add more alternatives!

License

This project is licensed under the MIT License - see the LICENSE file for details.


All versions of laravel-page-visits-counter with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
illuminate/cache Version ^6.0|^7.0|^8.0
illuminate/contracts Version ^6.0|^7.0|^8.0
illuminate/cookie Version ^6.0|^7.0|^8.0
illuminate/database Version ^6.0|^7.0|^8.0
illuminate/http Version ^6.0|^7.0|^8.0
illuminate/support Version ^6.0|^7.0|^8.0
jaybizzle/crawler-detect Version ^1.0
nesbot/carbon Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cyrildewit/laravel-page-visits-counter contains the following files

Loading the files please wait ....