Data collection
Learn how to collect information for personalization.
This guide explains how to associate the visitor with a user from your application so you can deliver personalized experiences.
Profile and session attributes are enriched on the client side through the JavaScript SDK once you set up the client-side SDK. The PHP SDK focuses on the server-side concern of identifying the visitor.
User identity
When a user registers or logs in, you may want to associate their profile with an identifier that is meaningful to your business, so that you can recognize them when they return.
You can use any identifier that is meaningful to your business, such as an email address, a username, or a customer ID, provided it is unique.
To associate a user profile with an identifier, use the identify method:
<?phpuse Croct\Plug\Croct;
$croct = Croct::fromDotenv();$croct->identify('john-doe');You usually call identify when the user logs in or registers. In the same way, you can call anonymize when the user logs out:
<?phpuse Croct\Plug\Croct;
$croct = Croct::fromDotenv();$croct->anonymize();You can then recognize identified users in your queries using the user variable:
user is identifiedSyncing identity automatically
Instead of calling identify and anonymize by hand, you can let the SDK keep the visitor in sync with your authentication system.
Implement the IdentityResolver interface to resolve the current user from your application:
<?phpuse Croct\Plug\IdentityResolver;
final class AuthIdentityResolver implements IdentityResolver{ private Auth $auth;
public function __construct(Auth $auth) { $this->auth = $auth; }
public function getUserId(): ?string { return $this->auth->getUser()?->getId(); }}Then provide it when you initialize the SDK:
<?phpuse Croct\Plug\Croct;use Croct\Plug\CookieStorage;
$croct = Croct::plug( appId: 'APPLICATION_ID', apiKey: 'API_KEY', storage: CookieStorage::fromGlobals(), identity: new AuthIdentityResolver($auth),);The SDK reconciles the visitor session with the resolved user on every request, so you no longer need to call identify or anonymize explicitly.
Resolving the user from the frameworkâs authentication and locale is handled automatically by our framework integrations, such as the Laravel, Symfony, and Drupal SDKs.