# Referral quickstart: Laravel

The tag in your layout, a middleware that keeps the code unencrypted, and each signup reported from the Registered event.

Referrals in a Laravel app: the tag in your layout, the partner's code kept by
your own server, every signup reported as a lead, and sales from your billing
system.

## Before you start

- A key with the **Referral tracking** preset (the single scope
  `conversions:write`), made under **Settings, Developers**. Keep it on your
  server as `BOLTCLAW_API_KEY`, never in a browser or an app.
- The program's id, on its **Setup** tab, as `BOLTCLAW_PROGRAM_ID`.

## 1. Put the tag on your site

Add the version 2 tag to every page, in your root layout or template. It keeps a
partner's code from `?via=<code>` in a first-party cookie on your own domain,
`bc_ref`, for the program's cookie window, and gives you
`BoltClawReferral.code()` in the browser.

```html
<script src="https://boltclaw.ai/referrals/v2.js" defer data-payment-links="auto"></script>
```

`data-payment-links="auto"` is only for Stripe Payment Links and the Buy Button;
`data-welcome="auto"` adds the friend's welcome. The tag makes no request at all
unless the welcome is on and the visitor arrived with a code.

## 2. Keep the code from your server

Laravel encrypts cookies by default, and the tag reads `bc_ref` as plain text, so
exclude it first:

```php
// bootstrap/app.php (Laravel 11 and later)
->withMiddleware(function (Middleware $middleware) {
    $middleware->encryptCookies(except: ['bc_ref']);
    $middleware->web(append: [\App\Http\Middleware\KeepReferral::class]);
})
```

```php
// app/Http/Middleware/KeepReferral.php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class KeepReferral
{
    public function handle(Request $request, Closure $next)
    {
        $code = (string) $request->query('via', '');
        if (strlen($code) >= 3 && strlen($code) <= 40 && preg_match('/^[a-z0-9]+(-[a-z0-9]+)*$/', $code)) {
            // 30 days (the program's cookie window), path /, secure, not httpOnly, SameSite Lax
            Cookie::queue('bc_ref', $code, 30 * 24 * 60, '/', null, true, false, false, 'lax');
        }
        return $next($request);
    }
}
```

**Why set the cookie from your server.** Safari deletes a cookie a script
created once the visitor has gone 7 days of browsing without using your site,
and keeps one only 24 hours on a page reached through a tracking link, so a
code the tag keeps can be gone a week later. A cookie your OWN server sets in
its response keeps its full lifetime, unless it comes through somebody else's
host (a third-party CNAME or IP address), which Safari caps at 7 days
(WebKit's published tracking prevention rules, read 2026-10-23). Setting `bc_ref`
from your server, not `HttpOnly`, keeps the full window and the tag still reads
it.

## 3. Report each signup as a lead

Listen for Laravel's `Registered` event:

```php
// app/Listeners/ReportSignup.php
namespace App\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Http;

class ReportSignup
{
    public function handle(Registered $event): void
    {
        $code = request()->cookie('bc_ref');
        Http::withToken(config('services.boltclaw.key'))
            ->post('https://api.boltclaw.ai/v1/referrals/conversions', array_filter([
                'program_id' => config('services.boltclaw.program'),
                'event' => 'lead',
                'event_id' => 'signup-' . $event->user->id,
                'customer' => ['id' => (string) $event->user->id],
                'referral' => $code ? ['code' => $code] : null,
            ]))
            ->throw();
    }
}
```

Put `BOLTCLAW_API_KEY` and `BOLTCLAW_PROGRAM_ID` in `.env` and read them in
`config/services.php`. Queue the listener if you like: the same `event_id` makes
a retry harmless.

## 4. Record sales

Connect the billing system you take payments with on the **Setup** tab (see
[Connect your billing system](https://boltclaw.ai/docs/referral-setup)), and pass the code to checkout
as its guide shows: sales, renewals and refunds are then reported for you, and
the setup status ticks them off. Not on one of the twelve? Report each sale from
your server with `event: "sale"`, your own `event_id` for the payment, the same
`customer.id` as the signup, `amount` in minor units and `currency`.

## 5. Send a test sale

Press **Send a test sale** on the Setup tab: a click, a signup, a sale with two
items and a partial refund go through your real rules as test data, credited to
the program's test partner, never paid and never counted. To check your own
wiring, send one of your own conversions with `"test": true`: it is test data
too, and **Clear test data** deletes it all. The setup status's rows turn green
only for live events.

<!-- https://boltclaw.ai/docs/referral-quickstart-laravel as markdown. Generated from openapi.yaml; API version 1.22.0. -->
<!-- Every /docs page answers as markdown: add .md to its path, or send Accept: text/markdown. -->
