From 3865600d6225d49d4a0d2b72205109fe977183a8 Mon Sep 17 00:00:00 2001 From: kleber Date: Sat, 2 Jul 2022 23:48:20 -0300 Subject: [PATCH 1/5] Add "trackAllPages" and "excludeUris" settings The first allows activating or not the automatic tracking through the 'post_controller_constructor' event and the second excluding certain uri's from tracking. --- src/Config/Events.php | 9 +++++++-- src/Config/Visits.php | 6 ++++++ src/Visits.php | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Config/Events.php b/src/Config/Events.php index ea5cae7..40adb24 100644 --- a/src/Config/Events.php +++ b/src/Config/Events.php @@ -4,5 +4,10 @@ use CodeIgniter\Events\Events; -Events::on('post_controller_constructor', static fn () => // Ignore CLI requests -is_cli() ?: service('visits')->record()); +Events::on('post_controller_constructor', function () { + $config = config('Visits'); + // Ignore CLI requests + if (!is_cli() and $config->trackAllPages) { + Services::visits()->record(); + } +}); diff --git a/src/Config/Visits.php b/src/Config/Visits.php index 7209d36..abfe3ef 100644 --- a/src/Config/Visits.php +++ b/src/Config/Visits.php @@ -6,6 +6,9 @@ class Visits extends BaseConfig { + // Enable tracking in all controllers using the post_controller_constructor event + public $trackAllPages = true; + /** * Metric for tracking a unique visitor * @@ -40,4 +43,7 @@ class Visits extends BaseConfig * @var bool */ public $ignoreAjax = true; + + // Exclude uris from tracking. Accept regex values. + public $excludeUris = array(); } diff --git a/src/Visits.php b/src/Visits.php index 099d028..b007d1d 100644 --- a/src/Visits.php +++ b/src/Visits.php @@ -88,6 +88,14 @@ public function record() // start the object with parsed URL components (https://secure.php.net/manual/en/function.parse-url.php) $visit->fill(parse_url(current_url())); + // Check if URI has been whitelisted from Visit check + if ($exclude_uris = $this->config->excludeUris) { + foreach ($exclude_uris as $excluded) { + if (url_is($excluded)) { + return $this; + } + } + } // add session/server specifics $visit->session_id = $this->session->session_id; $visit->user_id = $this->session->{$this->config->userSource} ?? null; // @phpstan-ignore-line From 70966b3d87fb6506a9754727ac6190004cb081f8 Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 4 Jul 2022 13:48:12 +0000 Subject: [PATCH 2/5] Supply missing docblocks --- examples/Visits.php | 14 ++++++++++++++ src/Config/Visits.php | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/examples/Visits.php b/examples/Visits.php index 373c092..2aae6f4 100644 --- a/examples/Visits.php +++ b/examples/Visits.php @@ -19,6 +19,12 @@ class Visits extends BaseConfig { + /** + * Whether to enable tracking in all controllers using + * the post_controller_constructor event. + */ + public bool $trackAllPages = true; + /** * Metric for tracking a unique visitor * @@ -53,4 +59,12 @@ class Visits extends BaseConfig * @var bool */ public $ignoreAjax = true; + + /** + * URIs to exclude from tracking. + * Accepts regex values. + * + * @var string[] + */ + public array $excludeUris = []; } diff --git a/src/Config/Visits.php b/src/Config/Visits.php index abfe3ef..dd4f5d1 100644 --- a/src/Config/Visits.php +++ b/src/Config/Visits.php @@ -6,8 +6,11 @@ class Visits extends BaseConfig { - // Enable tracking in all controllers using the post_controller_constructor event - public $trackAllPages = true; + /** + * Whether to enable tracking in all controllers using + * the post_controller_constructor event. + */ + public bool $trackAllPages = true; /** * Metric for tracking a unique visitor @@ -44,6 +47,11 @@ class Visits extends BaseConfig */ public $ignoreAjax = true; - // Exclude uris from tracking. Accept regex values. - public $excludeUris = array(); + /** + * URIs to exclude from tracking. + * Accepts regex values. + * + * @var string[] + */ + public array $excludeUris = []; } From e119ceb37385ed434070b21e456f99f476bf8dde Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 4 Jul 2022 13:52:02 +0000 Subject: [PATCH 3/5] Apply Rector --- src/Config/Events.php | 4 ++-- src/Visits.php | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Config/Events.php b/src/Config/Events.php index 40adb24..45cf3f6 100644 --- a/src/Config/Events.php +++ b/src/Config/Events.php @@ -4,10 +4,10 @@ use CodeIgniter\Events\Events; -Events::on('post_controller_constructor', function () { +Events::on('post_controller_constructor', static function () { $config = config('Visits'); // Ignore CLI requests - if (!is_cli() and $config->trackAllPages) { + if (! is_cli() && $config->trackAllPages) { Services::visits()->record(); } }); diff --git a/src/Visits.php b/src/Visits.php index b007d1d..c4748f9 100644 --- a/src/Visits.php +++ b/src/Visits.php @@ -89,13 +89,12 @@ public function record() $visit->fill(parse_url(current_url())); // Check if URI has been whitelisted from Visit check - if ($exclude_uris = $this->config->excludeUris) { - foreach ($exclude_uris as $excluded) { - if (url_is($excluded)) { - return $this; - } + foreach ($this->config->excludeUris as $excluded) { + if (url_is($excluded)) { + return $this; } } + // add session/server specifics $visit->session_id = $this->session->session_id; $visit->user_id = $this->session->{$this->config->userSource} ?? null; // @phpstan-ignore-line From 1c0842abca6184062d34e60992781e3406fa3c27 Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 4 Jul 2022 13:54:00 +0000 Subject: [PATCH 4/5] Reorganize --- src/Visits.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Visits.php b/src/Visits.php index c4748f9..cc1bc2b 100644 --- a/src/Visits.php +++ b/src/Visits.php @@ -2,7 +2,6 @@ namespace Tatter\Visits; -use CodeIgniter\Config\Services; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Session\Session; use Tatter\Visits\Config\Visits as VisitsConfig; @@ -45,7 +44,7 @@ public function __construct(VisitsConfig $config, $db = null) $this->config = $config; // initiate the Session library - $this->session = Services::session(); + $this->session = service('session'); // If no db connection passed in, use the default database group. $this->db = db_connect($db); @@ -78,16 +77,10 @@ public function record() } // Check for ignored AJAX requests - if (Services::request()->isAJAX() && $this->config->ignoreAjax) { + if (service('request')->isAJAX() && $this->config->ignoreAjax) { return; } - $visits = new VisitModel(); - $visit = new Visit(); - - // start the object with parsed URL components (https://secure.php.net/manual/en/function.parse-url.php) - $visit->fill(parse_url(current_url())); - // Check if URI has been whitelisted from Visit check foreach ($this->config->excludeUris as $excluded) { if (url_is($excluded)) { @@ -95,6 +88,12 @@ public function record() } } + $visits = model(VisitModel::class); + $visit = new Visit(); + + // start the object with parsed URL components (https://secure.php.net/manual/en/function.parse-url.php) + $visit->fill(parse_url(current_url())); + // add session/server specifics $visit->session_id = $this->session->session_id; $visit->user_id = $this->session->{$this->config->userSource} ?? null; // @phpstan-ignore-line From 571cd2163c071f8ce60304a8497acf29296a8395 Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 4 Jul 2022 13:59:42 +0000 Subject: [PATCH 5/5] Add basic docs --- README.md | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7a2616e..ee4133b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Tatter\Visits Lightweight traffic tracking for CodeIgniter 4 -[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions?query=workflow%3A%22PHPUnit) -[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions?query=workflow%3A%22PHPStan) +[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/phpunit.yml) +[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/phpstan.yml) +[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/deptrac.yml) [![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-visits/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-visits?branch=develop) ## Quick Start @@ -19,20 +20,24 @@ Provides automated traffic tracking for CodeIgniter 4 Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date: -* `> composer require tatter/visits` +```shell +> composer require tatter/visits +``` Or, install manually by downloading the source files and adding the directory to -`app/Config/Autoload.php`. +**app/Config/Autoload.php**. Once the files are downloaded and included in the autoload, run any library migrations to ensure the database is setup correctly: -* `> php spark migrate -all` +```shell +> php spark migrate --all +``` ## Configuration (optional) The library's default behavior can be altered by extending its config file. Copy **examples/Visits.php** to **app/Config/** and follow the instructions in the -comments. If no config file is found in app/Config the library will use its own. +comments. If no config file is found in **app/Config/** the library will use its own. ## Usage @@ -40,7 +45,22 @@ If installed correctly CodeIgniter 4 will detect and autoload the class, service config. The library includes an event listening for `post_controller_constructor` to record page loads. If you prefer to handle them manually you may use the service to load the class and record the current visit: -* `service('visits')->record();` +```php +service('visits')->record(); +``` + +When manually tracking be sure to disable automated tracking in your Config file: +```php +class Visits extends BaseConfig +{ + /** + * Whether to enable tracking in all controllers using + * the post_controller_constructor event. + */ + public bool $trackAllPages = true; +``` + +You may also limit which routes are tracked by adding them to the `$excludeUris` property. ## Accessing data