> For the complete documentation index, see [llms.txt](https://user.netmera.com/netmera-developer-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://user.netmera.com/netmera-developer-guide/api-documentation/recipes/showing-popularity-counts.md).

# Showing Popularity Counts with the Favorite Count API

Count the users that share a profile attribute value, for one value or for many values in a single request.

This recipe shows how to read the number of unique users whose profile contains a specific value, for example how many users selected a brand or a category. You can use the counts for social proof in your app without exporting user lists or building your own aggregation.

## When to Use

This recipe fits experiences such as "Favorited by X users" on a detail page, popularity indicators for a product, brand, or content item, and interest insights for analytics or in-app messaging.

## Prerequisites

* REST API key of the application
* A profile attribute that stores the values you want to count, for example an array attribute `favoriteBrands`. The attribute must be defined and active in the Netmera panel.
* The values stored in that attribute, for example the IDs of the brands

## Step 1: Count the Users of a Single Value

Send the condition to `targeting/count` in the `target.profile` object. The request uses the standard targeting format of Netmera.

```bash
curl https://restapi.netmera.com/rest/3.0/targeting/count \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'X-netmera-api-key: <API_KEY>' \
  --data '{
  "target": {
    "profile": {
      "favoriteBrands": ["1001"]
    }
  }
}'
```

The response contains the number of distinct users that match:

```json
{
  "count": 184523
}
```

When you send several values for the same attribute, the count includes the users who match at least one of them. To count the users who do not have an attribute, use the `$exists` operator with `false`, for example `"name": { "$exists": false }`.

**Expected result:** A `count` field with the number of unique users.

## Step 2: Count Many Values in One Request

When you need one figure per value, such as a popularity count per brand, call `targeting/count-batch` instead of sending one request per value.

```bash
curl https://restapi.netmera.com/rest/3.0/targeting/count-batch \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'X-netmera-api-key: <API_KEY>' \
  --data '{
  "groupBy": { "profile": "favoriteBrands" },
  "values": ["1001", "1002", "1003"]
}'
```

`groupBy` is required and must contain exactly one of `profile`, with a profile attribute code, or `installation`, with a device attribute such as `platform`. Without `values`, the response returns every value of the attribute. For numeric attributes, `groupBy.buckets` returns counts per range, for example `"buckets": [[18, 24], [25, 34]]`.

The response contains the count per value:

```json
{
  "counts": {
    "1001": 184523,
    "1002": 9210,
    "1003": 4487
  }
}
```

**Expected result:** A `counts` object with one entry per requested value.

## Step 3: Plan for Data Freshness

Counts are computed with aggregation and caching, so they are near-real-time and not real-time. The maximum delay is 2 to 3 hours, which is sufficient for popularity and social proof use cases. Query several values in one request, avoid frequent polling for the same entity, and cache the counts in your own system when you show them often.

## Verification Checklist

* The profile attribute is defined and active in the Netmera panel
* The values in the request match the values stored in the profiles
* `groupBy` contains exactly one of `profile` or `installation`; otherwise the request is rejected with `400`
* Counts shown in your app are cached on your side, and a delay of up to 3 hours is acceptable
* Only counts are returned; user lists are not exposed

## Related Pages

For all options and the error codes of the endpoints, see [Favorite Count & Social Proof](/netmera-developer-guide/api-documentation/rest-api/favorite-count-and-social-proof.md) in the REST API documentation. To store the values that you count, see [Registering Users and Building a Profile](/netmera-developer-guide/api-documentation/recipes/registering-users-and-profiles.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://user.netmera.com/netmera-developer-guide/api-documentation/recipes/showing-popularity-counts.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
