krowek/view-counter-bundle

The "View Counter" bundle

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 9

Type:symfony-bundle


README

Welcome to the "TchoulomViewCounterBundle".

This bundle is used to count the number of views of a web page (the viewership).

This bundle can also be used to draw a graphical representation of statistical data of the web pages.

Monthly views in 2018

Table of contents

Features include

- Viewcounter
- Statistics
- Geolocation

Documentation

The ViewCounter documentation

Installation

Step 1: Download TchoulomViewCounterBundle using composer

You can install it via Composer:

$ php composer.phar update tchoulom/view-counter-bundle

or

 $ composer require tchoulom/view-counter-bundle

or

 $ composer req tchoulom/view-counter-bundle

Check that it is recorded in the composer.json file

{
    "require": {
        ...
        "tchoulom/view-counter-bundle": "^4.0"
        ...
    }
}

Step 2: Enable the Bundle

Edit the appKernel.php file

    ...
    $bundles = array(
	     ...
	     new Tchoulom\ViewCounterBundle\TchoulomViewCounterBundle(),
	     ...
      );
     ...

Usage

Step 1: Interface and Property

Suppose that you have an Article Entity.

This Entity must implement the ViewCountable interface:

   use Tchoulom\ViewCounterBundle\Model\ViewCountable;
   
    ...
    class Article implements ViewCountable
    {
      ...
    }

Add the $views property and the target Entity ViewCounter.

The $views property allows to get the number of views:

   use Tchoulom\ViewCounterBundle\Model\ViewCountable;
   use Entity\ViewCounter;
   use Doctrine\Common\Collections\ArrayCollection;

    ...

    class Article implements ViewCountable
    {
      ...

    /**
      * @var integer
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     protected $id;

      /**
       * @ORM\OneToMany(targetEntity="Entity\ViewCounter", mappedBy="article")
       */
      protected $viewCounters;
          
      /**
      * @ORM\Column(name="views", type="integer", nullable=true)
      */
       protected $views = 0;
       
       /**
        * Constructor
        */
       public function __construct()
       {
           $this->viewCounters = new ArrayCollection();
       }

        /**
        * Gets id
        *
        * @return integer
        */
        public function getId()
        {
            return $this->id;
        }
       
       /**
        * Sets $views
        *
        * @param integer $views
        *
        * @return $this
        */
       public function setViews($views)
       {
           $this->views = $views;
   
           return $this;
       }
   
       /**
        * Gets $views
        *
        * @return integer
        */
       public function getViews()
       {
           return $this->views;
       }
       
       /**
        * Get $viewCounters
        *
        * @return Collection
        */
       public function getViewCounters()
       {
           return $this->viewCounters;
       }
   
       /**
        * Add $viewCounter
        *
        * @param ViewCounter $viewCounter
        *
        * @return $this
        */
       public function addViewCounter(ViewCounter $viewCounter)
       {
           $this->viewCounters[] = $viewCounter;
   
           return $this;
       }
   
       /**
        * Remove $viewCounter
        *
        * @param ViewCounter $viewCounter
        */
       public function removeViewCounter(ViewCounter $viewCounter)
       {
           $this->viewCounters->removeElement($viewCounter);
       }
      ...
    }

Step 2: ViewCounter

The ViewCounter Entity allows to set the IP address, the view_date, and the article_id.

The ViewCounter Entity must extend the BaseViewCounter:

    use Tchoulom\ViewCounterBundle\Entity\ViewCounter as BaseViewCounter;
    
    /**
     * ViewCounter.
     *
     * @ORM\Table(name="view_counter")
     * @ORM\Entity()
     */
    class ViewCounter extends BaseViewCounter
    {
        ...
    }

Update the doctrine relationship between the ViewCounter Entity and your Article Entity:

    use Tchoulom\ViewCounterBundle\Entity\ViewCounter as BaseViewCounter;
    
    /**
     * ViewCounter.
     *
     * @ORM\Table(name="view_counter")
     * @ORM\Entity()
     */
    class ViewCounter extends BaseViewCounter
    {
        ...
        
        /**
         * @ORM\ManyToOne(targetEntity="Article", cascade={"persist"}, inversedBy="viewCounters")
         * @ORM\JoinColumn(nullable=true)
         */
        private $article;
    
        /**
         * Gets article
         *
         * @return Article
         */
        public function getArticle()
        {
            return $this->article;
        }
    
        /**
         * Sets Article
         *
         * @param Article $article
         *
         * @return $this
         */
        public function setArticle(Article $article)
        {
            $this->article = $article;
    
            return $this;
        }
        
        ...
    }

