Data collection

Learn how to collect information for personalization.

This guide explains how to associate the visitor with a user from your site, 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 templates with the croct Twig filter.

Identity

When your site has authenticated users, you can link the visitor session with the logged-in user so you can recognize them when they return.

Automatic reconciliation

The module reconciles the visitor with the authenticated Drupal user on every request, using the user ID 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 identified

Custom identifier

By default, the module uses the Drupal user ID. To reconcile with a different identifier, such as the email, implement the identity resolver interface:

web/modules/custom/my_module/src/EmailIdentityResolver.php
1234567891011121314151617181920212223
<?php
declare(strict_types=1);
namespace Drupal\my_module;
use Croct\Plug\IdentityResolver;use Drupal\Core\Session\AccountInterface;
final class EmailIdentityResolver implements IdentityResolver{    private AccountInterface $account;
    public function __construct(AccountInterface $account)    {        $this->account = $account;    }
    public function getUserId(): ?string    {        return $this->account->isAnonymous() ? null : $this->account->getEmail();    }}

Then override the resolver service to use your class in your module’s *.services.yml:

web/modules/custom/my_module/my_module.services.yml
services:    Drupal\croct\AccountIdentityResolver:        class: Drupal\my_module\EmailIdentityResolver        arguments: ['@current_user']

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 templates with the croct Twig filter.

For example, to record the plan a user is considering on the pricing page:

{% apply croct %}    const selector = document.getElementById('plan-selector');
    selector.addEventListener('change', () => {        croct.session.edit()            .set('plan', selector.value)            .save();    });{% endapply %}

See Data collection for the full client-side API.

Locale detection

The module detects the visitor locale from Drupal’s language negotiation automatically and forwards it as the preferred locale, so content is returned in the matching language out of the box.

For full control, implement the locale resolver interface. For example, to serve content in each user’s preferred language instead of the negotiated one:

web/modules/custom/my_module/src/AccountLocaleResolver.php
1234567891011121314151617181920212223
<?php
declare(strict_types=1);
namespace Drupal\my_module;
use Croct\Plug\LocaleResolver;use Drupal\Core\Session\AccountInterface;
final class AccountLocaleResolver implements LocaleResolver{    private AccountInterface $account;
    public function __construct(AccountInterface $account)    {        $this->account = $account;    }
    public function getLocale(): ?string    {        return $this->account->getPreferredLangcode() ?: null;    }}

Then override the resolver service to use your class in your module’s *.services.yml:

web/modules/custom/my_module/my_module.services.yml
services:    Drupal\croct\LanguageManagerLocaleResolver:        class: Drupal\my_module\AccountLocaleResolver        arguments: ['@current_user']