# Data collection

Learn how to collect information for personalization.

This guide provides practical examples of using the Hydrogen SDK to enrich user profiles and sessions with information relevant to your business.

## User data

You can persist user-related information in one of the two ways described below depending on whether it is related to the user's profile or their current session.

### Profile data

You can enrich user profiles with details collected via forms, surveys, or other means.

These pieces of information, called *attributes*, are categorized as *standard* or *custom* depending on whether it is generic or specific to your organization.

#### Standard attributes

These are [predefined attributes](/reference/user/standard-attributes) that are common most businesses, such as the user's name, email, and interests.

You can set standard attributes using the [`user.edit`](/reference/sdk/javascript/api/user/edit) method. For example, let's say you have a newsletter subscription form, and you want to add the email address to the user profile on Croct.

This is what it would look like:

**Updating user profiles**

**JavaScript**

```jsx
import type {FormEvent} from 'react';
import {useCroct} from '@croct/plug-hydrogen';

export function NewsletterForm() {
const croct = useCroct();
const subscribe = event => {
  const form = new FormData(event.currentTarget);
  const email = form.get('email');

  // Your logic to persist the email
  api.subscribe(email);

  croct.user.edit()
    .set('email', email)
    .save();
};

return (
  <form onSubmit={subscribe}>
    📫 Subscribe to our newsletter!
    <input type="email" placeholder="Email"/>
    <button type="submit">Subscribe</button>
  </form>
);
}
```

**TypeScript**

```tsx
import type {FormEvent, ReactElement} from 'react';
import {useCroct} from '@croct/plug-hydrogen';

export function NewsletterForm(): ReactElement {
const croct = useCroct();
const subscribe = (event: FormEvent<HTMLFormElement>): void => {
  const form = new FormData(event.currentTarget);
  const email = String(form.get('email'));

  // Your logic to persist the email
  api.subscribe(email);

  croct.user.edit()
    .set('email', email)
    .save();
};

return (
  <form onSubmit={subscribe}>
    📫 Subscribe to our newsletter!
    <input type="email" placeholder="Email"/>
    <button type="submit">Subscribe</button>
  </form>
);
}
```

The [`user.edit`](/reference/sdk/javascript/api/user/edit) method returns a [`Patch`](/reference/sdk/javascript/api/patch) instance that allows you to chain multiple operations together.

You could alternatively write:

```tsx
const patch = croct.user.edit();
patch.set('email', email);

await patch.save();
```

When you call [`save`](/reference/sdk/javascript/api/patch/save), it saves changes to the user profile and returns a promise that confirms the operation.