Step 3: Configuration

For symfony 4 or 5:
  • Create the file config/packages/tchoulom_viewcounter.yaml
  • Add the following viewcounter configuration in the tchoulom_viewcounter.yaml file.
For version of symfony less than 4:
  • Add the following viewcounter configuration in the app/config.yml file.
viewcounter configuration:
    tchoulom_view_counter:
        view_counter:
            view_strategy: daily_view
        statistics:
            use_stats: false
            stats_file_name: stats
            stats_file_extension:
        geolocation:
            geolocator_id: App\Service\Geolocator

The "view_counter"

The different values of view_strategy are : daily_view, unique_view, increment_each_view, hourly_view, weekly_view, monthly_view, yearly_view, view_per_minute, view_per_second.

  • The daily_view allows to increment daily, for a given IP address, the number of views of an Article (the viewership). In fact it increments the $views property.

  • The unique_view allows to set to 1, for a given IP address, the number of view of an article

  • The increment_each_view allows to increment the number of views of an Article every time the user will refresh the page

  • The hourly_view allows to increment hourly, for a given IP address, the number of views of an Article (the viewership).

  • The weekly_view allows to increment weekly, for a given IP address, the number of views of an Article (the viewership).

  • The monthly_view allows to increment monthly, for a given IP address, the number of views of an Article (the viewership).

  • The yearly_view allows to increment yearly, for a given IP address, the number of views of an Article (the viewership).

  • The view_per_minute allows to increment every minute, for a given IP address, the number of views of an Article (the viewership).

  • The view_per_second allows to increment every second, for a given IP address, the number of views of an Article (the viewership).

The "statistics"

The use_stats allows to indicate if you want to use statistics.

If use_stats is set to true, statistics functionality will be used (confers the Step 6).

The stats_file_name allows to define the name of the statistics file.

The default name of stats_file_name is stats

The stats_file_extension allows to define the extension of the statistics file.

Example :

If stats_file_extension: txt, then the default name of the statistics file will be stats.txt

If stats_file_extension:, then the default name of the statistics file will be stats

The full path of the statistics file is var/viewcounter of your project.

The "geolocation"

The Geolocation defines a service which will allow you to geolocate page visits.

The geolocator_id corresponds to the identifier or the name of the class of your geolocation service, depending on the version of symfony used:

    tchoulom_view_counter:
        ...
        geolocation:
            geolocator_id: app.service.geolocator

or

    tchoulom_view_counter:
        ...
        geolocation:
            geolocator_id: App\Service\Geolocator

if your service is declared as such:

    app.service.geolocator:
        class: App\Service\Geolocator

You must then set up your "Geolocator" service as we will see in this documentation Step 6: The Geolocation.

You must comment on the geolocation configuration if you do not want to use it in your project:
    tchoulom_view_counter:
        ...
        # geolocation:
        #    geolocator_id: app.service.geolocator

Step 4: The Controller

2 methods are available:

Method 1

use App\Entity\ViewCounter;
use Tchoulom\ViewCounterBundle\Counter\ViewCounter as Counter;
...

// For Symfony 4 or 5, inject the ViewCounter service

/**
 * @var Counter
 */
protected $viewcounter;

/**
 * @param Counter $viewCounter
 */
public function __construct(Counter $viewCounter)
{
    $this->viewcounter = $viewCounter;
}

/**
 * Reads an existing article
 *
 * @Route("/read/{id}", name="read_article")
 * @ParamConverter("article", options={"mapping": {"id": "id"}})
 * @Method({"GET", "POST"})
 */
 public function readAction(Request $request, Article $article)
 {
    // Viewcounter
    $viewcounter = $this->get('tchoulom.viewcounter')->getViewCounter($article);
    // For Symfony 4 or 5
    $viewcounter = $this->viewcounter->getViewCounter($article);
    
    $em = $this->getDoctrine()->getEntityManager();
    
    if ($this->viewcounter->isNewView($viewcounter)) {
        $views = $this->viewcounter->getViews($article);
        $viewcounter->setIp($request->getClientIp());
        $viewcounter->setArticle($article);
        $viewcounter->setViewDate(new \DateTime('now'));
    
        $article->setViews($views);
    
        $em->persist($viewcounter);
        $em->persist($article);
        $em->flush();
        ...
    }
 }
