sierratecnologia / tracking
Tracking
Fund package maintenance!
ricasolucoes
ricasolucoes.com.br/open-source/support-us
Requires
- php: ^7.4
- donatj/phpuseragentparser: *
- geoip2/geoip2: ^2.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- illuminate/bus: ^7.0 || ^8.0
- illuminate/console: ^7.0 || ^8.0
- illuminate/contracts: ^7.0 || ^8.0
- illuminate/database: ^7.0 || ^8.0
- illuminate/http: ^7.0 || ^8.0
- illuminate/queue: ^7.0 || ^8.0
- illuminate/routing: ^7.0 || ^8.0
- illuminate/support: ^7.0 || ^8.0
- jenssegers/agent: ^2.0
- laravel/horizon: ^4.0|^5.0
- phpmetrics/phpmetrics: *
- ricardosierra/larametrics: ^1.2.4
- sierratecnologia/audit: ^0.4.0
- sierratecnologia/countries: ^0.4.0
- sierratecnologia/muleta: ^0.4.0
- spatie/laravel-analytics: ^3.0
- symfony/http-foundation: >=4.3
- torann/geoip: ^1.0|^3.0
- ua-parser/uap-php: ^3.8
- wnx/laravel-stats: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^9.3
- psalm/plugin-laravel: ^1.2
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2024-11-20 17:30:41 UTC
README
SierraTecnologia Tracking
SierraTecnologia Tracking is a lightweight, yet detailed package for tracking and recording user visits across your Laravel application. With only one simple query per request, important data is being stored, and later a cronjob crush numbers to extract meaningful stories from within the haystack.
Unlike other tracking packages that seriously damage your project's performance (yes, I mean that package you know 😅), our package takes a different approach by just executing only one query at the end of each request after the response is being served to the user, through the terminate
method of an automatically attached middleware, and then later on it uses the raw data previously inserted in the database to extract meaningfull numbers. This is done based on a random lottery request, or through a scheduled job (recommended) that could be queued to offload the heavy crunching work.
SierraTecnologia Tracking tracks each -valid- request, meaning only requests that goes through routing pipeline, which also means that any wrong URL that results in NotFoundHttpException
will not be tracked. If requested page has uncaught exceptions, it won't be tracked as well. It track user's logged in account (if any), session of all users and guests (if any), device (family, model, brand), platform (family, version), browser (agent, kind, family, version), path, route (action, middleware, parameters), host, protocol, ip address, language, status codes, and many more, and still we've plenty of awesome features planned for the future.
With such a huge collected data, the trackings_requests
database table will noticeably increase in size specially if you've a lot of visits, that's why it's recommended to clean it periodically. Other important data will stay still in their respective tables, normalized and without any performance issues, so only this table need to be cleaned. By default that will be done automatically every month.
The default implementation of SierraTecnologia Tracking comes with zero configuration out-of-the-box, which means it just works once installed. But it's recommended to change the defaults and disable the "Tracking Crunching Lottery" from config file, and replace it with a Scheduled Tasks for even better performance if you've large number of visits. See Usage for details.
Installation
-
Install the package via composer:
composer require sierratecnologia/tracking
-
Publish resources (migrations and config files):
php artisan sierratecnologia:publish:trackings
-
Execute migrations via the following command:
php artisan sierratecnologia:migrate:trackings
-
Done!
Usage
Well, this is the fun part! SierraTecnologia Tracking has no usage instructions, because it just works! You install it and you are done! Seriously!!
Anyway, as a recommended performance tweak go ahead and do the following (optionally):
-
Publish config file via the following command:
php artisan sierratecnologia:publish:trackings
-
Disable the "Tracking Crunching Lottery" from config file.
-
Follow the default Laravel documentation about Scheduled Tasks, then schedule both
\Tracking\Jobs\CrunchTracking
and\Tracking\Jobs\CleanTrackingRequests
jobs at whatever intervals you see appropriate. -
Enjoy!
Note: SierraTecnologia Tracking has a
\Tracking\Http\Middleware\TrackTracking
middleware that attach itself automatically to theweb
middleware group, that's how it works out-of-the-box with zero configuration.
Data retrieval
You may need to build your own frontend interface to browse trackings, and for that you can utilize any of the included eloquent models as you normally do with Laravel Eloquent.
All eloquent models are self explainatory:
\Tracking\Models\Tracking\Agent
browser agent model\Tracking\Models\Tracking\Datum
raw trackings data (to be crunched)\Tracking\Models\Tracking\Device
user device model\Tracking\Models\Tracking\Path
request path model\Tracking\Models\Tracking\Platform
user platform model\Tracking\Models\Tracking\Request
request data model (to be cleaned periodically)\Tracking\Models\Tracking\Route
request route details model
All models are bound to the Service Container so you can swap easily from anywhere in your application. In addition to the default normal way of using these models explicitely, you can use their respective service names as in the following example:
// Find first browser agent (any of these methods are valid and equivalent) app('tracking.trackings.agent')->first(); new \Tracking\Models\Tracking\Agent::first(); app(\Tracking\Contracts\AgentContract::class)->first();
Same for all other eloquent models.
Counts that matters
All agent, device, path, platform, route models have a count
attribute, which gets updated automatically whenever a new request has been tracked.
This count
attribute reflects number of hits. To make it clear let's explain through data samples:
Agents
This means there's 734 visit to our project through Chrome browser, version 63.0.3239, with agent (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36)
Devices
This means there's 83 visits to our project through iPhone device.
Platforms
This means there's 615 visits to our project through Mac OS X operating system, with version 10.12.6.
Paths
This means there's 12 visits to the admin dashboard roles management of the test.homestead.local host (in case you have multiple hosts or wildcard subdomains enabled on the same project, you can track all of them correctly here). The english interface was used, and the accessed route had two parameters, one for locale (english in this case), and updated role record (admin in this case).
This table could be used as a visit counter for all your pages. To retrieve and display page views you can use the following code for example:
$pageViews = app('tracking.trackings.path')->where('path', request()->decodedPath())->first()->count;
And simply use the $pageViews
variable anywhere in your views or controllers, or anywhere else. That way you have automatic visit counter for all your project's pages, very useful and performant, ready at your fingertips. You can add host
contraint in case you have wildcard subdomains enabled.
Routes
This means there's 41 visits to the adminarea.roles.edit
route, which has the {locale}/adminarea/roles/{role}
raw path, and served through the App\Http\Controllers\Adminarea\RolesController@form
controller action, and has the following middleware applied ["web","nohttpcache","can:access-adminarea","auth","can:update-roles,roles"]
, knowing the route accepts two parameters with the following regex requirements {"role": "[a-z0-9-]+", "locale": "[a-z]{2}"}
.
As you can see, this trackings_routes
table beside the trackings_paths
table are both complimentary, and could be used together to track which paths and routs are being accessed, how many times, and what controller actions serve it, and what parameters are required, with the actual parameter replacements used to access it. Think of routes as your raw links blueprint map, and of paths as the executed and actually used links by users.
Geoips
This means there's 57 visits to the project from IP address 127.0.0.0
with the latitude, longitude and timezone mentioned above coming from New Haven
city, Connecticut
state.
Requests
This is the most comprehensive table that records every single request made to the project, with access details as seen in the sample above. Through session_id
, user_id
and user_type
you can track guests (logged out) and users (logged in) and extract unique visits/visitors with the criteria you see appropriate for you.
Notes:
- As a final note, this package is a data hord, and it doesn't actually do much of the math that could be done on such a valuable gathered data, so it's up to your imagination to utilize it however you see fits your goals. Implementation details is up to you.
- We didn't explain the
trackings_data
table since it's used for temporary raw data storage until it's being crunched and processed by the package, so you should NOT care or mess with that table. It's used internally by the package and has no real end-user usage.- The
\Tracking\Models\Tracking\Request
model has relationships to all related data such asagent
,device
,path
,platform
, androute
. So once you grab a request instance you can access any of it's relationships as you normaly do with Eloquent Relationships like so:$trackingsRequest->agent->version
or$trackingsRequest->platform->family
.
Changelog
Refer to the Changelog for a full history of the project.
Support
The following support channels are available at your fingertips:
Contributing & Protocols
Thank you for considering contributing to this project! The contribution guide can be found in CONTRIBUTING.md.
Bug reports, feature requests, and pull requests are very welcome.
Security Vulnerabilities
If you discover a security vulnerability within this project, please send an e-mail to help@sierratecnologia.com. All security vulnerabilities will be promptly impressioned.
About SierraTecnologia
SierraTecnologia is a software solutions startup, specialized in integrated enterprise solutions for SMEs established in Alexandria, Egypt since June 2016. We believe that our drive The Value, The Reach, and The Impact is what differentiates us and unleash the endless possibilities of our philosophy through the power of software. We like to call it Innovation At The Speed Of Life. That’s how we do our share of advancing humanity.
License
This software is released under The MIT License (MIT).
(c) 2016-2020 SierraTecnologia LLC, Some rights reserved.