# Trending topics

Trending topics finds the topics whose share of your app's new posts is spiking, lets you promote topics by hand (for a live event, a campaign or breaking news), and makes both available to ranking as the `topic_trend` variable and to your app as a list of trending topics per user.

Topics are the activities' `interest_tags`. Set them yourself or let the [`text_interest_tags` activity processor](https://getstream.io/activity-feeds/docs/php/activity-processors/) set them.

## Turning it on

Set `for_you.trending` on the `foryou` feed group and use `topic_trend` in `score`. It needs `for_you.enabled`; see [For You ranking](https://getstream.io/activity-feeds/docs/php/for-you-ranking/) for the full update call.

```json
{
  "ranking": {
    "type": "expression",
    "score": "popularity * decay_exp(time) * (1 + topic_trend)",
    "for_you": { "enabled": true, "trending": true }
  }
}
```

Topics are counted from the moment `trending` is on. Posts created before that are not counted.

## How trends are detected

For each topic, Stream compares its share of the app's posts in the last few hours with its usual share over the last weeks. A topic whose share rises to several times its usual share is trending.

- `topic_trend` is 0 to 1: the highest trend among the activity's interest tags. 0 when no tag is trending or the activity has no tags. A tag that holds 5 times its usual share reads about 0.5 after an hour and about 0.85 after six, then falls as the new level becomes usual. In a busy app, a brand-new tag with a burst of posts reads 1.
- A tag needs about 5 recent posts before it can trend, and only the 50 tags with the most recent posts are considered.
- Changes that affect every topic alike, like a daily peak or a quiet weekend, don't make anything trend. Only a change in the mix of topics does.
- Only public activities count, and each post counts once, when it is created. Edits don't count again.
- `topic_trend` is the same for every user, so it works with shared ranked caches and the [hot feed cache](https://getstream.io/activity-feeds/docs/php/hot-feed-cache/).

## Promoting topics

A promotion gives its tags a fixed trend, its `weight`, until it expires. An activity's `topic_trend` is the highest of its detected trends and the weights of the active promotions that contain one of its tags.

```php label="PHP"
// Promote for one hour
$response = $feedsClient->promoteTopics(new GeneratedModels\PromoteTopicsRequest(
    tags: ['world cup', 'football'],
    weight: 0.8,
    ttl: 3600,
));
$promotionId = $response->getData()->promotedTopic->id;

// Active promotions, oldest first
$promotions = $feedsClient->listPromotedTopics()->getData()->promotedTopics;

// End a promotion early
$feedsClient->unpromoteTopics($promotionId);
```

| Field    | Description                                                                 | Default |
| -------- | --------------------------------------------------------------------------- | ------- |
| `tags`   | 1 to 10 tags, each at most 255 characters                                   | -       |
| `weight` | The trend the tags get while the promotion is active, above 0 and at most 1 | `1`     |
| `ttl`    | How long the promotion lasts, in seconds, 60 to 86400 (24 hours)            | -       |

- An app can have at most 20 active promotions.
- Promoting needs `for_you.trending` on a feed group or view. Listing and ending promotions keep working after it is turned off.
- Ending a promotion that is missing or already expired returns 404.
- These endpoints are server-side only.
- A new or ended promotion reaches ranking within a few seconds. Feeds already ranked for a user keep their order until their cache expires.

## Listing trending topics for a user

Show users what is trending, for example as topic chips above the feed. The list contains detected trends and active promotions, weighted by the user's interest in each topic.

```php label="PHP"
$response = $feedsClient->getTrendingTopics('jack', 10);
foreach ($response->getData()->topics as $topic) {
    echo $topic->tag . ' ' . $topic->score . PHP_EOL;
}
```

- `score` is the topic's trend multiplied by `1 +` the user's [interest](https://getstream.io/activity-feeds/docs/php/user-interests/) weight in it. Topics the user dislikes (a negative weight) are left out.
- `promoted` is true when an active promotion contains the tag.
- `limit` defaults to 10, at most 50. The list is not paginated.
- Client-side, a user can only read their own list. The list is empty when no feed group or view has `for_you.trending`.

## Real-time updates

A client watching a `foryou` feed receives a `feeds.promoted_topics.updated` WebSocket event when a promotion is created or ended early with `unpromoteTopics`. The event carries the app's active promotions, each with its `tags` and `expires_at`, so the app can update its topic chips without polling. Promotions that expire send no event; use `expires_at` to drop them on the client. There is no webhook for this event.

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/php/trending-topics/](https://getstream.io/activity-feeds/docs/php/trending-topics/).