...

Method 2

You only need to save your Article Entity via the 'tchoulom.viewcounter' service:

...

use Tchoulom\ViewCounterBundle\Counter\ViewCounter as Counter;

// For Symfony 4 or 5, inject the ViewCounter service

/**
 * @var Counter
 */
protected $viewcounter;

/**
 * @param Counter $viewCounter
 */
public function __construct(Counter $viewCounter)
{
    $this->viewcounter = $viewCounter;
}

/**
 * Reads an existing article
 *
 * @Route("/read/{id}", name="read_article")
 * @ParamConverter("article", options={"mapping": {"id": "id"}})
 * @Method({"GET", "POST"})
 */
public function readAction(Request $request, Article $article)
{
    // Saves the view
    $page = $this->get('tchoulom.viewcounter')->saveView($article);
    // For Symfony 4 or 5
    $page = $this->viewcounter->saveView($article);
    ...
}

The second method returns the current page ($article).

You can choose the method that is most appropriate for your situation.

Step 5: The View

Finally you can display the number of views:

...
<h1>The number of views of this article :</h1> {{ article.views }}
...

Step 6: The Geolocation

Some bundles can be used to have a geolocation system in your project. These bundles usually use the ip address in order to geolocate the visitor of the web page.

For the purposes of this documentation, we will use this bundle, for example:

gpslab/geoip2 : https://github.com/gpslab/geoip2

You can read the documentation for installing and using this bundle if you want to use it.

Otherwise, you can use another geolocation bundle according to your preferences.

Create the "Geolocator" service that will allow you to manage geolocation data

<?php

namespace App\Service;

use GeoIp2\Database\Reader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Tchoulom\ViewCounterBundle\Adapter\Geolocator\GeolocatorInterface;

/**
 * Class Geolocator
 *
 * This service must implements the "GeolocationInterface".
 */
class Geolocator implements GeolocatorInterface
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var Reader
     */
    protected $reader;

    /**
     * Geolocator constructor.
     *
     * @param RequestStack $requestStack
     * @param Reader $reader
     */
    public function __construct(RequestStack $requestStack, Reader $reader)
    {
        $this->request = $requestStack->getCurrentRequest();
        $this->reader = $reader;
    }

    /**
     * Gets the record.
     * 
     * @return \GeoIp2\Model\City|mixed
     * @throws \GeoIp2\Exception\AddressNotFoundException
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException
     */
    public function getRecord()
    {
        $clientIp = $this->request->getClientIp();

        return $this->reader->city($clientIp);
    }

    /**
     * Gets the continent.
     *
     * @return string
     * @throws \GeoIp2\Exception\AddressNotFoundException
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException
     */
    public function getContinent(): string
    {
        return $this->getRecord()->continent->name;
    }

    /**
     * Gets the country.
     * 
     * @return string
     * @throws \GeoIp2\Exception\AddressNotFoundException
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException
     */
    public function getCountry(): string
    {
        return $this->getRecord()->country->name;
    }

    /**
     * Gets the region.
     * 
     * @return string
     * @throws \GeoIp2\Exception\AddressNotFoundException
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException
     */
    public function getRegion(): string
    {
        return $this->getRecord()->subdivisions[0]->names['en'];
    }

    /**
     * Gets the city.
     * 
     * @return string
     * @throws \GeoIp2\Exception\AddressNotFoundException
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException
     */
    public function getCity(): string
    {
        return $this->getRecord()->city->name;
    }
}

Your Geolocation service must implement the "Tchoulom\ViewCounterBundle\Adapter\Geolocator\GeolocatorInterface" interface.

you are free to improve the above "Geolocator" service, in particular to verify the existence of geolocation data.

You can go to this step for the use of geolocation data Search for geolocation data.

Step 7: Exploitation of statistical data

Trick

For Symfony 4 or 5, inject the service you want to use, instead of going through the container: $this->get('tchoulom.viewcounter.stats_finder');

  • Example:
