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;
class ArticleListener extends StatsListener
{
// Define your events here
public function implementedEvents(): array
{
return [
'App.Article.hit' => 'onHit',
];
}
public function onHit(Event $event, int $articleId, int $count = 1): bool
{
// track if any articles were viewed
// Article.hits is a StatsObject key.
parent::recordCount($event, 'Articles.hits'); // leave out count to just increment by one.
// track the specific article
// Article.hits.[id] is a seperate StatsObject key from above.
parent::recordCount($event, 'Articles.hits.' . $articleId, $count);
}
}
declare(strict_types=1);
namespace Fr3nch13\Blog;
use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventManager;
use Fr3nch13\Stats\Event\TestListener;
class BlogPlugin extends BasePlugin
{
// other code
public function bootstrap(PluginApplicationInterface $app): void
{
// Register your listener with the Event Manager
EventManager::instance()->on(new ArticleListener());
parent::bootstrap($app);
}
/// other code
}
declare(strict_types=1);
namespace Fr3nch13\Blog\Controller;
use Cake\Event\Event;
use Fr3nch13\Blog\AppController;
class ArticlesController extends AppController
{
/**
* Example of how to register a hit
*/
public function view(int $id): ?Response
{
$article = $this->Articles->get($id);
// do this reight before rendering the view incase your code above throws an error,
// or redirects somewhere else.
$this->getEventManager()->dispatch(new Event('App.Article.hit', $this, [
'articleId' => $id,
'count' => 1,
]));
}
}
declare(strict_types=1);
namespace Fr3nch13\Blog\Admin\Controller;
use Fr3nch13\Stats\Controller\ChartJsTrait;
use Fr3nch13\Blog\Admin\AppController;
class ArticlesController extends AppController
{
/**
* Used to do the common tasks for chartjs graphs.
*/
use ChartJsTrait;
// other code
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);
}
// other code
/**
* 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 = $this->StatsCounts->getObjectStats('Articles.hits');
/*
$stats will look like:
$stats = [
'year' => 12001, <-- counts
'month' => 3001,
'week' => 701,
'day' => 101,
'hour' => 11,
];
*/
$this->set(compact('stats'));
$this->viewBuilder()->setOption('serialize', ['stats']);
return null;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.