User:Dsimic/Traffic stats calculation

Automated monthly statistics calculation
Below is a rather simple PHP program that fetches monthly page views statistics provided by the Pageview API in JSON format (that's a public API developed and maintained by the Wikimedia Foundation, see also its detailed REST API documentation), for a specified list of articles, and calculates their total monthly views and average views per day. The fetched page views statistics don't include spider- or bot-generated traffic. The program is intended to be run interactively from a command-line interface (CLI); instead of running it locally, on a machine capable of executing PHP scripts, you may also use some of the freely available online PHP development environments.

Initially, this program used the page views statistics provided by http://stats.grok.se/ in JSON format, but that web service unfortunately became no longer updated around mid-January 2016, and it remains defunct. If needed, you can also have a look at that older version of the program code and documentation.

As pretty much everything else here on Wikipedia, I'm releasing this program code under the terms of the CC BY-SA 3.0 license, so please feel free to use it and modify according to your needs. Of course, feel free to use my talk page to in case you have any questions, suggestions, bug reports, etc.

Source code
Before running this program, you need to modify the list of articles contained in the  variable (what's in the code below is the list of articles I've created or started), and to modify the month and year for which statistics are to be fetched and calculated, which are specified through the   and   constants, respectively. When the program is configured to calculate statistics for the current month, it takes into account only the whole/elapsed days; as a result, running the program on the first day of the month to calculate current month statistics isn't supported. Also, in case whole days are missing in the statistics data available from the Pageview API, the program doesn't count in such zero-page-views days when calculating the averages. The  constant selects the encyclopedia:   is for the English Wikipedia,   is for the German Wikipedia, etc.

Just as a note, getting ready-to-run PHP code of this program is as easy as viewing the Wiki code of this page and copying what's between the  and  tags. The program code below is the latest available version, and it is updated on this page after any improvements or bugfixes are implemented.

Output example
Below is an example of the output produced when the program from above is run. The program sorts the articles by their total page views in descending order, so the article that has received the largest number of page views is first in the printed list. In the  line, dots  represent the progress updates during the processing of each article chunk, while the hash marks  represent the beginning of processing for each new article chunk. This chunking is necessary because the Pageview API imposes a rate limit on the API queries it receives, which,, is specified at 10 requests per second.