use Tchoulom\ViewCounterBundle\Finder\StatsFinder:

/**
 * @var StatsFinder
 */
protected $statsFinder;

/**
 * @param StatsFinder $statsFinder
 */
public function __construct(StatsFinder $statsFinder)
{
    $this->statsFinder = $statsFinder;
}

The StatsFinder service

Use the StatsFinder service to get statistics of a web page :

   // The "statsFinder" service
   $statsFinder = $this->get('tchoulom.viewcounter.stats_finder');
   
   // Get all statistical data
   $contents = $statsFinder->loadContents();
   
   // Finds statistics by page
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Page
   $page = $statsFinder->findByPage($article);
    
   // Finds statistics by year (year number: 2019)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Year
   $year = $statsFinder->findByYear($article, 2019);
     
   // Finds statistics by month (month number: 1)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Month
   $month = $statsFinder->findByMonth($article, 2019, 1);
   
   // Finds statistics by week (week number: 3)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Week
   $week = $statsFinder->findByWeek($article, 2019, 1, 3);
   
   // Finds statistics by day (name of the day: 'thursday')
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Day
   $day = $statsFinder->findByDay($article, 2019, 1, 3, 'thursday');
   
   // Finds statistics by hour (time name: 'h17' => between 17:00 and 17:59)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Hour
   $hour = $statsFinder->findByHour($article, 2019, 1, 3, 'thursday', 'h17');
   
   // Finds statistics by minute (the name of the minute: 'm49' => in the 49th minute)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Minute
   $minute = $statsFinder->findByMinute($article, 2019, 1, 3, 'thursday', 'h17', 'm49');
   
   // Finds statistics by second (the name of the second: 's19' => in the 19th second)
   // Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Second
   $second = $statsFinder->findBySecond($article, 2019, 1, 3, 'thursday', 'h17', 'm49', 's19');
   

You can also get statistical data of a web page by year, month, week, day, hour, minute and second

Get the yearly statistics

   // Get the yearly statistics
   $yearlyStats = $statsFinder->getYearlyStats($article); 

Result:

   [
      [2019,98537215], [2018,95548144], [2017,47882376]
   ]

In 2019, there were 98537215 views.

In 2018, there were 95548144 views.

In 2017, there were 47882376 views.

Get the monthly statistics

   // Get the monthly statistics in 2019
   $monthlyStats = $statsFinder->getMonthlyStats($article, 2019);

Result:

   [
      [8,951224], [7,921548], [6,845479]
   ]

In the month of August (month number 8) 2019, there were 951224 views.

In the month of July (month number 7) 2019, there were 921548 views.

In the month of June (month number 6) 2019, there were 845479 views.

Get the weekly statistics

   // Get the weekly statistics of the month of August (month number 8) in 2019
   $weeklyStats = $statsFinder->getWeeklylyStats($article, 2019, 8);

Result:

   [
      [34,494214], [33,117649], [32,183254]
   ]

In the week number 34 in august (month number 8) 2019, there were 494214 views.

In the week number 33 in august 2019, there were 117649 views.

In the week number 32 in august 2019, there were 183254 views.

Get the daily statistics

   // Get the daily statistics of the week number 33 in august 2019
   $dailyStats = $statsFinder->getDailyStats($article, 2019, 8, 33);

Result:

   [
      ['Monday',16810],['Tuesday',16804],['Wednesday',16807],['Thursday',16807],['Friday',16807],['Saturday',16807],['Sunday',16807]
   ]

On Monday of the week number 33 in august (month number 8) 2019, there were 16810 views.

On Tuesday of the week number 33 in august 2019, there were 16804 views.

On Wednesday of the week number 33 in august 2019, there were 16807 views.

...

Get the hourly statistics

   // Get the hourly statistics for Thursday of the week number 33 in august 2019
   $hourlyStats = $statsFinder->getHourlyStats($article, 2019, 8, 33, 'Thursday');

Result:

   [
      ['00',650],['01',750],['02',500],['03',900],['04',700],['05',700],['06',700],['07',700],['08',700],['09',700],['10',700],['11',720],['12',680],['13',700],['14',200],['15',1200],['16',700],['17',700],['18',700],['19',700],['20',100],['21',1300],['22',700],['23',700]
   ]

