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

# Custom conversion count metrics

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains how to create a custom conversion count metric in LaunchDarkly.

## About custom conversion count metrics

Custom conversion count metrics track how many times an event occurs. You can use this metric with any event you set up in your codebase.

For example, you can use custom conversion count metrics to track:

* How many times each user submitted a form
* How many times each customer completed the checkout process
* How many errors a new process produced

<Warning>
  **Call track and flush when an end user is redirected**

  SDKs send events at regular intervals, such as every five seconds. If a browser redirects an end user to another page, any events that occurred between when the SDK last sent events and when the browser redirected are lost. To avoid this, call track and then flush when you know the browser redirects an end user to another page, such as on click. To learn more, read [Tracking custom events](/docs/sdk/features/events) and [Flushing events](/docs/sdk/features/flush).
</Warning>

### Example metric in SQL

Some customers find it helpful to think about LaunchDarkly metric calculations in terms of SQL statements. The following example shows how a custom conversion count metric could be represented as a SQL expression, using example table and column names to represent metric components:

<CodeGroup>
  ```sql title="Custom conversion count metric" lines wrap theme={null}
  WITH user_aggregates AS (
     SELECT user_id, COUNT(1) AS value
     FROM events
     GROUP BY user_id
  )
  SELECT aggregation_func(value)
  FROM user_aggregates;
  ```
</CodeGroup>

In this example:

* `events` is a table that stores all of the metric events on which the metric is defined.
* `user_id` is a table column that represents the context both for the metric analysis unit and for the randomization unit used in the experiment or guarded rollout. This example uses the "user" context kind, but a metric can use other context kinds, such as "account," "session," or "device."
* `aggregation_func` represents the aggregation function that corresponds to the analysis method you select, such as `AVG` for average analysis or `PERCENTILE_CONT` for percentile analysis.

## Metric definition

When you configure the metric definition for a custom conversion count metric, you first select **Count** as the aggregation type for your measurement, and then select one or more context kinds for which you will count events.

You then choose from options to define the analysis method and success criteria:

* Analysis method:
  * **Average**: "average" is the default analysis method. This method calculates the average number of conversions per context. For metrics you plan to use in funnel metric groups, you must select "Average."
  * **P50** to **P99**: to use percentile analysis, choose between P50-P99. The options represent the 50th through the 99th percentile. This method counts the conversions per context, then finds the number of conversions that fall into the chosen percentile.
* Success criteria:
  * **higher is better**: choose this option for metrics measuring positive things like cart checkouts or sign-ups.
  * **lower is better**: choose this option for metrics measuring negative things like errors.

To learn more about the metric definition options, read [Metric components](/docs/home/metrics/components).

## Create custom conversion count metrics

Custom conversion count metrics require an event key from your application's code to track conversions for any event in your code. To add a custom conversion count metric in LaunchDarkly, you must identify it with a code snippet embedded in your app.

This is an example of tracking a `custom` event:

<CodeGroup>
  ```java title="Example event" lines wrap theme={null}
  client.track("example-event-key", context);
  ```
</CodeGroup>

The area of your code you should put custom conversion count metric information into, and the type of information you should include, varies based on which SDKs you use. Often, the information you should put in your code includes the event key, context object, data field, or numeric value fields.

<Warning>
  **Event keys and metric keys are different**

  Sending custom events to LaunchDarkly requires a unique **event key**. Set the event key to anything you want. Adding this event key to your codebase lets your SDK track actions customers take in your app as events. To learn more, read [Tracking custom events](/docs/sdk/features/events).

  LaunchDarkly also automatically generates a **metric key** when you create a metric. You only use the metric key to identify the metric in API calls. To learn more, read [Creating and managing metrics](/docs/home/metrics/create-metrics).
</Warning>

<Card icon="rectangle-terminal" horizontal>
  Try it in your SDK: [Tracking custom events](/docs/sdk/features/events)
</Card>

To create a custom conversion count metric:

1. Open the **Data** section and navigate to the **Metrics** list.

2. Click **Create metric**. The "Create metric" dialog appears.

3. If you use [warehouse native metrics](/docs/home/warehouse-native/metrics), you can select either **LaunchDarkly hosted** or **Warehouse native**.
   * Select **LaunchDarkly hosted** to measure events from LaunchDarkly SDKs.
   * Select **Warehouse native** to measure events from an external warehouse, such as Snowflake.
     * Select an existing **Metric data source** or click **+ Create** to create a new data source. To learn more, read [Metric data sources](/docs/home/warehouse-native/metric-data-sources).

4. If you chose the **LaunchDarkly hosted** event source, select an event kind of **Custom**.

5. Search for or enter an **Event key**.
   * A list of events your environment has recently received appears when you click into the **Event key** field. Begin typing an event key to view a list of events that match your search. Hover over an event from the list to view which environments the event appears in, which context kinds sent the latest event, and which SDKs the event is coming from.

6. In the [Metric definition](#metric-definition) section, choose **Count** from the drop-down menu. The window populates a full metric definition using default values.

   <Frame caption="A custom conversion count metric.">
     <img src="https://mintcdn.com/launchdarkly/A4UXoRTW8ATNw4yq/images/auto/metrics-event-kind-custom-count.auto.png?fit=max&auto=format&n=A4UXoRTW8ATNw4yq&q=85&s=be22c2abff4b4ce5bf3e5f6546bcea6a" alt="A custom conversion count metric." width="1220" height="926" data-path="images/auto/metrics-event-kind-custom-count.auto.png" />
   </Frame>

7. Change options in the "Metric definition" drop-down menus as needed:
   * Change or select additional context kinds to use as the analysis units for the metric. Only context kinds marked as available for experiments and guarded rollouts appear as options. To learn more, read [Mark context kinds available for experiments](/docs/home/experimentation/randomization#mark-context-kinds-available-for-experiments).
   * Select **Average** or a percentile between **P50** to **P99** to configure the analysis method.
   * Choose **higher is better** or **lower is better** to define success criteria for attached experiments or guarded rollouts.

8. Enter a metric **Name**.

9. (Optional) Add a **Description**.

10. (Optional) Add any **Tags**.

11. (Optional) Update the **Maintainer**.

12. Click **Create**.

You can connect the metric to an [experiment](/docs/home/experimentation/create) or [guarded rollout](/docs/home/releases/guarded-rollouts) to monitor the impact of a flag change.

<Card icon="https://mintcdn.com/launchdarkly/YIC2H8XW-fomhquw/assets/icons/openapi-logo.svg?fit=max&auto=format&n=YIC2H8XW-fomhquw&q=85&s=dd9578a9668a86e1a8b8c314921f204c" horizontal width="2500" height="2452" data-path="assets/icons/openapi-logo.svg">
  You can also use the REST API: [Create metric](/docs/api/metrics/create-metric)
</Card>
