Download the PHP package n2ns/laravel-stats without Composer

On this page you can find all versions of the php package n2ns/laravel-stats. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-stats

N2N Stats

Cookieless web analytics for Laravel, for a single site or for many. Install it in a site's own Laravel app to count that site in its own database and admin, or run one Laravel app that counts any number of websites. It collects page views, sources, outbound clicks, downloads, custom events, time on page and AI fetches, and stores only daily aggregates.

Packagist version Packagist downloads CI license

n2ns/laravel-site-stats is made by Datafrog LLC as part of N2NS Lab.

Why

What is and isn't collected

Collected (as daily counts) Not collected
Page path (without query string or fragment), page title, page type and content group IP addresses
Referrer host, UTM parameters, source type Full User-Agent strings
Country (from a CDN header or a local country database) Cookies or any browser storage
Device class, browser, operating system, browser language, site language version Individual page views or sessions
Outbound link, download and event targets (without query string) Identifiers that last longer than a day
Custom event names and labels; moves between pages within a visit Anything about a visitor across days or sites
AI fetcher requests (server side)

See PRIVACY.md for the full list and a paragraph you can adapt for your privacy policy.

Requirements

Installation

stats:install publishes config/stats.php, runs the migrations (pass --force in production) and prints the next steps. The app that stores the data runs in host mode (the default, STATS_MODE=host). Then, in every setup:

  1. Allow access to the statistics. They are guarded by the viewStats gate, which denies everyone until you define it, for example in AppServiceProvider::boot():

  2. Run the scheduler. The package schedules stats:maintain every five minutes. It ends visits that have been idle for 30 minutes, deletes expired salts and per-day deduplication data, and removes aggregates older than retention_days:

  3. Country and visitor IP. The country is read from the CF-IPCountry request header, so with the site (or the collector domain) behind Cloudflare with the proxy enabled there is nothing to do. Another CDN, or your web server, may send the country in another header: name it in STATS_COUNTRY_HEADER (e.g. CloudFront-Viewer-Country). Without such a header, point STATS_COUNTRY_DATABASE to a local country database in the MaxMind DB format (.mmdb), such as the free DB-IP IP to Country Lite or MaxMind GeoLite2 Country, and replace the file with each new release (both are updated monthly or more often; check the terms of the one you choose, DB-IP Lite asks for a link to DB-IP.com where its results are shown). The database is used when the header gives no country; it is read on your server and the IP address is not sent anywhere. With neither, every visit has the country unknown. Behind a proxy or CDN, configure Laravel's trusted proxies so that $request->ip() is the visitor's address: it is only used for the daily hash, the country lookup and rate limits, but if all requests seem to come from the proxy, all visitors become one.

Then set up one or both of the following.

Single site: count the app itself

The site is the Laravel app the package is installed in. The script and the collector are served on the app's own domain, the data is in its own database, and the statistics are in its own dashboard or Filament admin; nothing is sent to another server.

  1. Create the site while installing (or later with stats:site create):

  2. Add the script to the <head> of your layout. @statsScript prints the tag of the site whose domains match the request (or of STATS_HOST_SITE, on its own domains); it prints nothing when there is no such site:

  3. Optionally count on the server as well (see Counting on the server): TrackAiFetches for AI fetchers, and TrackPageviews for page views without JavaScript and only for real pages. With TrackPageviews, keep the script for its events only:

  4. Look at the statistics at /stats, or in your Filament admin with the plugin and STATS_DASHBOARD_ENABLED=false. With a single site there is no site switcher.

Multiple sites: one app counts many websites

One Laravel app stores the statistics of other websites, which may be on other servers and do not have to be Laravel apps. Their visitors' browsers send the data directly to this app.

  1. Choose where the collector lives. The tracking script, the collector and the read API can run on their own domain, for example stats.example.com, while the dashboard stays on the app's normal domain:

    Point that domain at the same Laravel app (DNS record, a web server server block with the same root and PHP handling, a TLS certificate). Leave it empty to serve them on the app's own domain under STATS_COLLECTOR_PATH (empty by default, so the script is at /js/stats.js).

  2. Add each website with stats:site create (or on the dashboard) and put the printed snippet in the <head> of its pages:

    With collector_domain set, the snippet points to https:// on that domain; otherwise its URL is built from APP_URL, so set APP_URL to the app's public URL first.

  3. Other Laravel sites can also count on their own server (AI fetches, page views, events) and send it to this app in client mode. Each such request crosses the network, so prefer the single-site setup for Laravel sites far from the host.

  4. With more than one site, the dashboard (or the Filament page) has a site switcher.

Site settings

Each counted website, in either setup, is a site with these settings (stats:site create and update, or the Site settings page opened from the top right of the dashboard or of the Filament statistics page):

Other actions:

update replaces the domain, site language and excluded path lists when any --domain, --locale or --exclude-path is given. delete removes the site and all of its statistics. Sites can also be added and edited on the dashboard.

The tracking script

The script is a single dependency-free file of about 8 KB.

Automatic:

Page details make reports easier to read. Each page view sends the page title and, when present, these meta tags. After a single-page-app navigation the page view is sent once the router has had a chance to render the new page, so the new title and meta tags are the ones sent:

Custom events, either from JavaScript:

or without JavaScript, by marking an element; a click sends the event, and when the element is or is inside a link, the link (internal links too) is sent as its target:

Event names match [a-z0-9_.-]{1,64}; labels are at most 120 characters; targets are stored without query string.

Exclusions: nothing is sent when navigator.webdriver is true (automated browsers), on file:// pages, or on localhost, 127.0.0.1 and [::1]. A data-exclude-paths attribute on the script tag excludes paths: a comma-separated list where * matches any characters, including / (for example data-exclude-paths="/admin*,/preview/*"). Downloads are detected by file extension; data-download-extensions="pdf,zip" on the script tag replaces the built-in list. Requests whose User-Agent is empty or matches the bot_user_agents list are dropped by the collector.

Data is sent with navigator.sendBeacon (or fetch with keepalive and no credentials) as a text/plain request, so browsers do not send a CORS preflight. The collector always answers 204 No Content, also when it drops an event; only malformed requests get 400.

Counting on the server

Three things can be counted by your Laravel app itself instead of the browser:

Register the middleware, for example in bootstrap/app.php:

In client mode everything is sent after the response has been delivered, with a 2-second timeout; failures are logged and never affect the page.

Dashboard

Open /stats (the dashboard_path) on your app's domain (unless dashboard_enabled is off, e.g. for apps that use the Filament pages instead). It is plain server-rendered Blade with inline SVG charts and a little plain JavaScript: no front-end build, no CDN, no external fonts. The interface is in English and Simplified Chinese and follows your app's locale (zh_CN shows Chinese). It looks like a default Filament panel (zinc grays, amber accent), has a light and a dark theme that follow the system setting, works down to phone width, and respects reduced motion.

The pages are standalone by default. To show them inside your own Blade admin, with its menu around them, set dashboard_layout to a layout to extend or to a layout component. There they keep the light theme, unless your admin follows the system's light or dark theme: then add 'theme' => 'system' and they follow it too:

Admins built with Inertia or another single-page front end can't wrap Blade pages; link to the standalone pages from their menu, and set dashboard_back_url to come back.

Filament

If your app uses Filament, register the plugin in your panel:

It adds, built from native Filament components (the tab charts are the dashboard's inline SVG in Filament sections) so they follow your panel's colours and dark mode:

All of it is visible only to users allowed by the viewStats gate. Report tabs load when they are opened. The Filament pages do not use the standalone dashboard, so turn that off to keep a single entry:

Read API

A read-only JSON API is served on the collector domain. Create a token:

The token is shown only once; only its SHA-256 hash is stored. Send it as a bearer token:

Endpoint Returns
GET /api/v1/sites The sites the token can read
GET /api/v1/sites/{public_id}/summary Totals for the range, and comparison: the complete-day current and previous ranges with their totals
GET /api/v1/sites/{public_id}/timeseries Daily visitors, visits, page views, events, outbound clicks, pages viewed, views per visit, bounce rate, average visit duration and average time on page
GET /api/v1/sites/{public_id}/ai AI visits, visitors, page views and fetches, with a daily series
GET /api/v1/sites/{public_id}/breakdown/{report}?limit=&offset=&min_visitors= Rows of one report
GET /api/v1/sites/{public_id}/snapshot?limit=&min_visitors= Summary, timeseries, AI and the top rows of every report in one call

report is one of pages, entries, exits, engagement, page_types, content, transitions, source_types, sources, campaigns, landing_pages, campaign_pages, search_pages, countries, devices, browsers, os, languages, locales, outbound, downloads, events, event_pages, event_locales, ai_referrals, ai_pages, ai_fetchers, ai_fetched_pages. Breakdown rows are paged with limit (1–500, default 50) and offset; has_more says whether more rows follow. min_visitors leaves out rows with fewer visitors. The snapshot's limit is 1–100 (default 10).

Every per-site endpoint takes the range as from and to (YYYY-MM-DD in the site's timezone; at most 366 days; default the last 30 days) or as period, one of today, yesterday, 7d, 15d, 30d, 90d, month, last_month (the same ranges as the dashboard). An unknown period, or a period together with from or to, returns 422. Responses include partial: true when the range includes the site's today, whose numbers are still growing. Requests are limited to 60 per minute per token.

Public counts

To show view or like counts on your own pages, turn on public counts for the site (stats:site update --site=<public id> --public-counts, or on the dashboard). Then anyone can read all-time numbers without a token:

Up to 50 paths and 20 custom event names per request; event counts are grouped by label. Responses allow any origin (Access-Control-Allow-Origin: *), are cacheable for 60 seconds and are limited to rate_limits.public_per_minute requests per IP and minute. Sites without public counts answer 404.

Configuration

Publish the file with php artisan vendor:publish --tag=stats-config. Main options:

Key Env Default Meaning
mode STATS_MODE host host stores data and serves the collector, dashboard and API; client only sends server-side events (AI fetches, page views, events) to a host
table_prefix STATS_TABLE_PREFIX stats_ Prefix of all tables
collector_domain STATS_COLLECTOR_DOMAIN none Domain for the script, collector and API; none = the current domain
collector_path STATS_COLLECTOR_PATH empty Path prefix of those routes
dashboard_enabled STATS_DASHBOARD_ENABLED true Serve the standalone dashboard and site management pages; turn off when the host shows the statistics in its own admin (Filament plugin)
dashboard_path STATS_DASHBOARD_PATH stats Dashboard route prefix
dashboard_middleware ['web'] Middleware of the dashboard routes (the viewStats gate is always applied)
dashboard_layout null Host admin layout for the dashboard and site pages: ['view' => …, 'section' => …, 'title_sections' => […], 'sections' => […]] or ['component' => …], either with 'theme' => 'system' when the layout follows the system theme; standalone when null
dashboard_back_url STATS_DASHBOARD_BACK_URL null Address of a back link at the top of the dashboard and site pages
dashboard_back_label null Text of the back link (the translated "Back" when null)
country_header STATS_COUNTRY_HEADER CF-IPCountry Request header with the visitor's two-letter country code
country_database STATS_COUNTRY_DATABASE none Path of a local .mmdb country database, used when the header gives no country
host_site STATS_HOST_SITE none Host mode: public id of the app's own site, used by the middleware, Stats::event() and @statsScript (which prints a tag only on that site's domains); none = found by the Host header
visit_timeout_minutes 30 Idle time that ends a visit
retention_days STATS_RETENTION_DAYS none Days to keep daily aggregates; none = forever
rate_limits Collector limits per site (3000/min), per IP (120/min, IPv6 per /64) and per visitor (60/min); AI fetches per site (600/min), API per token (60/min), public counts per IP (120/min)
max_new_rows_per_day 5000 New daily rows per site, day and table; further values are counted under (other)
bot_user_agents User-Agent substrings that are dropped as bots
ai_fetchers User-Agent substrings counted as AI fetchers
ai_user_fetchers AI fetchers that fetch on a person's request (the others are crawlers)
search_hosts, social_hosts, ai_hosts Referrer hosts that classify a source as search, social or AI; ai_hosts also match a utm_source given without medium and campaign
click_ids Click-id query parameters (gclid, fbclid, …) and the source they imply
download_extensions File extensions counted as downloads
client.endpoint, client.site, client.key STATS_CLIENT_* Client mode: host URL, site public id and server key

Data model

All tables use the table_prefix. Daily tables are bucketed by the site's timezone.

Table Contents Kept
stats_sites Name, public id, allowed domains, timezone, site languages, excluded paths, public counts, hash of the server key Until deleted
stats_api_tokens Name, hash of the token, allowed sites, last use Until revoked
stats_daily_totals Page views, visitors, visits, bounces, visit duration, events, outbound clicks per day retention_days
stats_daily_pages Per path: page views, visitors, entries, visitors who entered there, exits, the page views, outbound clicks and events of visits that entered there; time on page and scroll depth; title, page type, content group retention_days
stats_daily_sources Per source (type, host or UTM source, medium, campaign, content, term): visits, visitors, page views, bounces, outbound clicks, events retention_days
stats_daily_entries Per entry page and source: visits, visitors retention_days
stats_daily_transitions Per move from one page to another: count, visitors retention_days
stats_daily_dimensions Per country, device, browser, OS, browser language, site language: visits, visitors, page views retention_days
stats_daily_events Outbound clicks, downloads, custom events and AI fetches per name, label, target and page: count, visitors retention_days
stats_salts The random salt of each day Two days
stats_uniques Visitor hashes already counted per day and report row Two days
stats_visits Visits in progress (hash, start, last activity, page views, entry and exit page, source, device class, browser, OS, country, language) Until 30 minutes idle

How the metrics are defined

Limitations

Contributing and security

See CONTRIBUTING.md. Please report vulnerabilities privately as described in SECURITY.md.

License

Apache-2.0. Copyright 2026 Datafrog LLC.


All versions of laravel-stats with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/console Version ^12.0|^13.0
illuminate/database Version ^12.0|^13.0
illuminate/http Version ^12.0|^13.0
illuminate/routing Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
illuminate/view Version ^12.0|^13.0
maxmind-db/reader Version ^1.12
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package n2ns/laravel-stats contains the following files

Loading the files please wait ...