On Thursday of the week number 33 of August (month number 8) 2019:

  • At midnight, there were 650 views.

  • At 1 hour, there were 750 views.

  • At 2 hour, there were 500 views.

  • At 3 hour, there were 900 views.

  • ...

Get the statistics per minute

   // Get the statistics per minute on Saturday of the week number 33 in august 2019 at 15h ('h15')
   $statsPerMinute = $this->get('tchoulom.viewcounter.stats_finder')->getStatsPerMinute($article, 2019, 8, 33, 'Saturday', 'h15');

Result:

   [
      ['00',650],['01',740],['02',520],['03',752],['04',700],['05',700],['06',400],['07',400],['08',800],['09',700],['10',700],['11',720],['12',680],['13',700],['14',200],['15',100],['16',105],['17',700],['18',700],['19',700],['20',100],['21',130],['22',700],['23',700],['24',110],['25',210],['26',110],['27',10],['28',110],['29',10],['30',141],['31',148],['32',181],['33',141],['34',141],['35',171],['36',141],['37',181],['38',141],['39',141],['40',191],['41',193],['42',194],['43',194],['44',191],['45',191],['46',148],['47',191],['48',191],['49',191],['50',191],['51',151],['52',131],['53',191],['54',171],['55',191],['56',111],['57',191],['58',254],['59',91]
   ]

On Saturday of the week number 33 of August (month number 8) 2019 at 15h ('h15') :

  • At the minute 0, there were 650 views.

  • At the minute 1, there were 740 views.

  • At the minute 2, there were 520 views.

  • At the minute 3, there were 752 views.

  • ...

Get the statistics per second

   // Get the statistics per second on Saturday of the week number 33 in august 2019 at 15H49
   $statsPerSecond = $this->get('tchoulom.viewcounter.stats_finder')->getStatsPerSecond($article, 2019, 8, 33, 'Saturday', 'h15', 'm49');

Result:

   [
      ['00',60],['01',40],['02',21],['03',72],['04',70],['05',70],['06',50],['07',20],['08',80],['09',70],['10',70],['11',72],['12',68],['13',70],['14',20],['15',10],['16',15],['17',70],['18',70],['19',70],['20',10],['21',13],['22',70],['23',7],['24',11],['25',21],['26',11],['27',10],['28',110],['29',10],['30',14],['31',14],['32',18],['33',14],['34',14],['35',17],['36',14],['37',18],['38',14],['39',14],['40',19],['41',19],['42',19],['43',19],['44',19],['45',19],['46',18],['47',19],['48',19],['49',19],['50',19],['51',15],['52',13],['53',19],['54',17],['55',19],['56',11],['57',19],['58',25],['59',71]
   ]

On Saturday of the week number 33 of August (month number 8) 2019 at 15H49:

  • At the second 0, there were 60 views.

  • At the second 1, there were 40 views.

  • At the second 2, there were 21 views.

  • At the second 3, there were 72 views.

  • ...

The data in the stats file represents the view statistics, as shown in the following figure:

Statistical data in 2018

The figure above shows that the statistical data contain 2 "viewcountable" entities: article and news.

The statistical data of the entity article are recorded over 12 months.

Let's zoom in on the statistics for the first week of January:

the statistical data of the first week of January 2018

Search for geolocation data

  • Observation

The geolocation data of visitors to your web pages may look like the following figure:

the geolocation data

The figure above shows that visitors have viewed the web page (with the identifier 3) from 4 countries: United States, France, United Kingdom and Ireland.

Let's zoom in on the country "United States" in order to visualize the geolocation data it contains:

the geolocation data washington

There are 5 views from the United States including 4 in the town of Washington located in the District of Columbia region.

These 5 views are spread over 2 regions: District of Columbia and New York.

View date data can also be used:

the geolocation view date

  • Search:
Gets country stats
$countryStats = $this->statFinder->getCountryStats($article);

Result:

   [
      ["France", 6],["United States", 45],["Ireland", 8],["United Kingdom", 8]
   ]
  • There are 6 views in France.
  • There are 45 views in United States.
  • ...
Gets region stats
$regionStats = $this->statFinder->getRegionStats($article);

Result:

   [
      ["Île-de-France", 3],["Normandy", 3],["District of Columbia", 44],["New York", 1],["Leinster", 8],["England", 2]
   ]
  • There are 3 views in Île-de-France.
  • There are 3 views in Normandy.
  • There are 44 views in District of Columbia.
  • ...
