mosamy / tracking
Tracking package for Laravel applications
Requires
- php: >=8.2
This package is not auto-updated.
Last update: 2026-04-22 00:42:58 UTC
README
A Laravel package to collect request and visitor geo/device analytics and store them in a trackings table.
What This Package Does
- Tracks each request (path, query params, IP, browser, OS, device).
- Enriches request data using an external IP geo provider.
- Stores full provider response for debugging and analytics.
- Links tracking rows to authenticated users through a polymorphic relation.
- Exposes helper methods through a facade:
- Tracker::track()
- Tracker::browser()
- Tracker::os()
- Tracker::device()
Supported Drivers
- geoplugin
- ipgeo
- ipwhois
Each driver returns a TrackingResponse object that is mapped into database columns.
Requirements
- PHP 8.2+
- Laravel application
Installation
Require the package.
composer require mosamy/tracking
Run migrations.
php artisan migrate
Configure environment variables in your application .env.
TRACKING_ENABLED=true TRACKING_DRIVER=geoplugin TRACKING_IP=
IPGEO_API_KEY= IPWHOIS_API_KEY=
Configuration
Config file path inside package:
- config/tracking.php
Config keys:
- enabled
- Boolean flag for enabling/disabling tracking.
- driver
- Active driver key, one of: geoplugin, ipgeo, ipwhois.
- ip
- Optional forced IP for testing. If set, middleware overrides REMOTE_ADDR.
- drivers
- Driver classes and API keys.
Driver config example:
- geoplugin
- class: Mosamy\Tracking\Trackers\Geoplugin
- ipgeo
- class: Mosamy\Tracking\Trackers\Ipgeo
- api_key: from IPGeolocation
- ipwhois
- class: Mosamy\Tracking\Trackers\Ipwhois
- api_key: from ipwhois.pro
Enable Request Tracking
Apply middleware to routes you want to track.
Laravel 11+ (bootstrap/app.php)
->withMiddleware(function ($middleware) {
$middleware->append(\Mosamy\Tracking\Middleware\TrackingMiddleware::class);
})
Laravel 10 and below (app/Http/Kernel.php)
- Add to web middleware group, api group, or global middleware based on your need.
How Tracking Works
- TrackingMiddleware runs for incoming request.
- If TRACKING_IP is set, request REMOTE_ADDR is overridden.
- Middleware calls Tracker::track().
- TrackerManager resolves configured driver from config.
- Driver fetches geo data and builds TrackingResponse.
- TrackerManager stores data using Mosamy\Tracking\Models\Tracking.
- If user is authenticated, model boot hook associates user morph relation.
Manual Usage
You can call tracking manually in your own code.
use Mosamy\Tracking\Facades\Tracker;
Tracker::track();
You can also detect user agent details:
Tracker::browser(); Tracker::os(); Tracker::device();
Database Schema
Migration creates table: trackings
Columns:
- id
- path
- params (json)
- elapsed_time
- ip
- continent
- continent_code
- country
- country_code
- city
- city_code
- latitude
- longitude
- timezone
- currency_code
- currency_symbol
- browser
- os
- device
- response (json)
- meta (json)
- user_type
- user_id
- created_at
- updated_at
Tracking Model
Model: Mosamy\Tracking\Models\Tracking
Features:
- Casts params, response, meta to arrays.
- Morph relation:
- user()
- Query scopes:
- forPage(page)
- fromTo(from, to)
Examples:
use Mosamy\Tracking\Models\Tracking;
Tracking::query()->forPage('products')->get(); Tracking::query()->fromTo('2026-04-01', '2026-04-07')->get();
Drivers Behavior
Geoplugin:
- Calls http://www.geoplugin.net/json.gp with request IP.
- Maps geoplugin_* fields into normalized columns.
Ipgeo:
- Calls https://api.ipgeolocation.io/ipgeo with apiKey.
- Maps continent, country, city, timezone, currency info.
Ipwhois:
- Calls http://ipwhois.pro/{ip} with key.
- Maps continent, country, city, timezone, currency info.
Response DTO
DTO: Mosamy\Tracking\DTO\TrackingResponse
Contains normalized camelCase properties and provides toArray() that maps to snake_case database columns.
It also automatically enriches meta with:
- user_agent
- referer
- method
- host
Common Use Cases
- Visitor analytics by page and period.
- Geo distribution dashboards.
- Fraud and abuse monitoring by IP/device.
- User activity insights tied to authenticated accounts.
Support
For support, please contact dev.mohamed.samy@gmail.com.
Author
Created by Mohamed Samy