# Laravel analytics with CrispStats

Updated 2026-11-23

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.

## Steps

### 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.

### 2. Paste the tag into your Blade layout

Open the layout your pages extend, often `resources/views/layouts/app.blade.php`, or `resources/views/components/layout.blade.php` if you use a layout component. Paste the tag inside `<head>`. The `@production` directive, described in [Laravel's Blade docs](https://laravel.com/docs/blade), 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.

`resources/views/layouts/app.blade.php`

```html
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    <meta charset="utf-8" />
    <title>@yield('title')</title>
    @production
    <script
      defer
      data-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>
```

### 3. Deploy and open your live app

`php artisan serve` runs on `localhost` or `127.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, add `data-debug` and `localhost` to `data-domains`: each event is logged in the console and nothing is sent ([localhost and testing](/docs/tracking/localhost-and-testing)).

### 4. Count clicks from a Blade view (optional)

A `data-cs-event` attribute turns any link or button into a [custom event](/glossary/custom-event), with properties from `data-cs-prop-` attributes. No JavaScript is needed.

`resources/views/pricing.blade.php`

```html
<a href="{{ route('register') }}" data-cs-event="signup click" data-cs-prop-plan="pro">Start free</a>
```

### 5. Send events from your server (optional)

Server-side events need a secret API key (`cs_sk_…`) with the **Events** scope, kept in `.env` and read through `config/services.php`, never in a view. Pass the visitor id you saved from `crispstats.id()` so the event joins that visitor's history. For payments from your own billing, use the [payment API](/docs/revenue/payment-api) instead.

`app/Services/CrispStats.php`

```php
use 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.

## 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-vitals` and `data-errors` on the tag, from a core script of about 2.2 KB gzipped.

## Notes for Laravel

- 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](/integrations/cloudflare)) in front of your app: see [the proxy guide](/docs/tracking/proxy).
- If you set a Content Security Policy, allow `https://in.crispstats.com` for `script-src` and `connect-src`.

Full reference: [Laravel in the docs](https://docs.crispstats.com/install/laravel)

## Frequently asked questions

### Is there a Laravel package for this?

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.

### How do I tie visits to signed-in users?

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.

### Does it work with Inertia?

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.

### What does it cost for a Laravel app?

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.

## Related

- [Web analytics for developers](/for/developers)
- [Server-side tracking explained](/glossary/server-side-tracking)
- [Revenue attribution](/features/revenue-attribution)
- [Stripe revenue attribution](/integrations/stripe)
- [Rails analytics](/integrations/rails)
- [Django analytics](/integrations/django)