Gets city stats
$cityStats = $this->statFinder->getCityStats($article);

Result:

   [
      ["Paris", 3],["Rouen", 3],["Washington", 44],["Buffalo", 1],["Dublin", 8],["London", 2]
   ]
  • There are 3 views in Paris.
  • There are 3 views in Rouen.
  • There are 44 views in Washington.
  • ...
Gets stats by country
$statsByCountry = $this->statFinder->getStatsByCountry($article, 'United States');

Result:

  45
  • There are 45 views in the "united states" country.
Gets stats by region
$statsByRegion = $this->statFinder->getStatsByRegion($article, 'United States', 'District of Columbia');

Result:

  44
  • There are 44 views in the "District of Columbia" region.
Gets stats by city
$statsByCity = $this->statFinder->getStatsByCity($article, 'United States', 'District of Columbia', 'Washington');

Result:

  44
  • There are 44 views in the "Washington" city.

The StatsFinder service provides other functions that you can browse.

Build a graph with "Google Charts"

You can now use these statistical data to build a graph, as shown in the following figure:

Statistics of monthly views in 2018

Monthly views in 2018

Also, you can build the graph on statistics of daily view, hourly view, weekly view and yearly view according to the data in the statistics file.

You can find out about "Google Charts" here

The StatsComputer service

Use the StatsComputer service to calculate the min value, max value, average value, range value, mode value, median value and the number of occurrences of the statistics :

First of all, get the stats computer service

   $statsComputer = $this->get('tchoulom.viewcounter.stats_computer');

The functions of the statsComputer service can take as argument the $yearlyStats, $monthlyStats, $weeklyStats, $daylyStats, $hourlyStats, $statsPerMinute, and $statsPerSecond

Calculates the min value

   // Get the min value of the yearly statistics
   $minValue = $statsComputer->computeMinValue($yearlyStats);

Result:

    [2017,47882376]

Calculates the max value

   // Get the max value of the monthly statistics
   $maxValue = $statsComputer->computeMaxValue($monthlyStats);

Result:

    [8,951224]

Calculates the average

The average is the sum of the values ​​of the statistical series divided by the number of values.

   // Get the average of the weekly statistics
   $average = $statsComputer->computeAverage($weeklyStats);

Result:

    265039

Calculates the range

The range is the difference between the highest number and the lowest number.

   // Get the range of the daily statistics
   $range = $statsComputer->computeRange($dailyStats);

Result:

    6

Calculates the mode

The mode is the number that is in the array the most times.

   // Get the mode of the hourly statistics
   $mode = $statsComputer->computeMode($hourlyStats);

Result:

    700

Calculates the median

The median is the middle value after the numbers are sorted smallest to largest.

   // Get the median of the statistics per minute
   $median = $statsComputer->computeMedian($statsPerMinute);

Result:

    75.5

Count the number of values ​​in the statistical series

   // Get the count of the statistics per second
   $count = $statsComputer->count($statsPerSecond);

Result:

    60

Tools

Command

Cleanup viewcounter data

You can delete the viewcounter data using the ViewcounterCleanupCommand command:

  • Delete all the viewcounter data from the database:
   php bin/console tchoulom:viewcounter:cleanup
  • Delete all the viewcounter data whose article was viewed at least 1 hour ago:
   php bin/console tchoulom:viewcounter:cleanup --min=1h
  • Delete all the viewcounter data whose article was viewed at most 1 day ago:
   php bin/console tchoulom:viewcounter:cleanup --max=1d
  • Delete all the viewcounter data whose article was viewed at least 3 years ago:
   php bin/console tchoulom:viewcounter:cleanup --min=3y
  • Delete all the viewcounter data whose article was viewed at most 5 months ago:
   php bin/console tchoulom:viewcounter:cleanup --max=5M
  • Examples of date interval:
's' => 'second'
'm' => 'minute'
'h' => 'hour'
'd' => 'day'
'w' => 'week'
'M' => 'month'
'y' => 'year'

Original Credits

Created by Ernest TCHOULOM for tchoulom.com.

License

This bundle is released under the MIT license. See the complete license in the bundle:

LICENSE

Enjoy!

Need help or found a bug? http://www.tchoulom.com