# Laravel

Add ProductLense to a Laravel app in the main Blade layout, then identify the signed-in user with a JSON-encoded id.

In Laravel the script goes into the Blade layout that every page extends, usually `resources/views/layouts/app.blade.php`. Apps that use Inertia or Livewire navigation change the URL with the history API, which ProductLense records as pageviews.

## Add the script

Replace `SITE_ID` with the id from Settings → Sites in the ProductLense dashboard.

```blade title="resources/views/layouts/app.blade.php" {5-6}
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    <meta charset="utf-8">
    <script>window.productlense=window.productlense||function(){(window.productlense.q=window.productlense.q||[]).push(arguments)}</script>
    <script defer data-site="SITE_ID" src="https://api.productlense.app/a.js"></script>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
  </head>
  <body>
    @yield('content')
  </body>
</html>
```

To keep the site id out of the template, put it in `.env` as `PL_SITE_ID`, expose it in `config/services.php`, and print it with `{{ config('services.productlense.site_id') }}`.

## Identify users

Use `@json`, which encodes the value safely for a script block:

```blade title="resources/views/layouts/app.blade.php" {3}
@auth
  <script>
    window.productlense('identify', String(@json(auth()->id())));
  </script>
@endauth
```

Place it after the queue stub. Use the same id you store on the customer in your billing provider; see [how payments are attributed](/docs/revenue/attribution).

## Events from your server

Actions that happen only on the server, such as a queued job that converts a trial, can be sent to the Events API with an API key. API keys are on the Scale plan; see [server-side events](/docs/install/server-side-events).

## Verify your installation

1. Deploy, then open your site in a normal browser window.
2. Open the Live page in the ProductLense dashboard. Your visit appears within a few seconds.
3. In the browser's developer tools, the Network tab shows a `POST` to `https://api.productlense.app/api/e` with status `204`.

`php artisan serve` runs on `localhost`, where nothing is sent unless you add `data-dev="true"` to the tag. Ad blockers in your own browser may block the request. If the visit does not appear, follow [Verify your installation](/docs/install/verify).
