Skip to content

Commit

Permalink
Merge pull request #17 from tattersoftware/new-settings
Browse files Browse the repository at this point in the history
Add "trackAllPages" and "excludeUris" settings
  • Loading branch information
MGatner authored Jul 4, 2022
2 parents 07f1e13 + 571cd21 commit 0fa8bcc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,28 +20,47 @@ 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

If installed correctly CodeIgniter 4 will detect and autoload the class, service, and
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

Expand Down
14 changes: 14 additions & 0 deletions examples/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 = [];
}
9 changes: 7 additions & 2 deletions src/Config/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', static function () {
$config = config('Visits');
// Ignore CLI requests
if (! is_cli() && $config->trackAllPages) {
Services::visits()->record();
}
});
14 changes: 14 additions & 0 deletions src/Config/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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
*
Expand Down Expand Up @@ -40,4 +46,12 @@ class Visits extends BaseConfig
* @var bool
*/
public $ignoreAjax = true;

/**
* URIs to exclude from tracking.
* Accepts regex values.
*
* @var string[]
*/
public array $excludeUris = [];
}
14 changes: 10 additions & 4 deletions src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -78,11 +77,18 @@ 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();
// Check if URI has been whitelisted from Visit check
foreach ($this->config->excludeUris as $excluded) {
if (url_is($excluded)) {
return $this;
}
}

$visits = model(VisitModel::class);
$visit = new Visit();

// start the object with parsed URL components (https://secure.php.net/manual/en/function.parse-url.php)
Expand Down

0 comments on commit 0fa8bcc

Please sign in to comment.