Skip to content

Commit 0fa8bcc

Browse files
authored
Merge pull request #17 from tattersoftware/new-settings
Add "trackAllPages" and "excludeUris" settings
2 parents 07f1e13 + 571cd21 commit 0fa8bcc

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Tatter\Visits
22
Lightweight traffic tracking for CodeIgniter 4
33

4-
[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions?query=workflow%3A%22PHPUnit)
5-
[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions?query=workflow%3A%22PHPStan)
4+
[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/phpunit.yml)
5+
[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/phpstan.yml)
6+
[![](https://github.com/tattersoftware/codeigniter4-visits/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-visits/actions/workflows/deptrac.yml)
67
[![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-visits/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-visits?branch=develop)
78

89
## Quick Start
@@ -19,28 +20,47 @@ Provides automated traffic tracking for CodeIgniter 4
1920

2021
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
2122
and always be up-to-date:
22-
* `> composer require tatter/visits`
23+
```shell
24+
> composer require tatter/visits
25+
```
2326

2427
Or, install manually by downloading the source files and adding the directory to
25-
`app/Config/Autoload.php`.
28+
**app/Config/Autoload.php**.
2629

2730
Once the files are downloaded and included in the autoload, run any library migrations
2831
to ensure the database is setup correctly:
29-
* `> php spark migrate -all`
32+
```shell
33+
> php spark migrate --all
34+
```
3035

3136
## Configuration (optional)
3237

3338
The library's default behavior can be altered by extending its config file. Copy
3439
**examples/Visits.php** to **app/Config/** and follow the instructions in the
35-
comments. If no config file is found in app/Config the library will use its own.
40+
comments. If no config file is found in **app/Config/** the library will use its own.
3641

3742
## Usage
3843

3944
If installed correctly CodeIgniter 4 will detect and autoload the class, service, and
4045
config. The library includes an event listening for `post_controller_constructor` to
4146
record page loads. If you prefer to handle them manually you may use the service to load
4247
the class and record the current visit:
43-
* `service('visits')->record();`
48+
```php
49+
service('visits')->record();
50+
```
51+
52+
When manually tracking be sure to disable automated tracking in your Config file:
53+
```php
54+
class Visits extends BaseConfig
55+
{
56+
/**
57+
* Whether to enable tracking in all controllers using
58+
* the post_controller_constructor event.
59+
*/
60+
public bool $trackAllPages = true;
61+
```
62+
63+
You may also limit which routes are tracked by adding them to the `$excludeUris` property.
4464

4565
## Accessing data
4666

examples/Visits.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
class Visits extends BaseConfig
2121
{
22+
/**
23+
* Whether to enable tracking in all controllers using
24+
* the post_controller_constructor event.
25+
*/
26+
public bool $trackAllPages = true;
27+
2228
/**
2329
* Metric for tracking a unique visitor
2430
*
@@ -53,4 +59,12 @@ class Visits extends BaseConfig
5359
* @var bool
5460
*/
5561
public $ignoreAjax = true;
62+
63+
/**
64+
* URIs to exclude from tracking.
65+
* Accepts regex values.
66+
*
67+
* @var string[]
68+
*/
69+
public array $excludeUris = [];
5670
}

src/Config/Events.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44

55
use CodeIgniter\Events\Events;
66

7-
Events::on('post_controller_constructor', static fn () => // Ignore CLI requests
8-
is_cli() ?: service('visits')->record());
7+
Events::on('post_controller_constructor', static function () {
8+
$config = config('Visits');
9+
// Ignore CLI requests
10+
if (! is_cli() && $config->trackAllPages) {
11+
Services::visits()->record();
12+
}
13+
});

src/Config/Visits.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
class Visits extends BaseConfig
88
{
9+
/**
10+
* Whether to enable tracking in all controllers using
11+
* the post_controller_constructor event.
12+
*/
13+
public bool $trackAllPages = true;
14+
915
/**
1016
* Metric for tracking a unique visitor
1117
*
@@ -40,4 +46,12 @@ class Visits extends BaseConfig
4046
* @var bool
4147
*/
4248
public $ignoreAjax = true;
49+
50+
/**
51+
* URIs to exclude from tracking.
52+
* Accepts regex values.
53+
*
54+
* @var string[]
55+
*/
56+
public array $excludeUris = [];
4357
}

src/Visits.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tatter\Visits;
44

5-
use CodeIgniter\Config\Services;
65
use CodeIgniter\Database\ConnectionInterface;
76
use CodeIgniter\Session\Session;
87
use Tatter\Visits\Config\Visits as VisitsConfig;
@@ -45,7 +44,7 @@ public function __construct(VisitsConfig $config, $db = null)
4544
$this->config = $config;
4645

4746
// initiate the Session library
48-
$this->session = Services::session();
47+
$this->session = service('session');
4948

5049
// If no db connection passed in, use the default database group.
5150
$this->db = db_connect($db);
@@ -78,11 +77,18 @@ public function record()
7877
}
7978

8079
// Check for ignored AJAX requests
81-
if (Services::request()->isAJAX() && $this->config->ignoreAjax) {
80+
if (service('request')->isAJAX() && $this->config->ignoreAjax) {
8281
return;
8382
}
8483

85-
$visits = new VisitModel();
84+
// Check if URI has been whitelisted from Visit check
85+
foreach ($this->config->excludeUris as $excluded) {
86+
if (url_is($excluded)) {
87+
return $this;
88+
}
89+
}
90+
91+
$visits = model(VisitModel::class);
8692
$visit = new Visit();
8793

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

0 commit comments

Comments
 (0)