# Install on Laravel

> Add Reveata to the Blade layout of a Laravel app.

Laravel renders the `<head>` of your pages from a Blade layout, so the snippet goes there once.

Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):

```html
<script
  defer
  data-website-id="ws_your_tracking_id"
  src="https://reveata.com/js/script.js"
></script>
```

## Add it to your site

1. Open the layout your pages use, usually `resources/views/layouts/app.blade.php` (starter kits also have `resources/views/layouts/guest.blade.php` for the sign-in pages; add it there too). With Inertia, it is the root template, usually `resources/views/app.blade.php`.
2. Paste the snippet before `</head>`. To load it only in production, wrap it in Blade’s `@production` directive:

```blade
<head>
    {{-- … --}}
    @production
        <script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script>
    @endproduction
</head>
```

3. Deploy. If you cache views (`php artisan view:cache`), run it again after the change.

Inertia page changes go through the browser’s History API, which the script follows, so every page is counted.

## Check it works

Open your site in a private window, then check that the visit appears in your Reveata dashboard. If it doesn’t, follow [Verify your installation](https://reveata.com/docs/verify-your-installation). The script ignores `localhost` by default, so local development visits stay out of your numbers.

## Goals

Add `data-reveata-goal="signup"` to a button or link in your Blade views, or call `reveata('goal', 'signup')` from JavaScript. See [Custom goals](https://reveata.com/docs/custom-goals).

## Serve it from your own domain

Two routes in `routes/web.php` can pass the script and events through your own domain, with the visitor’s address and your proxy secret: see [Laravel](https://reveata.com/docs/self-hosted-proxy#laravel) in the proxy guide. Store the secret as `REVEATA_PROXY_SECRET` and read it through `config()`, so it survives `php artisan config:cache`. Laravel Forge and most servers also run NGINX in front, which can do it instead: [Proxy through NGINX](https://reveata.com/docs/nginx#proxy-through-nginx). Or use the [managed proxy](https://reveata.com/docs/managed-proxy): one DNS record, no code.

## Revenue

First connect your payment provider in Reveata (for example Stripe, which Laravel Cashier uses): see [Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider). Then pass the Reveata visitor id with each checkout, so the payment is credited to the visit that brought the buyer.

In the Blade view with your buy button, add two hidden fields and fill them when the form is sent:

```blade
<form method="post" action="{{ route('checkout') }}">
    @csrf
    <input type="hidden" name="reveata_visitor_id">
    <input type="hidden" name="reveata_session_id">
    <button>Buy</button>
</form>
<script>
    document.addEventListener('submit', function (e) {
        var ids = window.reveata && window.reveata.metadata ? window.reveata.metadata() : {};
        Object.keys(ids).forEach(function (name) {
            var input = e.target.querySelector('input[name="' + name + '"]');
            if (input) input.value = ids[name];
        });
    });
</script>
```

With Cashier, pass them as the Checkout Session’s metadata:

```php
use Illuminate\Http\Request;

Route::post('/checkout', function (Request $request) {
    $ids = array_filter($request->only(['reveata_visitor_id', 'reveata_session_id']));

    return $request->user()->checkout(['price_123' => 1], [
        'success_url' => route('thanks').'?session_id={CHECKOUT_SESSION_ID}',
        'cancel_url' => route('pricing'),
        'metadata' => $ids,
    ]);
})->middleware('auth')->name('checkout');
```

For subscriptions, use `newSubscription('default', 'price_123')->checkout([...])` with the same `metadata`.

**Payments from your own billing.** If Reveata doesn’t connect to your provider, send each payment with the [Payments API](https://reveata.com/docs/payments-api):

```php
use Illuminate\Support\Facades\Http;

Http::withToken(config('services.reveata.site_key'))
    ->post('https://reveata.com/api/v1/payments', [
        'transactionId' => 'order_'.$order->id,
        'amount' => (float) $order->total_without_tax, // major units, e.g. 49.00
        'currency' => 'USD',
        'email' => $order->email,
        'visitorId' => $order->reveata_visitor_id, // saved from the hidden field
    ]);
```

Add `'site_key' => env('REVEATA_SITE_KEY')` to the `reveata` entry in `config/services.php`.

## Common pitfalls

- **Layouts you forgot.** Starter kits have separate layouts for guest pages; Inertia apps use `resources/views/app.blade.php`. The snippet goes in each layout that renders `<head>`.
- **Cached views and config.** Run `php artisan view:cache` and `php artisan config:cache` again after deploying.
- **`@production`.** With the snippet wrapped in it, nothing is sent from your local or staging environments. That’s on purpose; test the production site.
- **Counting payments twice.** With your payment provider connected in Reveata, don’t also send the same payments with the Payments API.
