Skip to content

Commit

Permalink
Merge pull request #6 from cyrildewit/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
cyrildewit authored Jun 17, 2017
2 parents 5bdb661 + 4579167 commit 2a8ba65
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
74 changes: 56 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Laravel Page Visit Counter

[![StyleCI](https://styleci.io/repos/94131608/shield?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![Build Status](https://travis-ci.org/cyrildewit/laravel-page-visits-counter.svg?branch=master)](https://travis-ci.org/cyrildewit/laravel-page-visits-counter)
[![Packagist](https://img.shields.io/packagist/v/cyrildewit/laravel-page-visits-counter.svg)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![Total Downloads](https://img.shields.io/packagist/dt/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)

This package allows you to store page visits of different models into the database.
Expand All @@ -9,10 +11,10 @@ Once installed you can do stuff like this:

```php
// Return total visits of the article
$article->total_visits_count
$article->total_visits_count->formatted

// Return total visits of last 24 hours
$article->last_24h_visits_count
$article->last_24h_visits_count->formatted

// Store new visit in the databae
$article->addVisit();
Expand All @@ -23,9 +25,37 @@ $article->addVisitThatExpiresAt(Carbon::now()->addHours(3));

This package is not built with the intent to collect analyticial data. It is made to simply save the visits of an Laravel model item. You would use our trait for models like `Task`, `Article`, `Post` or `Course`. But of course you can use this package as you want.

## Installation
## Overview

This package can be used in Laravel 5.4 or higher.
Laravel Page Visits Counter is a powerful, flexible and lightweight Laravel package for adding a page view counter to your Eloquent models. It's designed to be flexible and useful for various projects. Instead of only a simple visits counter we provide out of the box some great functionalities.

### Features

Here are some of the main features of Laravel Page Visits Counter:

* Add a new visit.
* Add a new visit with expiry date (history is stored in the session).
* Get the total visits.
* Get the total visits from the past: 24 hours, 7 days or 14 days.
* Retrieve the visits count formatted like 120.000 instead of 120000 (great for blade views)

## Documentation

In this documention you will find some helpful information about the use of this Laravel package. If you have any questions about this package or if you discover any security related issues, then feel free to get in touch with me at: info(at)cyrildewit.nl.

**In this documention:**

1. [Getting Started](#getting-started)
2. [Usage](#usage)
* [Making a Elqouent model visitable](#making-a-eloquent-model-visitable)
* [Retrieving page visits count](#retrieving-page-visits-count)
* [Storing new visits](#storing-new-visits)
3. [Configuration](#configuration)
* [Configuring the formatted number format](#configuring-the-formatted-number-format)

## Getting Started

Before you can use this package you have to install it with composer ;).

You can install the package via composer:
```winbatch
Expand Down Expand Up @@ -61,7 +91,13 @@ php artisan vendor:publish --provider="Cyrildewit\PageVisitsCounter\PageVisitsCo

## Usage

First add the `Cyrildewit\PageVisitsCounter\Traits\HasPageVisitsCounter` trait to your visitable model(s).
In the following sections you will find information about the usage of this package.

### Making a Elqouent model visitable

First add the `Cyrildewit\PageVisitsCounter\Traits\HasPageVisitsCounter` trait to your visitable Eloquent model(s).

Here's an example of a an Eloquent model:

```php
use Illuminate\Database\Eloquent\Model;
Expand All @@ -75,15 +111,15 @@ class Article extends Model
}
```

### Retrieving Page Visits Count
### Retrieving page visits count

```php
// Return total number of visits of the article.
$article->total_visits_count
Our attributes always return an object. This object contains two properties: `number` & `formatted`. As the names maybe suggests `total_visits_count->number` will return the whole number and `total_visits_count->formatted` will return a formatted string (120000 -> 120.000).

$article->last_24h_visits_count // Only in past 24 hours
$article->last_7d_visits_count // Only in past 7 days
$article->last_14d_visits_count // Only in past 14 days
```php
$article->total_visits_count // Retrieve all counted visits
$article->last_24h_visits_count // Retrieve all counted visits from the past 24 hours
$article->last_7d_visits_count // Retrieve all counted visits from the past 7 days
$article->last_14d_visits_count // Retrieve all counted visits from the past 14 days

// Retrieve visits from date (past 2 weeks)
$article->retrievePageVisitsFrom(Carbon::now()->subWeeks(2));
Expand All @@ -92,19 +128,23 @@ $article->retrievePageVisitsFrom(Carbon::now()->subWeeks(2));
$article->retrievePageVisitsCountBetween(Carbon::now()->subMonths(1), Carbon::now()->subWeeks(1));
```

### Adding a new visit
### Storing new visits

```php
// Add one visit
// Stores a new visit into the database
$article->addVisit()

// Add one with expriy date
// Store a new visit into the database with expiry date.
// When storing it, it will it's not already viewed by the current user.
// The visits will be stored into the session
$article->addVisitThatExpiresAt(Carbon::now()->addHours(2))
```

## Configuration

### Configuring the formatted number format

It is very easy to change the format of the converted numbers. Simply change the three parameters of the official function [`number_format()`](http://php.net/manual/en/function.number-format.php).
It is very easy to change the format of the converted numbers. Simply change the three parameters of the official PHP function [`number_format()`](http://php.net/manual/en/function.number-format.php).

In `config/page-visit-counter.php` you will find the following code:

Expand Down Expand Up @@ -144,8 +184,6 @@ return [
- [Cyril de Wit](https://github.com/cyrildewit)
- [All Contributors](../../contributors)

Special thanks to [Freek Van der Herten](https://github.com/freekmurze) who inspired me to create opensource Laravel packages. Before creating this package I didn't had any experience with creating a Laravel Package and how to use create composer.json files. Because he created a lot of opensource packages I could learn how to do it. Make sure you take a look at [his open source packages](https://spatie.be/nl/opensource/laravel) as well!

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
2 changes: 1 addition & 1 deletion src/Classes/SessionHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function removeExpiredVisitsFromSession(string $uniqueKey)
* @param string $value
* @return int The converted string.
*/
protected function fromCamelCaseToDashes(string $value)
public function fromCamelCaseToDashes(string $value)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $value, $matches);

Expand Down
2 changes: 1 addition & 1 deletion src/Models/PageVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->setTable(config('page-visits-counter.table_names.page-visits'));
$this->setTable(config('page-visits-counter.table_names.page-visits', 'page-visits'));
}
}
Empty file removed src/PageVisitsCounter.php
Empty file.
2 changes: 1 addition & 1 deletion src/PageVisitsCounterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function register()
// Merge the config file
$this->mergeConfigFrom(
__DIR__.'/../config/page-visits-counter.php',
'page-visits-counter.php'
'page-visits-counter'
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasPageVisitsCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait HasPageVisitsCounter
*/
public function __construct()
{
$this->configSettings = config('page-visits-counter.php');
$this->configSettings = config('page-visits-counter');

return parent::__construct();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCases/VisitVisitableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Cyrildewit\PageVisitsCounter\Test\TestCases;

use Carbon\Carbon;
use Cyrildewit\PageVisitsCounter\Test\TestCase;
use Cyrildewit\PageVisitsCounter\Classes\SessionHistory;

class VisitVisitableTest extends TestCase
{
Expand All @@ -26,4 +28,20 @@ public function it_can_store_new_visits_into_the_database()
$this->assertTrue($hasFirstVisit);
$this->assertTrue($hasSecondVisit);
}

/** @test */
public function it_can_store_new_visits_with_expiry_dates_into_the_database()
{
$uniqueKey = (new SessionHistory())->fromCamelCaseToDashes(class_basename($this->testTaskModel));
$visitable_id = $this->testTaskModel->id;

// Store new visit
$this->testTaskModel->addVisitThatExpiresAt(Carbon::now()->addSeconds(40));

$hasNewVisit = ($this->testTaskModel->total_visits_count->number === 1 ? true : false);
$this->assertTrue($hasNewVisit);

$hasNewVisitInSession = (new SessionHistory())->isItemVisited($uniqueKey, $visitable_id);
$this->assertTrue($hasNewVisitInSession);
}
}

0 comments on commit 2a8ba65

Please sign in to comment.