# For You ranking

The `for_you` block of a ranking config turns on ranking features built for the `foryou` feed group: ready-made ranking presets, ranking variables that learn from each user's behavior, an exploration slot for new content, and long-dwell tracking. Everything in it is opt-in. A group without `for_you` ranks exactly as before.

For the basics of a "for you" style feed (selectors, interest tags and reasons), start with the [For You feed](https://getstream.io/activity-feeds/docs/java/for-you-feed/) guide.

## Turning it on

Set `ranking.for_you.enabled` on the `foryou` feed group. The example below uses the `for_you_v2` preset instead of writing a `score`, and also turns on the exploration slot and long dwells. Updating a feed group replaces its configuration, so the example sends the activity selectors too, each with the same 2 day cutoff window the group's default selectors use.

```java label="Java"
feeds.updateFeedGroup("foryou", UpdateFeedGroupRequest.builder()
    .activitySelectors(List.of(
        ActivitySelectorConfig.builder().type("following").cutoffWindow("2d").build(),
        ActivitySelectorConfig.builder().type("popular").cutoffWindow("2d").build(),
        ActivitySelectorConfig.builder().type("interest").cutoffWindow("2d").build()))
    .ranking(RankingConfig.builder()
        .type("expression")
        .forYou(ForYouConfig.builder()
            .enabled(true)
            .preset("for_you_v2")
            .exploration(ForYouExplorationConfig.builder().enabled(true).slot(15).build())
            .dwell(ForYouDwellConfig.builder().enabled(true).thresholdMs(5000).build())
            .build())
        .build())
    .build()).execute();
```

Rules for the `for_you` block:

- On a feed group it is accepted only on the `foryou` group, and only when that group is not aggregated.
- A [feed view](https://getstream.io/activity-feeds/docs/java/feed-views/) can carry a `for_you` block too. It applies only when the view is read on the `foryou` group, and is ignored elsewhere.
- `enabled: false` turns every `for_you` feature off, whatever else the block contains.

| Field         | Description                                                                                                                | Default |
| ------------- | -------------------------------------------------------------------------------------------------------------------------- | ------- |
| `enabled`     | Turns For You ranking on. Required by every other field.                                                                   | `false` |
| `preset`      | A ready-made ranking formula used when `score` is empty: `for_you_v2` or `hot`. See [Presets](#presets).                   | -       |
| `exploration` | The exploration slot. See [Exploration](#exploration).                                                                     | off     |
| `dwell`       | Long-dwell tracking. See [Long dwells](#long-dwells).                                                                      | off     |
| `trending`    | Trending topics and the `topic_trend` variable. See [Trending topics](https://getstream.io/activity-feeds/docs/java/trending-topics/).   | `false` |
| `analytics`   | Engagement analytics per feed view. See [Experiments and analytics](https://getstream.io/activity-feeds/docs/java/for-you-experiments/). | `false` |
| `experiment`  | A/B test between feed views. See [Experiments and analytics](https://getstream.io/activity-feeds/docs/java/for-you-experiments/).        | -       |

## Presets

A preset is a ranking formula Stream maintains, so you don't have to write and tune a `score` yourself. Set `preset` and leave `score` empty.

| Preset       | What it ranks for                                                                                                                                                                                                                                                                                                                                  |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `for_you_v2` | Engagement (reactions, comments, shares and bookmarks, each on a log scale), the user's show more/less feedback, freshness (half the weight after a day), a lift for posts from people the user follows or whose friends engaged, a penalty for posts many users hid or asked to see less of, and a boost for authors the user often engages with. |
| `hot`        | What is popular right now: reactions, comments and shares on a log scale, with a 12 hour half-life.                                                                                                                                                                                                                                                |

The `for_you_v2` formula:

```text
(ln(1 + reaction_count) + 3 * ln(1 + comment_count) + 4 * ln(1 + share_count) + 2 * ln(1 + bookmark_count) + 4 * preference_score)
  * decay_exp(time)  // scale 1d, offset 2h, decay 0.5
  * (selector_source == "following" || selector_source == "friends_engaged" ? 1.0 : 0.75)
  * (neg_feedback >= 3 ? 1.0 - min(0.5, 0.1 * neg_feedback) : 1.0)
  * (1 + 0.5 * author_affinity)
```

The `hot` formula:

```text
ln(1 + reaction_count + 2 * comment_count + 3 * share_count) * decay_exp(time)  // scale 12h, offset 1h, decay 0.5
```

- A `score` you set takes precedence over the preset. Copy a preset's formula into `score` to tune it.
- With a preset and no `score`, `functions` and `defaults` are rejected, because the preset brings its own.
- An unknown preset name is rejected.
- Set `include_ranking_config: true` on a server-side feed read to see the expanded formula the feed was ranked with.

## For You ranking variables

These variables can be used in `score` only when the same ranking has `for_you.enabled`. A config that uses one without it is rejected.

| Variable             | Value                                                                                                                                                                                                                                                                                                                                |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `neg_feedback`       | How many users currently hide the activity or chose to see less of it, through [activity feedback](https://getstream.io/activity-feeds/docs/java/activity-feedback/).                                                                                                                                                                              |
| `impressions_bucket` | How many times the activity was shown in For You feeds, as `floor(log2(impressions))`: 0 for 1 impression, 3 for 8 to 15, 10 for 1024 to 2047. Counts impressions sent with a `request_id` within a 48 hour window, and never goes down. Updated about once a minute. See [Feed reads and impressions](#feed-reads-and-impressions). |
| `author_affinity`    | 0 to 1: how much the user engages with the activity's author, from the user's comments (weighted 3), reactions (2), bookmarks (1) and long dwells (1) in the last 30 days, with recent engagement counting more. Any author counts, followed or not.                                                                                 |
| `is_mutual`          | 1 when the user and the activity's author follow each other, else 0.                                                                                                                                                                                                                                                                 |
| `friends_engagement` | How many of the user's close friends reacted to the activity. Read from the [`friends_engaged` selector](https://getstream.io/activity-feeds/docs/java/activity-selectors/#friends-engaged-activity-selector); 0 without it.                                                                                                                       |
| `topic_trend`        | 0 to 1: how much the activity's topics are trending. Needs `for_you.trending`. See [Trending topics](https://getstream.io/activity-feeds/docs/java/trending-topics/).                                                                                                                                                                              |

- `neg_feedback` and `impressions_bucket` are internal: they shape the ranking but are not returned in `score_vars` or anywhere else in the API.
- `author_affinity`, `is_mutual` and `friends_engagement` are per user. A ranking that uses them is computed per user, like `interest_score`.
- A variable that cannot be loaded for a read (for example during an outage) reads 0 for that read. The feed still serves.

For example, to lift posts from authors the user engages with and from mutual follows:

```text
popularity * decay_exp(time) * (1 + author_affinity) * (1 + 0.5 * is_mutual)
```

>
> **Tip:** A boost on `author_affinity` can let one favourite author fill a page. Pair it with [`ranking.arrange`](https://getstream.io/activity-feeds/docs/java/feed-arrangement/) in `diversify` mode to cap how many posts per author a page shows.
>

## Exploration

The exploration slot puts one recent, under-exposed activity at a fixed position in each user's ranked feed, so new posts and new creators get a chance to be seen.

```json
{
  "for_you": { "enabled": true, "exploration": { "enabled": true, "slot": 15 } }
}
```

- `slot` is the 1-based position, 1 to 100. It defaults to 15.
- Candidates are recent activities from the last 24 hours, across the app. Activities that were already shown about 1000 times are skipped.
- The pick balances how many times an activity was shown (`impressions_bucket`) against its engagement, so a new post with few impressions gets a chance, and one that keeps getting shown without engagement stops being picked.
- The pick is per user and stays the same for about 10 minutes, then a new one is picked. It never repeats an activity the user's feed already contains, the user's own activities, or activities the user hid.
- The picked activity has `selector_source: "exploration"` and the `exploration` [reason](https://getstream.io/activity-feeds/docs/java/for-you-feed/#why-am-i-seeing-this).
- There is no exploration slot on reads with a request `filter` or `filter_tags`, or when a selector uses a filter or a feed group scope.

>
> **Info:** Exploration requires a **paid Feeds plan** and an authenticated user.
>

Exploration learns from impressions, so send impressions with the feed's `request_id`, as described below.

## Long dwells

A long dwell is a `dwell` event at or above the threshold, meaning the user stopped on the activity. Long dwells count toward `author_affinity`, so a user who reads an author's posts without reacting to them still gets more of that author.

```json
{
  "for_you": {
    "enabled": true,
    "dwell": { "enabled": true, "threshold_ms": 5000 }
  }
}
```

`threshold_ms` is 1000 to 60000 and defaults to 5000. Each user's last 20 long dwells of the past 48 hours are kept, and are deleted with the user's data. Send dwell events as described in [Activity metrics](https://getstream.io/activity-feeds/docs/java/activity-metrics/#dwell-time).

>
> **Info:** Long dwells require a **paid Feeds plan**.
>

## Feed reads and impressions

When exploration, analytics or a ranking using `impressions_bucket` is on, a feed read by an authenticated user on a paid plan returns:

- `request_id`: a signed token for that read, valid for 48 hours.
- `position` on each activity, only when exploration or `impressions_bucket` is on: its 1-based position in the ranked feed, continuing across pages.

Send the `request_id`, and the activity's `position`, with the `impressions`, `clicks` and `dwell` events you track for activities from that read. See [Activity metrics](https://getstream.io/activity-feeds/docs/java/activity-metrics/#tracking-feed-impressions). When exploration or `impressions_bucket` is on, impressions sent this way feed them instead of the activity's `metrics.impressions`.

## Friends and authors

- The [`friends_engaged` selector](https://getstream.io/activity-feeds/docs/java/activity-selectors/#friends-engaged-activity-selector) adds posts the user's close friends reacted to, whoever wrote them. Activities from it get the `liked_by_friends:<N>` reason.
- With `author_affinity` in `score`, activities by authors the user engages with a lot (`author_affinity` of at least 0.5) get the `you_often_engage` reason.

See [Why am I seeing this](https://getstream.io/activity-feeds/docs/java/for-you-feed/#why-am-i-seeing-this) for every reason code.

## Caching

Ranked feeds keep their order for a short time after a config change, the same as after any `score` edit. A change shows on every page once the cached rankings expire.

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/java/for-you-ranking/](https://getstream.io/activity-feeds/docs/java/for-you-ranking/).