You can then access standard attributes in your queries using the [`user`](/reference/cql/context#user) variable:

```cql
user's email ends with "@croct.com"
```

For a complete list of attributes, see [User profile reference](/reference/cql/data-types/user/user).

#### Custom attributes

In addition to the standard attributes, you can add [custom attributes](/reference/user/custom-attributes) to enrich user profiles with information relevant to your business.

For example, let's say you have a SaaS application and you want to ask your users about their area of expertise so you can personalize the onboarding experience.

This is what it would look like:

**JavaScript**

```jsx
import {useCroct} from '@croct/plug-hydrogen';

export function ExpertiseSelector() {
const croct = useCroct();

const onChange = event => {
  croct.user.edit()
    .set('custom.expertise', event.target.value)
    .save();
};

return (
  <select onChange={onChange}>
    <option value="Engineering">Engineering</option>
    <option value="Design">Design</option>
    <option value="Marketing">Marketing</option>
  </select>
);
}
```

**TypeScript**

```tsx
import type {ReactElement, ChangeEvent} from 'react';
import {useCroct} from '@croct/plug-hydrogen';

export function ExpertiseSelector(): ReactElement {
const croct = useCroct();

const onChange = (event: ChangeEvent<HTMLSelectElement>): void => {
  croct.user.edit()
    .set('custom.expertise', event.target.value)
    .save();
};

return (
  <select onChange={onChange}>
    <option value="Engineering">Engineering</option>
    <option value="Design">Design</option>
    <option value="Marketing">Marketing</option>
  </select>
);
}
```

Note that custom attributes are prefixed with `custom` to avoid conflicts with standard attributes. However, you do not need to include the prefix in your queries:

```cql
user's expertise is "Engineering"
```

### Session data

Besides user profiles, you can also store information relevant to the user's current session.

Storing session information can help you keep track of important the user's current session that may not be relevant to their profile. For example, you can store the plan the user selected on the pricing page, or any other information you want to record during the user's visit.

To store session information, use the [`session.edit`](/reference/sdk/javascript/api/session/edit) method:

**Saving session data**

**JavaScript**

```jsx
import {useCroct} from '@croct/plug-hydrogen';

export function PlanCard({plan, price}) {
const croct = useCroct();

const onSelect = () => {
  croct.session.edit()
    .set('plan', plan)
    .save();
};

return (
  <div>
    <strong>{plan}</strong>
    <p>${price}</p>
    <button onClick={onSelect}>Select</button>
  </div>
);
}
```

**TypeScript**

```tsx
import type {ReactElement} from 'react';
import {useCroct} from '@croct/plug-hydrogen';

type PlanCardProps = {
plan: string,
price: number,
}

export function PlanCard({plan, price}: PlanCardProps): ReactElement {
const croct = useCroct();

const onSelect = (): void => {
  croct.session.edit()
    .set('plan', plan)
    .save();
};

return (
  <div>
    <strong>{plan}</strong>
    <p>${price}</p>
    <button onClick={onSelect}>Select</button>
  </div>
);
}
```

In the same way as with user profiles, the [`session.edit`](/reference/sdk/javascript/api/session/edit) method returns a [`Patch`](/reference/sdk/javascript/api/patch) instance that allows you to chain multiple operations together.

```tsx
const patch = croct.session.edit();
patch.set('plan', plan);

await patch.save();
```

When you call [`save`](/reference/sdk/javascript/api/patch/save), it saves changes to the session and returns a promise that confirms the operation. You can then access session information in your queries using the [`session`](/reference/cql/context#session) variable:

```cql
session's plan is "premium"
```

For a complete list of attributes, see [Session reference](/reference/cql/data-types/session/web-session).

## User identity

By default, all visitors are anonymous, recognized across requests by a client ID. If your storefront has logged-in customers, you can link the Croct user profile with their Shopify customer ID so that personalization stays consistent across devices and sessions.

### Automatic handling

When your storefront uses [Shopify customer accounts](https://shopify.dev/docs/api/customer-accounts), the logged-in customer is identified on every request out of the box, reading the customer ID from the [Customer Account API](https://shopify.dev/docs/api/customer). No extra code is required.

The default behavior relies on the Customer Account API session, so visitors authenticated another way are not visible to it. To support them, provide your own [identity resolver](/reference/sdk/hydrogen/api/types/user-id-resolver), which reads the identity from your own source. This covers guest-only stores, the classic customer accounts on the [Storefront API](https://shopify.dev/docs/api/storefront), and your own or third-party authentication such as Auth0, SSO, or a custom backend. Return the logged-in user's ID, or nothing to keep the visitor anonymous.

Provide the resolver to the [middleware](/reference/sdk/hydrogen/api/setup/create-croct-middleware) on [React Router 7](https://reactrouter.com), or to the [context helper](/reference/sdk/hydrogen/api/setup/create-croct-context) on [Remix](https://remix.run):

**React Router 7**

```tsx
import {createCroctMiddleware} from '@croct/plug-hydrogen/server';

export const middleware = [
  createCroctMiddleware({
    // Return the logged-in user ID from your own auth, or null when anonymous.
    userIdResolver: context => resolveUserId(context),
  }),
];
```

**Remix**

```tsx
import {createCroctContext} from '@croct/plug-hydrogen/server';

const croct = await createCroctContext(request, hydrogenContext, {
  // Return the logged-in user ID from your own auth, or null when anonymous.
  userIdResolver: context => resolveUserId(context),
});
```

For a guest-only storefront, the resolver can return nothing so that every visitor stays anonymous.

### Manual handling

To control identity within a request, use the [`identify`](/reference/sdk/hydrogen/api/functions/identify) and [`anonymize`](/reference/sdk/hydrogen/api/functions/anonymize) functions from a loader or action.

The following example identifies a customer when they log in:

**app/routes/login.tsx**

```tsx
import {redirect} from 'react-router';
import {identify} from '@croct/plug-hydrogen/server';
import type {Route} from './+types/login';

export async function action({request, context}: Route.ActionArgs) {
  const form = await request.formData();

  await identify(String(form.get('customerId')), context);

  return redirect('/account');
}
```

Anonymize the visitor after logout in the same way:

**app/routes/logout.tsx**

```tsx
import {redirect} from 'react-router';
import {anonymize} from '@croct/plug-hydrogen/server';
import type {Route} from './+types/logout';

export async function action({context}: Route.ActionArgs) {
  await anonymize(context);

  return redirect('/');
}
```

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