Data collection
Learn how to collect information for personalization.
This guide explains how to associate the visitor with a user from your application, enrich their profile, and detect their locale, so you can deliver personalized experiences.
Identity and locale are resolved on the server. Profile and session attributes are collected in the browser, which you trigger from your views with the @croct Blade directive.
Identity
When your application has authenticated users, you can link the visitor session with the logged-in user so you can recognize them when they return.
Automatic reconciliation
When you use Laravel’s authentication, the package reconciles the visitor with the authenticated user on every request, using the user identifier as the user ID. This is enabled by default and requires no code.
You can then recognize identified users in your queries using the user variable:
user is identifiedCustom identifier
By default, the package uses the authenticated user’s primary key. To reconcile with a different identifier, such as the email or a UUID, implement the IdentityResolver interface:
<?php
namespace App\Croct;
use Croct\Plug\IdentityResolver;use Illuminate\Contracts\Auth\Factory as AuthFactory;
final class AuthIdentityResolver implements IdentityResolver{ private AuthFactory $auth;
public function __construct(AuthFactory $auth) { $this->auth = $auth; }
public function getUserId(): ?string { return $this->auth->guard()->user()?->email; }}Then bind it to the interface in a service provider so the package uses it instead of the default:
use App\Croct\AuthIdentityResolver;use Croct\Plug\IdentityResolver;
public function register(): void{ $this->app->singleton(IdentityResolver::class, AuthIdentityResolver::class);}Without authentication
If you do not use Laravel’s authentication, implement the same IdentityResolver interface to return your own user ID, for example from the session or a token. Returning null keeps the visitor anonymous.
The injected Plug service also exposes identify and anonymize for imperative control when you manage the session lifecycle yourself.
Profile and session attributes
Besides identity, you can enrich the visitor’s profile and session with information relevant to your business. These calls run in the browser, so you trigger them from your views with the @croct Blade directive, passing server-side values through Blade.
Profile data
Profile attributes describe the user across visits, such as their name, email, or interests. For example, to sync the signed-in user’s details into their profile:
<!DOCTYPE html><html> <head> <title>My application</title> </head> <body> @yield('content')
@auth @croct croct.user.edit() .set('name', @json(auth()->user()->name)) .set('email', @json(auth()->user()->email)) .save(); @endcroct @endauth </body></html>Session data
Session attributes describe the current visit, such as the plan a user is considering on the pricing page:
@extends('layouts.app')
@section('content') <select id="plan-selector"> <option value="basic">Basic</option> <option value="premium">Premium</option> <option value="enterprise">Enterprise</option> </select>
@croct const selector = document.getElementById('plan-selector');
selector.addEventListener('change', () => { croct.session.edit() .set('plan', selector.value) .save(); }); @endcroct@endsectionSee Data collection for the full client-side API.
Locale detection
The package detects the visitor locale from the application locale automatically and forwards it as the preferred locale, so content is returned in the matching language out of the box. This is enabled by default, controlled by the locale.enabled option.
To pin a fixed locale or set a fallback when detection is disabled, use the locale.default option:
'locale' => [ 'default' => 'en-us',],For full control, implement the LocaleResolver interface and bind it the same way as a custom identity resolver:
$this->app->singleton(LocaleResolver::class, AccountLocaleResolver::class);