> ## Documentation Index
> Fetch the complete documentation index at: https://docs.grainql.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Personalization

> User properties and configuration rules return a different value to each user.

Personalization returns a different configuration value to each user, based on the properties
of that user. Your code sends the properties. A rule in the dashboard evaluates them and returns
the matching value. Personalization uses Analytics Web (`@grainql/analytics-web`). GrainQL Tag has
no remote configuration. Read [Remote configuration](https://docs.grainql.com/core/remote-config.md) for the cache-first
flow.

## The two inputs

A rule reads properties from two places.

| Input | Method | Where the values live | When to use it |
| - | - | - | - |
| Stored user properties | `setProperty(properties, { userId? })` | On the user profile in GrainQL, for every later request | Properties that change rarely: plan, country, signup date. |
| Request properties | The `properties` option of `getConfigAsync`, `getAllConfigsAsync`, `fetchConfig`, `useConfig`, and `useAllConfigs` | In one request only | Properties that the page computes: days since signup, login count. |

`setProperty` converts every value to a string. The `properties` option accepts only strings
(`Record<string, string>`), so convert a number with `toString()` before you pass it. Rules
compare strings. Read [User properties](https://docs.grainql.com/core/user-properties.md).

Set the user ID before you set properties, so that the properties attach to the right user.

```typescript theme={null}
import { useEffect } from 'react';
import { useGrainAnalytics } from '@grainql/analytics-web/react';

function App({ user }) {
  const grain = useGrainAnalytics();

  useEffect(() => {
    grain.setUserId(user.id);
    grain.setProperty({
      plan: user.plan,
      country: user.country,
      signup_date: user.signupDate
    });
  }, [user, grain]);

  return <AppContent />;
}
```

Read [Identify users](https://docs.grainql.com/core/user-identification.md) for `setUserId`.

## A message by plan

The rule reads one property and returns one string per plan.

```typescript theme={null}
import { useConfig } from '@grainql/analytics-web/react';

function Hero({ plan }: { plan: string }) {
  const { value: message } = useConfig('hero_message', {
    properties: { plan }
  });

  return <h1>{message}</h1>;
}
```

In the dashboard, create the key `hero_message` with the default value `Welcome`. Then add one
rule per plan: if `plan` equals `premium`, return `Welcome back, premium member`. A user with no
matching rule receives the default.

## A price by country

Two keys share the same property. Each key has its own rules.

```typescript theme={null}
function Pricing({ country }: { country: string }) {
  const { value: currency } = useConfig('currency', { properties: { country } });
  const { value: price } = useConfig('monthly_price', { properties: { country } });

  return <p>{currency}{price} per month</p>;
}
```

Rules in the dashboard: if `country` equals `US`, `currency` returns `$` and `monthly_price`
returns `29`. If `country` equals `GB`, `currency` returns `£` and `monthly_price` returns `24`.
The price is a string. Parse it before you compute with it.

## A tutorial by login count

A computed number goes in the request as a string. The rule compares it as a number.

```typescript theme={null}
function Dashboard({ loginCount }: { loginCount: number }) {
  const { value: showTutorial } = useConfig('show_tutorial', {
    properties: { login_count: loginCount.toString() }
  });

  return (
    <>
      {showTutorial === 'true' && <Tutorial />}
      <MainContent />
    </>
  );
}
```

Rule in the dashboard: if `login_count` is less than `5`, `show_tutorial` returns `true`. The
default value is `false`.

## Feature access by plan

The value is not a flag but a label, so the code can show a different state for each label.

```typescript theme={null}
function AdvancedAnalytics({ plan }: { plan: string }) {
  const { value: access } = useConfig('analytics_access', {
    properties: { plan }
  });

  if (access !== 'granted') {
    return <UpgradePrompt />;
  }

  return <AnalyticsDashboard />;
}
```

Rule in the dashboard: if `plan` equals `premium` or `enterprise`, `analytics_access` returns
`granted`. The default value is `denied`. Read [Feature flags](https://docs.grainql.com/guides/feature-flags.md) for the
flag form of this pattern.

## Rules for the client

1. Set a default value for every personalized key. A user with no matching rule, and a client
   with no response yet, both receive the default.
2. Send only the properties that a rule reads. A property that no rule reads has no effect on
   the value.
3. Send only properties that the user consented to. Read
   [Privacy and consent](https://docs.grainql.com/essentials/privacy-and-compliance.md).
4. Test every rule with one user per branch: one per plan, one per country, one per side of a
   numeric limit.
5. To measure whether a personalized value converts better than the default, run it as an A/B
   test. Read [A/B tests](https://docs.grainql.com/guides/ab-testing.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.