Coder Social home page Coder Social logo

counter's Introduction

Hit Counter for Laravel 5.*

Installation

Run the following command:

composer require kryptonit3/counter:5.2.*

Add the following to your config\app.php Service Providers

Kryptonit3\Counter\CounterServiceProvider::class,

Add the following to your config\app.php Facades

'Counter' => Kryptonit3\Counter\Facades\CounterFacade::class,

Then run the following:

php artisan vendor:publish --provider="Kryptonit3\Counter\CounterServiceProvider" --tag="migrations"

php artisan migrate

Set Cookie Name in Laravel env Config file

    COUNTER_COOKIE="Name of your cookie"

How to Use

Regular pages

Just add Counter::showAndCount('home') ( for blades uses {{ Counter::showAndCount('home') }} ). Change home to a unique name for the page you are working with.

Dynamic pages

For dynamic pages, such as user profiles, or job listings etc you may provide a dynamic element like this Counter::showAndCount('user-profile', $user->id) ( for blades use {{ Counter::showAndCount('user-profile', $user->id) }} )

Change user-profile to a unique name for the page you are working with.

Number output is already formatted. So 3000 visitors will render as 3,000

If you have records on a page but do not want to count visiting the page displaying the records as a hit on the record itself then change showAndCount to just show. Example Counter::show('job-listings', $job->id) ( for blades use {{ Counter::show('job-listings', $job->id) }} )

You may get all hits for every page on the entire site with Counter::allHits() ( for blades use {{ Counter::allHits() }} ). To specify a day constraint, like only all hits for the past 30 days then do Counter::allHits(30) (for blades use {{ Counter::allHits(30) }} ).

If you would just like to process a hit for a page without displaying anything then just use Counter::count('user-profile', $user->id). Works the same as all previous examples for both static and dynamic pages, blade is the same syntax. Useful for counting hits for a page without letting everyone see.

Enjoy!

Extra

This package lets you see hit counts for specific pages/objects as well as an overall site hit count. It also uses a uniquely generated cookie (fallback to IP) to give a more accurate reading. Nice package for the pro-anonymous people :)

mysql> select * from kryptonit3_counter_page;
+----+--------------------------------------+
| id | page                                 |
+----+--------------------------------------+
|  2 | 24d83c12-a1e0-598b-93ee-df05ae3f87e7 |
|  1 | 597e0526-152f-5fc0-9d44-b51fd9e45b8f |
+----+--------------------------------------+
2 rows in set (0.00 sec)

mysql> select * from kryptonit3_counter_visitor;
+----+------------------------------------------------------------------+
| id | visitor                                                          |
+----+------------------------------------------------------------------+
|  1 | 88a5f67524a1bc75da5ea8b7250e8280c78d60dce59b129dc37123b137ce6199 |
+----+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from kryptonit3_counter_page_visitor;
+---------+------------+---------------------+
| page_id | visitor_id | created_at          |
+---------+------------+---------------------+
|       1 |          1 | 2015-06-22 17:52:43 |
|       2 |          1 | 2015-06-22 17:52:43 |
+---------+------------+---------------------+
2 rows in set (0.00 sec)

Pull requests are welcome.

counter's People

Contributors

ahmad-safar avatar johnhout avatar kanasite avatar kryptonit3 avatar reflow1319 avatar vitivs avatar

Watchers

 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.