# TikTok-style For You feed

This recipe builds a For You feed like TikTok's: one personalized feed per user that mixes posts from people they follow, popular posts and posts that match their interests. It ranks them by engagement, freshness and what the user does in your app, and gives new posts and creators a chance to be seen.

Everything here uses the built-in `foryou` feed group:

- Each user reads their own feed, `foryou:<user id>`.
- Activity selectors decide which posts are candidates for the feed, and the ranking decides their order.
- The feed learns from each user: their reactions and comments, how long they stop on a post, and their "show more" and "show less" feedback.

>
> **Info:** The exploration slot, long dwells and activity metrics (steps 1 and 6) need a paid Feeds plan. See [pricing](https://getstream.io/activity-feeds/pricing/).
>

## 1. Configure the For You feed group

Set the `foryou` group's activity selectors and ranking from your server. This configuration uses three selectors, the maximum for a feed group:

- `following`: posts from people the user follows.
- `popular`: posts with a popularity of at least 5.
- `interest`: posts whose interest tags match the user's interests. It takes the place of the group's default `follow_suggestion` selector.

`following` merges the feeds the user follows and caches the result, so it's cheap to read. `popular` and `interest` are computed when the feed is read (`popular` from a short-lived shared cache), so each selector only looks at posts from the last two days, which keeps that work small. Don't add computed selectors you don't need. See [Activity selectors](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-selectors/).

For the ranking, the `for_you_v2` preset is a formula Stream maintains, so you don't write one. It ranks by engagement, the user's show more and show less feedback and freshness. It lifts posts from people the user follows and from authors the user often engages with, and demotes posts many users hid. The configuration also turns on:

- **Exploration**: one recent post that few users have seen yet goes in position 15 of each user's feed, so new posts and creators get a chance.
- **Long dwells**: when a user stops on a post for 5 seconds or more, it counts as engagement with the post's author.

The `activity_filter` setting keeps users' own posts out of their For You feed.

See [For You ranking](https://getstream.io/activity-feeds/docs/node/for-you-ranking/) for the preset's formula and every option.

>
> **Warning:** Updating a feed group replaces most of its configuration. Activity selectors, default visibility and default follower role are kept when you leave them out, but every other setting you leave out, such as `ranking`, is reset. Send every setting you want the group to have.
>

```csharp label="C#"
await _feedsV3Client.UpdateFeedGroupAsync("foryou", new UpdateFeedGroupRequest
{
    ActivitySelectors = new List<ActivitySelectorConfig>
    {
        new() { Type = "following", CutoffWindow = "2d" },
        new() { Type = "popular", CutoffWindow = "2d" },
        new() { Type = "interest", CutoffWindow = "2d" }
    },
    // Keep users' own posts out of their For You feed
    ActivityFilter = new ActivityFilterConfig { ExcludeOwnerActivities = true },
    Ranking = new RankingConfig
    {
        Type = "expression",
        ForYou = new ForYouConfig
        {
            Enabled = true,
            Preset = "for_you_v2",
            Exploration = new ForYouExplorationConfig { Enabled = true, Slot = 15 },
            Dwell = new ForYouDwellConfig { Enabled = true, ThresholdMs = 5000 }
        }
    }
});
```

## 2. Tag posts with interests

The `interest` selector and show more and show less feedback work on a post's `interest_tags`. Set them when the post is created. For a video post, upload the video first, then add it to the activity's attachments. See [File uploads](https://getstream.io/activity-feeds/docs/dotnet-csharp/file-uploads/).

```csharp label="C#"
await _feedsV3Client.AddActivityAsync(new AddActivityRequest
{
    Feeds = new List<string> { "user:maria" },
    Type = "post",
    Text = "30 second pasta",
    InterestTags = new List<string> { "cooking", "pasta" },
    UserID = "maria"
});
```

To tag posts automatically instead, use the `text_interest_tags` [activity processor](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-processors/), which reads the post's text. Add it to the feed group users post to, for example `user`, not to `foryou`.

## 3. Seed interests at onboarding

Stream works out each user's interests from the posts they react to, so a new user has none yet. Ask new users what they like during onboarding, and save their answers as interests before they read their first feed. The `interest` selector uses a user's top 5 interests with a positive weight. Weights run from -1 to 1, but a negative weight (a dislike) has no effect on this feed: only [interest ranking](https://getstream.io/activity-feeds/docs/node/interest-ranking/) uses it. See [User interests](https://getstream.io/activity-feeds/docs/dotnet-csharp/user-interests/).

```csharp label="C#"
var response = await _feedsV3Client.UpsertUserInterestsAsync("jack", new UpsertUserInterestsRequest
{
    Interests = new List<UserInterestRequest>
    {
        new() { Tag = "cycling", Weight = 0.8 },
        new() { Tag = "jazz" }, // weight defaults to 1.0
    },
});
```

## 4. Follow from the For You feed

The `following` selector reads the feeds that `foryou:<user id>` follows, not the ones the user's timeline follows. When a user follows someone, follow from both feeds: the timeline for a "Following" tab, and the For You feed so that person's posts are candidates there too. Follower counts are kept per feed, so a user who follows from both feeds adds 2 to the `follower_count` of the followed `user` feed. If you show follower counts, keep that in mind.

If your app already has users, also create the `foryou` follows for the people they already follow. Until then, their For You feeds get nothing from the `following` selector.

```csharp label="C#"
// Jack follows Maria from both feeds in one request
await _feedsV3Client.GetOrCreateFollowsAsync(
    new FollowBatchRequest
    {
        Follows = new List<FollowRequest>
        {
            new FollowRequest { Source = "timeline:jack", Target = "user:maria" },
            new FollowRequest { Source = "foryou:jack", Target = "user:maria" }
        }
    }
);
```

## 5. Show the feed

Read the user's For You feed and render each activity as a full-screen post. Load the next page as the user scrolls. Full-screen posts fill one screen each, so a page of 10 is plenty, and keep the posts you already have when the app comes back to the foreground instead of reading the first page again. Pagination tokens expire after about an hour: if loading the next page fails with `pagination token expired`, read the first page again.

```js label="JavaScript"
const forYouFeed = client.feed("foryou", "jack");

await forYouFeed.getOrCreate({
  limit: 10,
});
console.log(forYouFeed.state.getLatestValue().activities);

// Load the next page when the user scrolls to the end
await forYouFeed.getNextPage();
```

Set `include_reasons: true` in the enrichment options to get, on each activity, short codes that explain why it's in the feed, for example `following`, `popular`, `interest:cooking` or `exploration`. You can show them in your app, for example as "Because you like cooking". Only set it on reads where you show the reasons, as with every enrichment option. See [Why am I seeing this](https://getstream.io/activity-feeds/docs/node/for-you-feed/#why-am-i-seeing-this).

## 6. Track impressions and time spent

The feed learns from what users actually see. With exploration on (step 1), a read of the For You feed returns a `request_id`, and a `position` on each activity. Send them with these events:

- **Impression**: once per activity per read, when the activity was on screen, for example at least half of it for half a second. Exploration uses impressions to stop picking posts that are shown a lot without getting engagement.
- **Dwell**: when the activity leaves the screen or the app goes to the background, with how long it was in view in `dwell_ms`. A dwell of 5 seconds or more is a long dwell.

```js label="JavaScript"
const feed = client.feed("foryou", "jack");
const response = await feed.getOrCreate();
const requestId = response.request_id;
const activity = response.activities[0];

// Once the activity has been on screen
await client.trackActivityMetrics({
  events: [
    {
      activity_id: activity.id,
      metric: "impressions",
      request_id: requestId,
      position: activity.position,
    },
  ],
});
```

Don't send a request per event. Collect events on the client and send them together, up to 100 per request, for example every few seconds and when the app goes to the background.

A dwell event looks like this:

```json
{
  "activity_id": "activity_123",
  "metric": "dwell",
  "dwell_ms": 6400,
  "request_id": "<request_id of the feed read>",
  "position": 3
}
```

See [Tracking feed impressions](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-metrics/#tracking-feed-impressions) and [Dwell time](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-metrics/#dwell-time).

## 7. Let users steer their feed

Add "Show more like this", "Show less like this" and "Not interested" actions to each post. Each one is a call to `activityFeedback` with one flag:

- `show_more`: the feed favors posts with the same interest tags.
- `show_less`: the feed ranks posts with those tags lower, and leaves them out once the user has asked to see less of those tags at least 3 times, and more often than to see more.
- `hide` ("Not interested"): the post is hidden from all of the user's feeds, from the next read on.

```csharp label="C#"
await _feedsV3Client.ActivityFeedbackAsync("activity_123", new ActivityFeedbackRequest
{
    ShowMore = true, // or ShowLess, or Hide
    UserID = "jack",
});
```

Hides and "show less" also count against the post itself: once 3 or more users push a post away, the `for_you_v2` preset demotes it for everyone. See [Activity feedback](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-feedback/).

## Going further

- **Rank by watch time.** On Enterprise plans, add a custom activity metric such as `watch_seconds`, with an hourly limit sized in seconds (for example 3600), and track the seconds watched as its `delta`. Then copy the `for_you_v2` preset's `score`, `functions` and `defaults` into the ranking and add a term for `metrics.watch_seconds`. The built-in `dwell` event can't be used for this, because it isn't added to an activity's `metrics`. See [Custom metrics configuration](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-metrics/#custom-metrics-configuration) and [Using metrics in ranking](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-metrics/#using-metrics-in-ranking).
- **Tune the ranking.** A `score` you set takes precedence over the preset. Copy the preset's `score`, `functions` and `defaults` into the ranking and change them (the score calls the preset's `for_you_decay` function), for example to rank by the user's interests with `interest_score`, which needs the ranking `type` set to `interest`. See [For You ranking](https://getstream.io/activity-feeds/docs/node/for-you-ranking/#presets) and [Interest ranking](https://getstream.io/activity-feeds/docs/node/interest-ranking/).
- **Keep one creator from filling the feed.** Use [feed arrangement](https://getstream.io/activity-feeds/docs/node/feed-arrangement/) in `diversify` mode to allow at most a few posts per author in any run of consecutive positions, for example 2 in any 10.
- **Show what friends like.** Replace the `popular` or `interest` selector with the [`friends_engaged` selector](https://getstream.io/activity-feeds/docs/dotnet-csharp/activity-selectors/#friends-engaged-activity-selector) to add posts the user's close friends reacted to.
- **Test changes.** Compare ranking configurations with [experiments and analytics](https://getstream.io/activity-feeds/docs/node/for-you-experiments/), and boost trending topics with [trending topics](https://getstream.io/activity-feeds/docs/node/trending-topics/).

---

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