Lightweight traffic tracking for CodeIgniter 4
- Install with Composer:
> composer require tatter/visits
- Update the database:
> php spark migrate -all
- Visits will be recorded automatically by a system event
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
Or, install manually by downloading the source files and adding the directory to 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
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.
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();
When manually tracking be sure to disable automated tracking in your Config file:
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.
This library provides a VisitModel
and a Visit
entity for convenient access to recorded
entries. Feel free to extend these classes for any additional functionality.
It is not legal nor advisable to track user traffic in all cases. By default Visits
will
only include user IDs when they are loaded into the specific session variable
logged_in
. If this session variable is not set access will be anonymous. You can
change the variable used to determine a logged in user by using the Config file and
altering the value (see above).