Frameworks
Laravel analytics with CrispStats
A Laravel app gets CrispStats from one script tag inside the head of the Blade layout your pages share, such as resources/views/layouts/app.blade.php. Every page rendered with that layout is tracked, and actions that happen on the server, such as a renewal in a webhook, are sent with one HTTP request.
Updated
Set up CrispStats on Laravel
- 01
Step 1: Copy the snippet with your key
In the dashboard, open your site’s Settings, select Tracking code and choose the HTML tab. It shows the tag with your public key (it starts with
cs_pk_) and your domain filled in. The public key is safe to publish. There is no Composer or npm package to install. - 02
Step 2: Paste the tag into your Blade layout
Open the layout your pages extend, often
resources/views/layouts/app.blade.php, orresources/views/components/layout.blade.phpif you use a layout component. Paste the tag inside<head>. The@productiondirective, described in Laravel’s Blade docs, renders it only in production; it is optional, because the script ignores local hosts anyway. If the admin area uses a separate layout, add the tag there only if you want it counted.Replace the highlighted values with your own.
resources/views/layouts/app.blade.php<!doctype html><html lang="{{ str_replace('_', '-', app()->getLocale()) }}"><head><meta charset="utf-8" /><title>@yield('title')</ title>@production<scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"src= "https:/ / in. crispstats. com/ d. js"></script>@endproduction</head><body>@yield('content')</body></html> - 03
Step 3: Deploy and open your live app
php artisan serveruns onlocalhostor127.0.0.1, which the script ignores on purpose. Deploy, then open your live app in a normal browser window. To watch events while you develop, adddata-debugandlocalhosttodata-domains: each event is logged in the console and nothing is sent (localhost and testing). - 04
Step 4: Count clicks from a Blade view (optional)
A
data-cs-eventattribute turns any link or button into a custom event, with properties fromdata-cs-prop-attributes. No JavaScript is needed.resources/views/pricing.blade.php<a href="{{ route('register') }}" data-cs-event="signup click" data-cs-prop-plan="pro">Start free</a> - 05
Step 5: Send events from your server (optional)
Server-side events need a secret API key (
cs_sk_…) with the Events scope, kept in.envand read throughconfig/services.php, never in a view. Pass the visitor id you saved fromcrispstats.id()so the event joins that visitor’s history. For payments from your own billing, use the payment API instead.app/Services/CrispStats.phpuse Illuminate\Support\Facades\Http;$response = Http::withToken(config( 'services. crispstats. key')) // cs_sk_…->post( 'https:/ / in. crispstats. com/ e', ['k' => 'cs_pk_your_site_key','t' => 'event','n' => 'subscription_renewed','u' => '','p' => ['plan' => 'pro'],'vid' => $subscription->crispstats_visitor_id,]);// 202 means the event was accepted.
Check that it works
Open your live app with the browser’s developer tools on the Network tab. You should see d.js load from in.crispstats.com and a POST to /e that answers 202. In the dashboard, Settings, Tracking code shows Receiving events. and the visit appears on the Realtime screen within seconds.
CrispStats on Laravel
What you get
- Pageviews, visitors and sessions for every page rendered with the layout, with no cookies and nothing stored on the visitor’s device.
- Sources and channels, including UTM campaigns and visits from AI assistants such as ChatGPT and Perplexity, plus countries, devices and browsers.
- With Inertia or another client-side router, each new URL counted as a pageview from the History API, with the tag in the root view once.
- Server events, such as renewals and cancellations, in the same reports as browser events, tied to the visitor through
crispstats_visitor_id. - Optional web vitals and JavaScript errors with
data-vitalsanddata-errorson the tag, from a core script of about 2.2 KB gzipped.
Notes and limits
- Only templates that use the layout get the tag. A standalone Blade view that does not extend it needs the tag too.
- Keep the secret key out of Blade views and out of anything sent to the browser. The public key in the tag is the only key the page needs.
- CrispStats records errors in the browser only. Exceptions thrown in your PHP code are not tracked; keep your error tracker for those.
- To count visitors whose ad blocker stops the collector host, add one forwarding rule on nginx, Caddy or Cloudflare (Worker guide) in front of your app: see the proxy guide.
- If you set a Content Security Policy, allow
https://in.crispstats.comforscript-srcandconnect-src.
The docs page covers the optional steps this guide leaves out.
Setup questions. Straight answers.
What people ask before they connect it.
No. The script tag in your layout and, for server events, one HTTP request with Laravel's HTTP client are the whole integration. There is nothing to install or keep updated.
Add the queue stub from the docs above the tag, then call identify with your own user id inside an @auth block. Identify makes those visits personal data, so read the identify guide in the docs before you turn it on.
Yes. Inertia changes pages without a reload and updates the URL through the History API, and the script counts each new URL as a pageview. Put the tag in the root view once. Updates that do not change the URL are not pageviews.
Pageviews from your Blade views and the events your PHP code sends share one monthly allowance. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events. Paid plans never pause at the limit.
Not answered here? Write to hello@crispstats.com
Go further
Related
- Web analytics for developers
A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.
- Server-side tracking explained
Server-side tracking sends analytics events from your server, not the visitor's browser.
- Revenue attribution
See which source, page and campaign each payment came from.
- Stripe revenue attribution
Connect Stripe to CrispStats with a restricted key and pass the visitor id at checkout, so every payment, renewal and refund is tied to the visit behind it.
- Rails analytics
Add CrispStats to a Rails app with one script tag in application.html.erb.
- Django analytics
Add CrispStats to a Django site with one script tag in base.html, the template your pages extend, then send server-side events from Python.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
Your traffic, crisp and clear
One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.