[ GUIDE ]

How to Track Users in Laravel Monitoring

Attach user identity to every request, exception, and job — then pivot from user to telemetry in a single click.

QUICK ANSWER

How do I attach user identity to Laravel monitoring?

Use a middleware or AppServiceProvider listener to attach the authenticated user to every telemetry event. Store user_id and email (avoid sensitive PII). Now every request, exception, job, and query logged in your monitoring tool can be filtered by user, and support tickets referencing a specific customer can be investigated without grep-ing logs.

Updated · 2026-04-13

Middleware approach

A middleware runs on every authenticated request. Push user context into a request-scoped container your telemetry tool reads.

app/Http/Middleware/AttachUserTelemetry.phpphp
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class AttachUserTelemetry
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $user = Auth::user();

            // Push into Laravel's log context — appears on every log line
            Log::withContext([
                'user_id' => $user->id,
                'user_email' => $user->email,
                'user_plan' => $user->subscription_plan ?? null,
            ]);

            // For monitoring SDKs that expose a user-setter (NightOwl / Nightwatch)
            if (function_exists('nightwatch')) {
                nightwatch()->setUser([
                    'id' => $user->id,
                    'email' => $user->email,
                    'plan' => $user->subscription_plan ?? null,
                ]);
            }
        }

        return $next($request);
    }
}

Register it on routes that should be tracked:

bootstrap/app.phpphp
->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \App\Http\Middleware\AttachUserTelemetry::class,
    ]);
    $middleware->api(append: [
        \App\Http\Middleware\AttachUserTelemetry::class,
    ]);
})

What to attach (and what not to)

ATTACH

  • User ID (stable, non-secret)
  • Email (useful for support)
  • Plan or subscription tier
  • Team / org ID for B2B apps
  • Feature flag variant

AVOID

  • Passwords, password hashes
  • Payment card info, bank details
  • Government IDs, SSNs
  • Full addresses, phone numbers
  • Any field users haven't consented to share

Frontend → backend correlation

For full visibility, your front end should generate a request ID and pass it via header on every API call. The backend picks it up and tags all telemetry with it.

Frontend (every fetch)

js
const requestId = crypto.randomUUID();
fetch('/api/orders', {
    headers: {
        'X-Request-Id': requestId,
        // ...
    },
});
app/Http/Middleware/EnsureRequestId.phpphp
public function handle($request, Closure $next)
{
    $id = $request->header('X-Request-Id') ?? (string) \Illuminate\Support\Str::uuid();
    $request->attributes->set('request_id', $id);

    Log::withContext(['request_id' => $id]);

    return tap($next($request), fn ($response) => $response->header('X-Request-Id', $id));
}

THE EASY WAY

NightOwl ships with a per-user view

The Nightwatch package captures user_id and email automatically when attached to the request. NightOwl exposes a Users page; click a user to see their detail view with FILTER BY tabs for Requests, Jobs, Exceptions, and Logs in one place. Perfect for support investigations.

bash
composer require nightowl/agent
php artisan nightowl:install

Frequently asked questions

How do I track users in my Laravel monitoring?

Attach the authenticated user to every request in a middleware or service provider, so telemetry — requests, exceptions, jobs, queries — includes user_id and email. This lets you answer 'which users hit this bug?' or 'is this slow endpoint slow for everyone or just one customer?' Most APMs (NightOwl, Nightwatch Cloud, Sentry) support this out of the box.

Is tracking users in monitoring a privacy risk?

It can be. Attach only the user ID and non-sensitive fields (email, plan). Don't send PII, payment data, or free-form text you haven't reviewed. Check your privacy policy and local regulations (GDPR) — you may need to disclose that you process user IDs in a monitoring system. BYOD tools like NightOwl keep the data in your database, which simplifies compliance.

How do I find all the exceptions a specific user hit?

Your monitoring tool needs to index exceptions by user_id. With NightOwl, there's a users view that shows requests, exceptions, and jobs per user — click a user to see their full activity. Without that, you're grep-ing log files or manually querying exception tables.

Should I track user sessions or just user IDs?

User ID is enough for backend monitoring — you want to know 'which user hit this error,' not reconstruct their click path. Session tracking is a front-end concern (RUM, session replay) handled by separate tools like FullStory or LogRocket. For Laravel backend monitoring, user ID + email is the standard.

How do I correlate a user's frontend actions with backend events?

Generate a request/trace ID in the front end and pass it through every API call in an X-Request-Id header. Log that ID on the backend. Now a support ticket that includes the front-end trace ID can be searched in backend logs/APM to find the exact backend trace. Laravel middleware can auto-generate one if the header is missing.

Does Laravel ship with user tracking for monitoring?

Laravel's Request::user() gives you the authenticated user, but there's no built-in telemetry pipeline. You need either a manual setup (attach user to logs + APM SDK calls) or a tool that ships with Laravel-native user tracking — the Nightwatch package captures user_id automatically on every watcher, which NightOwl surfaces in its user view.

How do I handle user tracking for guest traffic?

Use a persistent session cookie or device fingerprint so guest activity has a stable ID, even if they're not authenticated. Write that ID to a context field in your monitoring. When the guest converts to a user, you can link the pre-auth activity to the user account retroactively.

PRICING

Flat pricing. No event caps. No per-seat fees.

14-day free trial, no credit card. Your PostgreSQL, your data.

HOBBY

$5 /month

1 app · 14 days lookback · all Laravel events

TEAM

$15 /month

Up to 3 connected apps · unlimited environments · all Laravel events

AGENCY

$69 /month

Unlimited apps · unlimited agent instances · same flat rate at any traffic

Related