Download the PHP package krowek/view-counter-bundle without Composer
On this page you can find all versions of the php package krowek/view-counter-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krowek/view-counter-bundle
More information about krowek/view-counter-bundle
Files in krowek/view-counter-bundle
Informations about the package view-counter-bundle
The View Counter Bundle
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.

Table of contents
- Features include
- Documentation
- Installation
- Step 1: Download TchoulomViewCounterBundle using composer
- Step 2: Enable the Bundle
- Usage
- Step 1: Interface and Property
- Step 2: ViewCounter
- Step 3: Configuration
- The "view_counter"
- The "statistics"
- Step 4: The Controller
- Method 1
- Method 2
- Step 5: The View
- Step 6: The Geolocation
- Step 7: Exploitation of statistical data
- The StatsFinder service
- Get the yearly statistics
- Get the monthly statistics
- Get the weekly statistics
- Get the daily statistics
- Get the hourly statistics
- Get the statistics per minute
- Get the statistics per second
- Search for geolocation data
- Build a graph with "Google Charts"
- The StatsComputer service
- Calculates the min value
- Calculates the max value
- Calculates the average
- Calculates the range
- Calculates the mode
- Calculates the median
- Count the number of values in the statistical series
- Tools
- Command
- Cleanup viewcounter data
- Original Credits
- License
Features include
- Viewcounter
- Statistics
- Geolocation
Documentation
Installation
Step 1: Download TchoulomViewCounterBundle using composer
You can install it via Composer:
or
or
Check that it is recorded in the composer.json file
Step 2: Enable the Bundle
Edit the appKernel.php file
Usage
Step 1: Interface and Property
Suppose that you have an Article Entity.
This Entity must implement the ViewCountable interface:
Add the $views property and the target Entity ViewCounter.
The $views property allows to get the number of views:
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:
Update the doctrine relationship between the ViewCounter Entity and your Article Entity:
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:
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:
or
if your service is declared as such:
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:
Step 4: The Controller
2 methods are available:
Method 1
Method 2
You only need to save your Article Entity via the 'tchoulom.viewcounter' service:
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:
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
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:
The StatsFinder service
Use the StatsFinder service to get statistics of a web page :
You can also get statistical data of a web page by year, month, week, day, hour, minute and second
Get the yearly statistics
Result:
In 2019, there were 98537215 views.
In 2018, there were 95548144 views.
In 2017, there were 47882376 views.
Get the monthly statistics
Result:
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
Result:
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
Result:
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
Result:
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
Result:
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
Result:
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:

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:

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

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:

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:

-
Search:
Gets country stats
Result:
- There are 6 views in France.
- There are 45 views in United States.
- ...
Gets region stats
Result:
- 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
Result:
- There are 3 views in Paris.
- There are 3 views in Rouen.
- There are 44 views in Washington.
- ...
Gets stats by country
Result:
- There are 45 views in the "united states" country.
Gets stats by region
Result:
- There are 44 views in the "District of Columbia" region.
Gets stats by city
Result:
- 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

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
The functions of the statsComputer service can take as argument the $yearlyStats, $monthlyStats, $weeklyStats, $daylyStats, $hourlyStats, $statsPerMinute, and $statsPerSecond
Calculates the min value
Result:
Calculates the max value
Result:
Calculates the average
The average is the sum of the values of the statistical series divided by the number of values.
Result:
Calculates the range
The range is the difference between the highest number and the lowest number.
Result:
Calculates the mode
The mode is the number that is in the array the most times.
Result:
Calculates the median
The median is the middle value after the numbers are sorted smallest to largest.
Result:
Count the number of values in the statistical series
Result:
Tools
Command
Cleanup viewcounter data
You can delete the viewcounter data using the ViewcounterCleanupCommand command:
-
Delete all the viewcounter data from the database:
-
Delete all the viewcounter data whose article was viewed at least 1 hour ago:
-
Delete all the viewcounter data whose article was viewed at most 1 day ago:
-
Delete all the viewcounter data whose article was viewed at least 3 years ago:
-
Delete all the viewcounter data whose article was viewed at most 5 months ago:
- Examples of date interval:
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:
Enjoy!
Need help or found a bug? http://www.tchoulom.com