PHP code example of fr3nch13 / cakephp-stats

1. Go to this page and download the library: Download fr3nch13/cakephp-stats library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fr3nch13 / cakephp-stats example snippets



declare(strict_types=1);

namespace App\Event;

use Cake\Event\Event;
use Fr3nch13\Stats\Event\StatsListener;

class ArticleListener extends StatsListener
{
    public function implementedEvents(): array
    {
        return [
            'App.Article.hit' => 'onHit',
        ];
    }

    public function onHit(Event $event, int $articleId, int $count = 1): void
    {
        // Track all article views.
        parent::recordCount($event, 'Articles.hits', $count);

        // Track views for this specific article.
        parent::recordCount($event, 'Articles.hits.' . $articleId, $count);
    }
}


declare(strict_types=1);

namespace App;

use App\Event\ArticleListener;
use Cake\Event\EventManager;
use Cake\Http\BaseApplication;

class Application extends BaseApplication
{
    public function bootstrap(): void
    {
        parent::bootstrap();

        EventManager::instance()->on(new ArticleListener());
    }
}


declare(strict_types=1);

namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Http\Response;

class ArticlesController extends AppController
{
    /**
     * Example of how to register a hit
     */
    public function view(int $id): ?Response
    {
        $article = $this->Articles->get($id);

        // Dispatch this immediately before rendering so an earlier error or redirect does not record a view.
        $this->getEventManager()->dispatch(new Event('App.Article.hit', $this, [
            'articleId' => $id,
            'count' => 1,
        ]));

        return null;
    }
}


declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AppController;
use Cake\Http\Response;
use Fr3nch13\Stats\Controller\ChartJsTrait;

class ArticlesController extends AppController
{
    use ChartJsTrait;

    public function line(?int $range = null, ?string $timeperiod = null): ?Response
    {
        $keys = [
            'Articles.hits',
            'Articles.hits.1',
            'Articles.hits.2',
            'Articles.hits.3',
        ];

        return $this->chartJsLine($keys, $range, $timeperiod);
    }
}


declare(strict_types=1);

namespace App\Controller;

use App\Controller\AppController;
use Cake\Http\Response;

class ArticlesController extends AppController
{

    /**
     * To get the stats in a dashboard
     *
     * @return ?\Cake\Http\Response Renders view
     */
    public function dashboard(): ?Response
    {
        /** @var \Fr3nch13\Stats\Model\Table\StatsCountsTable $StatsCounts */
        $StatsCounts = $this->getTableLocator()->get('Fr3nch13/Stats.StatsCounts');

        $stats = $StatsCounts->getObjectStats('Articles.hits');

        $this->set(compact('stats'));
        $this->viewBuilder()->setOption('serialize', ['stats']);

        return null;
    }
}