# 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.

> **Collected on the client**
>
> Profile and session attributes are enriched on the client side through the [JavaScript SDK](/reference/sdk/javascript/data-collection) once you [set up the client-side SDK](client-side-setup). 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`](api/core/plug/identify) method:

```php
<?php
use 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`](api/core/plug/anonymize) when the user logs out:

```php
<?php
use Croct\Plug\Croct;

$croct = Croct::fromDotenv();
$croct->anonymize();
```

You can then recognize identified users in your queries using the [`user`](/reference/cql/context#user) variable:

```cql
user is identified
```

## Syncing identity automatically

Instead of calling [`identify`](api/core/plug/identify) and [`anonymize`](api/core/plug/anonymize) by hand, you can let the SDK keep the visitor in sync with your authentication system.

Implement the [identity resolver](api/resolvers/identity-resolver) interface to resolve the current user from your application:

```php
<?php
use 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](api/core/croct/plug):

```php
<?php
use 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`](api/core/plug/identify) or [`anonymize`](api/core/plug/anonymize) explicitly.

> **Good to know**
>
> Resolving the user from the framework's authentication and locale is handled automatically by our framework integrations, such as the [Laravel](/reference/sdk/laravel/integration), [Symfony](/reference/sdk/symfony/integration), and [Drupal](/reference/sdk/drupal/integration) SDKs.

## Explore

- [Audiences](/explanation/audience): Understand how to deliver content to specific groups of users.
- [CQL](/reference/cql/syntax): Get the basics on how to write conditions to define audiences.
