laravel-analytics maintained by helori
Laravel Analytics
Privacy-friendly visit analytics for Laravel. No UI: build your own dashboard on Stats, or read the statistics from another service through a token-protected JSON API.
- No IP stored. A visitor is a hash of the IP, the browser, the day and the app key: enough to count the distinct visitors of a day, not to identify anyone.
- Bots set aside. Declared bots (DeviceDetector) are not recorded. The others seldom run JavaScript: a visit only counts once the page's script confirms it, and automated browsers (
navigator.webdriver) don't confirm. Unconfirmed visits are reported apart. - Opt-out. A refusal cookie set by the page, and the browser's Global Privacy Control (
Sec-GPC), are respected. Logged-in users are not counted. - Retention. Visits older than 25 months are pruned every day (the CNIL's limit for audience measurement without consent).
Works with Postgres, MySQL/MariaDB and SQLite.
A visit is delimited by its own cookie, extended on every page and ended after 30 minutes without one. It records its entry page, its source (utm_source, ref, or the search engine / social network it came from), its campaign (utm_campaign), the site it came from, the device, the system and the browser.
Installation
composer require helori/laravel-analytics
php artisan migrate
If the app already has an analytics_visits table with the same schema, the migration keeps it and its data.
Count the pages with the analytics middleware:
Route::middleware('analytics')->group(function(){
Route::get('/', HomeController::class);
});
Then confirm the visit from the page's JavaScript:
import { confirmVisit } from '../../vendor/helori/laravel-analytics/resources/js/analytics.js';
confirmVisit();
It posts to /analytics/confirm with the CSRF token of <meta name="csrf-token">. To let visitors refuse, use setStatsRefused(true|false) and statsRefused() from the same module.
The package registers model:prune for the visits in the scheduler (daily, 04:30): the app's schedule:run cron must be running. Set ANALYTICS_SCHEDULE=false to schedule it yourself.
Configuration
php artisan vendor:publish --tag=analytics-config
| Key | Env | Default |
|---|---|---|
connection |
ANALYTICS_DB_CONNECTION |
default connection |
table |
analytics_visits |
|
retention_months |
ANALYTICS_RETENTION_MONTHS |
25 |
timeout_minutes |
30 | |
visit_cookie |
ANALYTICS_VISIT_COOKIE |
analytics_visit |
opt_out_cookie |
ANALYTICS_OPT_OUT_COOKIE |
analytics_opt_out |
exclude_authenticated |
true | |
confirm.path |
ANALYTICS_CONFIRM_PATH |
analytics/confirm |
api.token |
ANALYTICS_TOKEN |
MONITORING_TOKEN, see below |
api.prefix |
ANALYTICS_API_PREFIX |
api/analytics |
When the cookies or the confirmation path are not the defaults, tell the JavaScript:
import { configureAnalytics, confirmVisit } from '…/analytics.js';
configureAnalytics({ visitCookie: 'my_visit', optOutCookie: 'my_no_stats', confirmUrl: '/visit' });
confirmVisit();
In the app's dashboard
use Helori\LaravelAnalytics\Period;
use Helori\LaravelAnalytics\Stats;
$stats = new Stats(Period::lastDays(30)); // or Period::between('2026-11-01', '2026-11-30')
$stats->totals(); // visits, visitors, pageviews, unconfirmed
$stats->series(); // labels, visits, visitors per day (per week beyond 4 months)
$stats->breakdown('source'); // [{label, count}]: source, referrer, campaign, entry_path, device, os, browser
// Any ranking over the confirmed visits
Stats::top($stats->visits(), "COALESCE(client_name, 'Unknown')", 6);
The expression given to Stats::top() is raw SQL, grouped and ranked as is: keep it to portable SQL (COALESCE, LOWER, column names…), and never build it from user input.
API
Every request carries Authorization: Bearer <token>. Without a token configured the routes answer 404. Periods are days of the app's timezone; default: the last 30 days.
The token is ANALYTICS_TOKEN, any long random string:
php -r "echo bin2hex(random_bytes(32)), PHP_EOL;"
When it is not set, MONITORING_TOKEN is used: an app that also installs helori/laravel-monitoring needs a single token for both APIs.
GET /api/analytics/summary?from=2026-11-01&to=2026-11-30&unit=day|week
{
"app": {"name": "My App", "env": "production", "url": "https://example.com", "timezone": "Europe/Paris"},
"from": "2026-11-01", "to": "2026-11-30", "unit": "day",
"totals": {"visits": 1200, "visitors": 950, "pageviews": 3100, "unconfirmed": 340},
"series": {"labels": ["2026-11-01", "…"], "visits": [40, "…"], "visitors": [32, "…"]}
}
visitors counts distinct visitors each day: the hash changes every day, so a visitor coming back on another day counts again.
GET /api/analytics/breakdown?dimension=source&limit=10
Plus from and to. Dimensions: source (falls back to the referring site), referrer, campaign, entry_path, device, os, browser. A null label means unknown (direct access for source).
{"from": "…", "to": "…", "unit": "day", "dimension": "source", "items": [{"label": "google", "count": 420}, {"label": null, "count": 300}]}
Tests
composer install
composer test # SQLite in memory
# On a real server: an empty database, dropped and recreated by the tests
TEST_DB_DRIVER=pgsql TEST_DB_DATABASE=packages_test TEST_DB_USERNAME=postgres composer test
TEST_DB_DRIVER=mysql TEST_DB_DATABASE=packages_test TEST_DB_USERNAME=root composer test
TEST_DB_HOST, TEST_DB_PORT and TEST_DB_PASSWORD are also read.